Parameterizing Tests More Effectively

Introduction

Unit tests are either non-parameterized or parameterized:

  • A non-parameterized test means that a single test case is associated with a single test method.
  • A parameterized test means that multiple cases are associated with a test method. Furthermore, there are ways in which tests are parameterized.

Parameterization-specific attributes are attributes that enrich or enhance parametrization. There are six parameterization-specific attributes:

  • Combinatorial
  • Range
  • Random
  • Pairwise
  • Sequential
  • TestCaseSource

Each parameterization-specific attribute is covered below with other related attributes.

The Values attribute

The Values attribute is used with the Combinatorial, Range, Random, and Pairwise attributes. It is essentially an attribute that contains a list of various values. When used in conjunction with the other attributes, it supplies these attributes with the values defined in the values list. Its use is demonstrated in the subsections below.

The Combinatorial attribute

The Combinatorial attribute allows you to arrange all input data provided in the associated Values attribute according to various combinations. An example of this attribute usage is shown below:

[Test, Combinatorial]
public void MyTest([Values(1, 2, 3, 4)] int a, [Values(5, 6)] int b)
{
    //...
}

This code will result in the test method being called eight times as follows:

  • MyTest(1, 5)
  • MyTest(1, 6)
  • MyTest(2, 5)
  • MyTest(2, 6)
  • MyTest(3, 5)
  • MyTest(3, 6)
  • MyTest(4, 5)
  • MyTest(4, 6)

The Range attribute

The Range attribute allows you to arrange all input data according to the provided range. The first argument is the start of the range, the second argument is the end of the range, and the third argument is the increment used.

[Test]
public void MyTest([Range(2, 10, 2)] int a)
{
   //...
}

This code will result in the test method being called five times as follows:

  • MyTest(2)
  • MyTest(4)
  • MyTest(6)
  • MyTest(8)
  • MyTest(10)

This attribute can be paired with any other attribute. For example, it may be paired with the Values attribute as follows:

[Test]
public void Test1([Values(1, 2)] int a, [Range(2, 4, 1)] int b)
{
   //...
}

This code will result in the test method being called six times as follows:

  • MyTest(1, 2)
  • MyTest(1, 3)
  • MyTest(1, 4)
  • MyTest(2, 2)
  • MyTest(2, 3)
  • MyTest(2, 4)

The Random attribute

The Random attribute allows you to arrange all input data provided in the associated Values attribute according to randomly selected data. The first parameter is the lowest bound of a random number. The second parameter is the highest bound of the random number. The third parameter is the number of random numbers to be generated. An example of this attribute usage is shown below:

[Test]
public void MyTest([Random(-1.0, 1.0, 5)] double a)
{
   //...
}

This code will result in the test method being called five times as follows:

  • MyTest(-0.7654...)
  • MyTest(0.3458...)
  • MyTest(0.0035...)
  • MyTest(-0.9133...)
  • MyTest(0.4774...)

This attribute can be paired with any other attribute.

The Pairwise attribute

The Pairwise attribute allows you to pair input data provided in the Values attribute. This ensures that every possible pair from values in the Values set is matched.

An example of the usage of this attribute is shown below. Note that the test method does not contain any assertions but rather a console output, so you may see how the values are paired up.

Get hands-on with 1400+ tech skills courses.