Legacy Documentation. View NUnit 3 Documentation

PairwiseAttribute (NUnit 2.5)

The PairwiseAttribute is used on a test to specify that NUnit should generate test cases in such a way that all possible pairs of values are used. This is a well-known approach for combatting the combinatorial explosion of test cases when more than two features (parameters) are involved.


Using the Combinatorial attribute, the following test would be executed 12 (3x2x2) times. With Pairwise it is executed only enough times so that each possible pair is covered..

[Test, Pairwise]
public void MyTest(
    [Values("a", "b", "c")] string a,
    [Values("+", "-")] string b,
    [Values("x", "y")] string c)
    Console.WriteLine("{0} {1} {2}", a, b, c);

For this test, NUnit currently calls the method seven times, producing the following output:

	a - x
	b + y
	c + x
	b - x
	a - y
	c - y
	a + x

Note that this is not the optimal output. The pairs (a, -), (a, x), (+ x), (- x) and (- y) appear twice. NUnit uses a heuristic algorithm to reduce the number of test cases as much as it can. Improvements may be made in the future.


When used on a generic method the programmer must ensure that all possible combinations of arguments are valid. When multiple parameters use the same generic type (e.g.: T) this may not be possible and the attribute may generate invalid test cases.

See also...