【问题标题】:Are there Parameterized Test Fixtures for xunit?是否有用于 xunit 的参数化测试夹具?
【发布时间】:2015-01-29 09:44:57
【问题描述】:

xunit.net 是否支持像 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());
    }
}

【问题讨论】:

  • 好问题。不幸的是,我也有同样的问题。

标签: c# unit-testing testing xunit xunit.net


【解决方案1】:
【解决方案2】:

我正在考虑从 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 参数化测试的方式!

【讨论】:

    【解决方案3】:

    我只找到了这个项目:https://github.com/vytautas-mackonis/xUnit.Paradigms。 它仍然在 xUnit 的 1.9.2 版本上。

    【讨论】:

      猜你喜欢
      • 2019-05-21
      • 1970-01-01
      • 2020-03-27
      • 1970-01-01
      • 2011-03-10
      • 1970-01-01
      • 2018-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多