When using xUnit, one can have the same test run multiple times with different data using the InlineData attribute.
[Theory]
[InlineData("A", 1)]
[InlineData("B", 2)]
public void TestAllValues(string x, int y)
I want to combine those parameters in all possible combinations. I could write it as follow.
[Theory]
[InlineData("A", 1)]
[InlineData("A", 2)]
[InlineData("A", 3)]
[InlineData("B", 1)]
[InlineData("B", 2)]
[InlineData("B", 3)]
public void TestAllValues(string x, int y)
In my case I need to test a lot more combinations, lets say for each letter of the alphabet and for every number from one to 100. I like to write something like
[Theory]
[InlineData("A-Z", 1..100)]
public void TestAllValues(string x, int y)
Or any equivalent that does not require 2.600 lines. The example is made up for simplicity, but I really need a lot of cases to be tested.
As a bonus question. Can I reflect the combination in the name of the Test?
Turns out that there is something called the MemberData
attribute.
[Theory]
[MemberData("AllCombinations")]
public void TestAllValues(string x, int y)
{
Where you can generate all desired combinations.
public static IEnumerable<object[]>AllCombinations{
get
{
foreach(var c in generateCombinations()){
yield return new object [] { c.Letter, c.Number} //
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With