xunit 是否有参数化的测试装置? [英] Are there Parameterized Test Fixtures for xunit?

查看:24
本文介绍了xunit 是否有参数化的测试装置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

xunit.net 是否支持像 nunit 这样的参数化测试装置"(参见下面的示例代码).注意我寻找 IUseFixture[Theory] 因为它们不提供与 参数化测试装置

Does xunit.net support "Parameterized Test Fixtures" like nunit (see example code below). Note I'm not looking for IUseFixture<T> or [Theory] because these do not provide the same functionality as 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());
    }
}

推荐答案

我正在考虑从 NUnit 切换到 xUnit,最初被这个 SO 问题误导,认为 xUnit 无法直接执行与参数化测试装置等效的任何事情.

I was looking at switching from NUnit to xUnit, and was initially misled by this SO question into thinking that xUnit could not do anything directly equivalent to parameterized test fixtures.

但它可以——使用[Theory]请参阅下面的更新:xUnit 理论不像 NUnit 理论,实际上更像 NUnit 参数化测试!(因此,请注意,问题中的一个陈述假设是错误的,我认为这是消除错误假设后所能给出的最佳答案.)

But it can - using [Theory]! See update below: xUnit theories are not like NUnit theories, and are actually a lot more like NUnit parameterized tests! (Therefore, please note, one stated assumption in the question is false, and I think that this is the best answer that can be given once one removes that false assumption. )

这是一个经过轻微重构的 xUnit 版本的代码,它执行了相同的六个测试:

Here is a lightly refactored xUnit version of the code that does the same six tests:

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());
    }
}

或者,如果您需要,这个稍微复杂的代码会执行与原始问题中完全相同的数据所驱动的六个测试:

Or, in case you need it, this slightly more complex code does the same six tests driven by exactly the same data as in the original question:

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());
    }
}

请参阅此参考资料,但请注意 PropertyDataAttribute 现在已过时,取而代之的是 MemberDataAttribute.

See this reference, although note that PropertyDataAttribute is now obsolete in favour of MemberDataAttribute.

* 更新 *

这里可能引起严重混淆的一个原因是 NUnit 中还有一个 [TheoryAttribute],但它与 xUnit 中的同名属性具有不同意义.NUnit 理论中的每个子测试都被组合成一个通过或失败的测试(因此,是的,它不能用于实现与 NUnit 参数化测试装置相似的语义).但是 xUnit 理论中的每个子测试"在运行器中都作为一个单独的测试出现,即它增加了测试的数量,非常像 NUnit 参数化测试所做的那样!

A possible source of significant confusion here is that there is also a [TheoryAttribute] in NUnit, but it has different significance from the same-named attribute in xUnit. Every sub-test in an NUnit theory is combined into one test which passes or fails (so, yes, it couldn't be use to achieve similar semantics to an NUnit parameterized test fixture). But every 'sub-test' in an xUnit theory appears in the runner as a separate test, i.e. it multiplies up the number of tests, very much in the way that NUnit parameterized tests do!

这篇关于xunit 是否有参数化的测试装置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆