Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NUnit and TestCaseAttribute, cross-join of parameters possible?

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?

like image 555
Lasse V. Karlsen Avatar asked Oct 23 '09 08:10

Lasse V. Karlsen


People also ask

Which of the named parameters are supported by the TestCase attribute?

This attribute that helps in coming up with an NUnit parameterized test also supports several additional named parameters like Author, Category, Description, ExpectedResult, TestName, etc. The execution order of the TestCase attribute can vary when used in combination with other data-providing attributes.

Is a set of Testcases in NUnit?

There are other NUnit attributes that enable you to write a suite of similar tests. A [TestCase] attribute is used to create a suite of tests that execute the same code but have different input arguments. You can use the [TestCase] attribute to specify values for those inputs.

How can you make a NUnit test case Datadriven?

The first way to create data driven tests is by using the [TestCase] attribute that NUnit provides. You can add multiple [TestCase] attributes for a single test method, and specify the combinations of input and expected output parameters that the test method should take.


1 Answers

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
)
{
    ...
}
like image 81
Bojan Resnik Avatar answered Oct 12 '22 23:10

Bojan Resnik