Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I run a .NET Unit Test repeatedly with a set of parameters cartesianed

I have 1 unit test method that needs several parameters. I would like to run this test once for every possible value of a cartesian with a predefined list for each parameter. I imagine that parameters could be passes in via the test context, but I do not want to connect to an external database.

For example: If I had 2 parameters with the following possible values, the test would execute 6 times (order doesn't matter). (pseudo-code)

p1 = { 1, 5, 10 }
p2 = { "blue", "red" }

test 1: ( 1, "red" )
test 2: ( 5, "red" )
test 3: ( 10, "red" )
test 4: ( 1, "blue" )
test 5: ( 5, "blue" )
test 6: ( 10, "blue" )

Note: I'm using the built-in Visual Studio 2010 unit testing, not NUnit or one of the many other unit test frameworks.

Edit:

I'm storing the possible values as enumerations in the test class, but it is also reasonable to use arrays. My goal is to automate the link between the source enumerations/arrays and the actual test. While my sample only has 2 parameters with 6 permutations, the actual set is much larger. I do not want to skip a scenerio just because I missed something in a manual conversion.

like image 737
chilltemp Avatar asked Jan 21 '23 23:01

chilltemp


1 Answers

you can do that with a tool like Gallio in .net

[Test]
[Row(0, 0)]
[Row(1, 1)]
public void Lower_bounds(int x, int expectedResult)
{
   int result = Fibonacci.Calculate(x);
   Assert.AreEqual(expectedResult, result);
}

Tip 1 : You could extend this further by learning about the [Factory] attribute in Gallio. Tip 2 : Pair-wise helps you to generate combination

Thank you Jeroen for Editing the code and for the support

like image 105
3 revs, 2 users 75% Avatar answered Mar 08 '23 23:03

3 revs, 2 users 75%