[英]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.