xunit.net是否支持&#34;参数化测试夹具&#34;像nunit(见下面的示例代码)。请注意,我不正在寻找IUseFixture<T>
或[Theory]
,因为这些功能与Parameterized Test Fixtures不提供相同的功能
[TestFixture("hello", "hello", "goodbye")]
[TestFixture("zip", "zip")]
[TestFixture(42, 42, 99)]
public class ParameterizedTestFixture
{
private string eq1;
private string eq2;
private string neq;
public ParameterizedTestFixture(string eq1, string eq2, string neq)
{
this.eq1 = eq1;
this.eq2 = eq2;
this.neq = neq;
}
public ParameterizedTestFixture(string eq1, string eq2)
: this(eq1, eq2, null) { }
public ParameterizedTestFixture(int eq1, int eq2, int neq)
{
this.eq1 = eq1.ToString();
this.eq2 = eq2.ToString();
this.neq = neq.ToString();
}
[Test]
public void TestEquality()
{
Assert.AreEqual(eq1, eq2);
if (eq1 != null && eq2 != null)
Assert.AreEqual(eq1.GetHashCode(), eq2.GetHashCode());
}
[Test]
public void TestInequality()
{
Assert.AreNotEqual(eq1, neq);
if (eq1 != null && neq != null)
Assert.AreNotEqual(eq1.GetHashCode(), neq.GetHashCode());
}
}
答案 0 :(得分:2)
答案 1 :(得分:1)
我正在考虑从NUnit切换到xUnit,最初被这个SO问题误导,认为xUnit无法做任何直接等同于参数化测试装置的事情。
但它可以 - 使用[Theory]
! 请参阅下面的更新:xUnit理论与NUnit理论不同,实际上更像NUnit参数化测试!(因此,请注意,问题中有一个陈述的假设是假的,我认为这个一旦删除了错误假设,就可以得到最好的答案。)
这是一个轻微重构的xUnit版本的代码,它执行相同的六个测试:
public class ParameterizedTestFixture
{
public static IEnumerable<object[]> TestCases = new[] {
new object[] { "hello", "hello", "goodbye" },
new object[] { "zip", "zip", null },
new object[] { "42", "42", "99" }
};
[Theory]
[MemberData(nameof(TestCases))]
public void TestEquality(string eq1, string eq2, string neq)
{
Assert.Equal(eq1, eq2);
if(eq1 != null && eq2 != null)
Assert.Equal(eq1.GetHashCode(), eq2.GetHashCode());
}
[Theory]
[MemberData(nameof(TestCases))]
public void TestInequality(string eq1, string eq2, string neq)
{
Assert.NotEqual(eq1, neq);
if(eq1 != null && neq != null)
Assert.NotEqual(eq1.GetHashCode(), neq.GetHashCode());
}
}
或者,如果您需要它,这个稍微复杂的代码执行相同的六个测试,这些测试由完全驱动与原始问题相同的数据:
public class ParameterizedTestFixture
{
public static IEnumerable<object[]> TestCases = new[] {
new object[] { "hello", "hello", "goodbye" },
new object[] { "zip", "zip", null },
new object[] { 42, 42, 99 }
};
private string eq1;
private string eq2;
private string neq;
public void Init(object _eq1, object _eq2, object _neq)
{
this.eq1 = (_eq1 == null ? null : _eq1.ToString());
this.eq2 = (_eq2 == null ? null : _eq2.ToString());
this.neq = (_neq == null ? null : _neq.ToString());
}
[Theory]
[MemberData(nameof(TestCases))]
public void TestEquality(object _eq1, object _eq2, object _neq)
{
Init(_eq1, _eq2, _neq);
Assert.Equal(eq1, eq2);
if(eq1 != null && eq2 != null)
Assert.Equal(eq1.GetHashCode(), eq2.GetHashCode());
}
[Theory]
[MemberData(nameof(TestCases))]
public void TestInequality(object _eq1, object _eq2, object _neq)
{
Init(_eq1, _eq2, _neq);
Assert.NotEqual(eq1, neq);
if(eq1 != null && neq != null)
Assert.NotEqual(eq1.GetHashCode(), neq.GetHashCode());
}
}
请参阅this reference,但请注意PropertyDataAttribute
现已过时,转而使用MemberDataAttribute
。
*更新*
这里可能存在重大混淆的原因是NUnit中还有一个[TheoryAttribute]
,但它与xUnit中同名属性的不同重要性。 NUnit理论中的每个子测试都被合并到一个测试中,该测试通过或失败(因此,是的,它无法用于实现与NUnit参数化测试夹具类似的语义)。但是,xUnit理论中的每个“子测试”都作为单独的测试出现在跑步者中,即它将测试次数增加,与NUnit参数化测试的方式非常相似!
答案 2 :(得分:0)
我发现只有这个项目:https://github.com/vytautas-mackonis/xUnit.Paradigms。 它仍然在xUnit的1.9.2版本上。