13

I have a unit-test that tests a variety of cases, like this:

public void Test1(Int32 a, Int32 b, Int32 c)

Let's say I want to create test-code without a loop, so I want to use TestCase to specify parameters like this:

[TestCase(1, 1, 1)]
public void Test1(Int32 a, Int32 b, Int32 c)

Is it possible for me with this attribute to say this:

  • For the first parameter, here's a set of values
  • For the second parameter, here's a set of values
  • For the third parameter, here's a set of values
  • Now, test all combinations of the above

Ie. something like this:

[TestCase(new[] { 1, 2, 3, 4 }, new[] { 1, 2, 3, 4 }, new[] { 1, 2, 3, 4 })]
public void Test1(Int32 a, Int32 b, Int32 c)

Doesn't seem like it, but perhaps I'm overlooking something?

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825

1 Answers1

17

NUnit provides the Values attribute which can be used together with Combinatorial attribute to achieve this:

[Test, Combinatorial]
public void Test1( 
    [Values(1,2,3,4)] Int32 a, 
    [Values(1,2,3,4)] Int32 b, 
    [Values(1,2,3,4)] Int32 c
)
{
    ...
}
Bojan Resnik
  • 7,320
  • 28
  • 29
  • That worked, and Combinatorial is apparently the default as well, intellisense doc said that, and I tried without that particular attribute and it worked exactly like you said it would. Thanks a bunch. – Lasse V. Karlsen Oct 23 '09 at 09:27
  • 1
    @LasseV.Karlsen In addition to the `Values` attribute, the [`Range` attribute](http://www.nunit.org/index.php?p=range&r=2.5) can also be used in the same manner. Instead of explicitly listing all of the values, they can be specified as a range. So, in your example, instead of `[Values(1,2,3,4)]`, you can substitute with `[Range(1,4,1)]`. – Ray Oct 04 '11 at 16:05