简体   繁体   English

让xUnit组合参数

[英]Let xUnit combine parameters

When using xUnit, one can have the same test run multiple times with different data using the InlineData attribute. 使用xUnit时,可以使用InlineData属性使用不同的数据多次执行相同的测试运行。

  [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 在我的情况下,我需要测试更多的组合,比如对于字母表中的每个字母以及从1到100的每个数字。我喜欢写类似的东西

  [Theory]
  [InlineData("A-Z", 1..100)]
  public void TestAllValues(string x, int y)

Or any equivalent that does not require 2.600 lines. 或者任何不需要2.600行的等价物。 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. 事实证明,有一种称为MemberData属性的东西。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在xunit / autofixture中组合PropertyData和AutoNSubstituteData属性? - How to combine PropertyData and AutoNSubstituteData attributes in xunit/autofixture? xUnit Abstract Test Class,带有带参数的虚拟测试方法 - xUnit Abstract Test Class with virtual test methods with parameters xUnit测试引擎的InlineDataAttribute +可选方法参数 - xUnit test engine's InlineDataAttribute + optional method parameters 如何将多个参数传递给在Matlab xUnit中共享相同设置代码的测试? - How to pass multiple parameters to tests that share the same setup code in Matlab xUnit? 单元测试 - XUnit - IAssemblyFixture - 以下构造函数参数没有匹配的夹具数据 - Unit Testing - XUnit - IAssemblyFixture -The following constructor parameters did not have matching fixture data 尝试使用 xUnit 测试服务时,构造函数参数没有匹配的夹具数据。 这是什么意思,我的问题在哪里? - Constructor parameters did not have matching fixture data, when trying to test a service using xUnit. What does this mean and where is my issue? xUnit没有等待异步测试 - xUnit not awaiting async test 使用 xUnit 测试控制器 - Testing controller with xUnit 使用XUnit测试动态Expando - Testing dynamic expando with XUnit Xunit调试不稳定 - Xunit Debug is Flaky
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM