title: Unit testing various foreach of some inputs
body: <p>Especially for <em>C# Unit Testing</em>, how to make a test run for <strong>various inputs</strong> or an input matrix?</p>
  
  <p>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.</p>
  
  <p>The simplest case is testing the <em>addition</em>:</p>
  
  <ul>
  <li>0 + 0 = 0</li>
  <li>1 + 0 = 1</li>
  <li>0 + -1 = -1</li>
  <li>1 + -1 = 0</li>
  </ul>
  
  <p>The same tests would be done for each of those inputs (in this example there are 4 inputs, each associated to some expected output.</p>
  
  <p>The <strong>simple solution</strong> is:</p>
  
  <pre><code>object[] inputs = ...
  foreach (var input in inputs)
     // Put Assert here
  </code></pre>
  
  <p>The problem with that is that when you run your test it <strong>doesn't tell for which input it failed</strong> unless you include enough details in the message log <em>manually</em>.</p>
  
  <p>Another solution is to <strong>unroll the loop</strong> (like below). The stack trace then give which input failed and this allows to run the test for a single input again.</p>
  
  <pre><code>TestAddition(0, 0, 0);
  TestAddition(1, 0, 1);
  TestAddition(0, -1, -1);
  TestAddition(1, -1, 0);
  </code></pre>
  
  <p>Last but not least, are solutions like <a href="http://fitnesse.org/FitNesse.UserGuide.TwoMinuteExample" rel="nofollow noreferrer">FitNess</a> which seem to help such cases but also add a lot of complexity and don't seem to fit well for standard C# development.</p>
  
  <p><strong>All those solution don't work well with fixtures, because the <code>[TestInitialize]</code> is called once for all inputs.</strong></p>
  
  <p><em>What is your solution?</em></p>
  
  <p><em>Is there some feature I don't know of that helps those cases?</em></p>