Are there Parameterized Test Fixtures for xunit?

风格不统一 提交于 2021-02-18 22:15:45

问题


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

回答1:


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.

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. )

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

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

* Update *

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!




回答2:


As of July 25th, the devs have decided not to support parameterized text fixtures in xUnit 2.x.




回答3:


I found only this project: https://github.com/vytautas-mackonis/xUnit.Paradigms. It is still on the 1.9.2 version of xUnit.



来源:https://stackoverflow.com/questions/28211431/are-there-parameterized-test-fixtures-for-xunit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!