Especially for C# Unit Testing, how to make a test run for various inputs or an input matrix?
I very often have the case of unit tests making a matrix of inputs, but usually it can be reasonably solved by checking a list of inputs.
The simplest case is testing the addition:
- 0 + 0 = 0
- 1 + 0 = 1
- 0 + -1 = -1
- 1 + -1 = 0
The same tests would be done for each of those inputs (in this example there are 4 inputs, each associated to some expected output.
The simple solution is:
object[] inputs = ...
foreach (var input in inputs)
// Put Assert here
The problem with that is that when you run your test it doesn't tell for which input it failed unless you include enough details in the message log manually.
Another solution is to unroll the loop (like below). The stack trace then give which input failed and this allows to run the test for a single input again.
TestAddition(0, 0, 0);
TestAddition(1, 0, 1);
TestAddition(0, -1, -1);
TestAddition(1, -1, 0);
Last but not least, are solutions like FitNess which seem to help such cases but also add a lot of complexity and don't seem to fit well for standard C# development.
All those solution don't work well with fixtures, because the [TestInitialize]
is called once for all inputs.
What is your solution?
Is there some feature I don't know of that helps those cases?