简体   繁体   中英

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?

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} //
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM