Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Let xUnit combine parameters

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?

like image 954
Stijn Van Antwerpen Avatar asked Aug 18 '15 09:08

Stijn Van Antwerpen


1 Answers

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} //
        }
    }
like image 111
Stijn Van Antwerpen Avatar answered Sep 30 '22 03:09

Stijn Van Antwerpen