c# - 在 NUnit 中,如何指示 'DataPoint' 仅适用于一种理论?

标签 c# nunit nunit-2.5

NUnit中,如果存在多个理论,是否有任何方法表明Datapoint(s)Attribute应该仅应用于一种理论相同的 TestFixture 类?

我问的原因是,我通常遵循单元测试约定,其中测试类(CUT)的所有方法都通过滚入一个单元的多个 [Test] 方法进行测试。 单个测试夹具类,现在我正在尝试从参数化测试转向[理论]

或者我应该继续使用Values / Range / Random此类测试的参数化测试的属性?

例如下面,我想确保不同的数据点适用于加法和除法理论:

// C.U.T.
public class BadMaths
{
    public int BadAdd(int x, int y) { return x + y - 1; }
    public int Divide(int x, int y) { return x / y; }
}

[TestFixture]
public class BadMathsTest
{
    // Ideally I want 2 x different datapoints - one for Add, and a different one for divide
    [Datapoints]
    private Tuple<int, int>[] _points = new Tuple<int, int>[]
        {
           new Tuple<int, int>(20, 10),
           new Tuple<int, int>(-10, 0),
        };

    [Theory]
    public void AddTheory(Tuple<int, int> point)
    {
        Assume.That((long)point.Item1 + (long)point.Item2 < (long)int.MaxValue);
        Assert.That(point.Item1 + point.Item2, Is.EqualTo(new BadMaths().BadAdd(point.Item1, point.Item2)));
    }

    [Theory]
    public void DivideTheory(Tuple<int, int> point)
    {
        Assume.That(point.Item2 != 0); // Seems the best I can do - test is inconclusive
        Assert.That(point.Item1 / point.Item2, Is.EqualTo(new BadMaths().Divide(point.Item1, point.Item2)));
    }

}

编辑

上面给出的示例并不是理论用法的一个很好的示例 - 它更适合 TestCaseSource ,以及新的 Roslyn nameof源数据上不需要 [DataPoints][UsedImplicitly] 属性。

    [TestCaseSource(nameof(_points)]
    public void EnsureAddPoints(Tuple<int, int> point)
    { ....

最佳答案

我不认为有任何直接的方法可以要求 NUnit 针对不同的理论使用相同类型的不同数据点。但是,您可以通过两种可能的方法来解决:

第一种方法是使用不同的 TextFixture 类来进行需要不同数据点值的测试:

[TestFixture]
public class BadMathsAdditionTest
{
    // Ideally I want 2 x different datapoints - one for Add, and a different one for divide
    [Datapoints]
    private Tuple<int, int>[] _points = new Tuple<int, int>[]
    {
       new Tuple<int, int>(20, 10),
       new Tuple<int, int>(-10, 0),
    };

    // add tests that use these datapoints
    [Theory]
    public void AddTheory(Tuple<int, int> point)
    {
        Assume.That((long)point.Item1 + (long)point.Item2 < (long)int.MaxValue);
        Assert.That(point.Item1 + point.Item2, Is.EqualTo(new BadMaths().BadAdd(point.Item1, point.Item2)));
    }

}

[TestFixture]
public class BadMathsDivisionTest
{

    // Ideally I want 2 x different datapoints - one for Add, and a different one for divide
    [Datapoints]
    private Tuple<int, int>[] _points = new Tuple<int, int>[]
    {
       new Tuple<int, int>(20, 10),
    };

    // add test that use these datapoints

}

第二种方法需要更多的工作,但可以说提供了更多可读的代码,即将每个数据点集包装在不同的结构中,如下所示:

// C.U.T.
public class BadMaths
{
    public int BadAdd(int x, int y) { return x + y - 1; }
    public int Divide(int x, int y) { return x / y; }
}

[TestFixture]
public class BadMathsTest
{

    public struct AdditionData
    {
        public int First { get; set; }
        public int Second { get; set; }
    }
    [Datapoints]
    private AdditionData[] _points = new AdditionData[]
    {
        new AdditionData{First=20, Second=10},
        new AdditionData{First=-10, Second=0}
    };


    public struct DivisionData
    {
        public int First { get; set; }
        public int Second { get; set; }
    }

    [Datapoints]
    private DivisionData[] _points2 = new DivisionData[]
    {
        new DivisionData{First=20, Second=10},
    };

    [Theory]
    public void AddTheory(AdditionData point)
    {
        Assume.That((long)point.First + (long)point.Second < (long)int.MaxValue);
        Assert.That(point.First + point.Second, Is.EqualTo(new BadMaths().BadAdd(point.First, point.Second)));
    }

    [Theory]
    public void DivideTheory(DivisionData point)
    {
        Assume.That(point.Second != 0); // Actually you probably want to keep this condition anyway. Second==0 would be a separate test
        Assert.That(point.First / point.Second, Is.EqualTo(new BadMaths().Divide(point.First, point.Second)));
    }

}

关于c# - 在 NUnit 中,如何指示 'DataPoint' 仅适用于一种理论?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16515561/

相关文章:

c# - 更改 outlook MailItem 图标

c# - NUnit 不能很好地与 Assert.AreEqual 配合使用

c# - 如何将 "Assert.That()"列表中的项目与 NUnit 匹配某些条件?

testing - 在运行时为 NUnit 测试提供参数

c# - Parallel.ForEach 迭代中的操作超时

c# - GridView 导出到 Excel 正在导出整个 aspx 页面

visual-studio - 使用 coderush 或 testdriven.net 在 Visual Studio 中运行的 NUnit "no tests found"

NUnit 扩展

c# - NUnit 可以期待超时吗?

c# - 在实时图表中设置轴的最小值