Pairwise
PairwiseAttribute tells NUnit to generate test cases so all possible pairs of parameter values are covered, while using fewer cases than full combinatorial generation.
Usage
This is a parameterless attribute that can only be applied to test methods.
[Pairwise]
Applies To
| Test Methods | Test Fixtures (Classes) | Assembly |
|---|---|---|
| ✅ | ❌ | ❌ |
Example
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)
{
Assert.That(a, Is.Not.Null);
Assert.That(b, Is.Not.Null);
Assert.That(c, Is.Not.Null);
}
For this test, NUnit currently calls the method six times, producing the following output:
a - x
a + y
b - y
b + x
c - x
c + y
Note that this is not the optimal output. The pairs (-, 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.
Notes
- Pairwise generation is heuristic and may not produce the theoretical minimum number of test cases.
- When used on generic methods, ensure generated combinations are valid for all type constraints.
- Use
Combinatorialwhen you need full coverage of all combinations.