2

在中,如果同一类中有多个理论,NUnit是否有任何方法表明 a应该只应用于一个理论?Datapoint(s)AttributeTestFixture

我问的原因是,我通常遵循单元测试约定,其中测试类 (CUT) 的所有方法都由[Test]滚动到单个测试夹具类中的多个方法进行测试,现在我正试图从参数化测试转向[Theory].

还是我应该继续使用参数化测试的值/范围/随机属性进行此类测试?

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

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

}

编辑

上面给出的示例不是一个很好的用法示例-Theory它更适合TestCaseSource,并且使用新的 Roslynnameof运算符,源数据上不需要 or 属性。[DataPoints][UsedImplicitly]

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

1 回答 1

3

我不相信有任何直接的方法可以要求 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)));
    }

}
于 2013-06-03T10:40:55.147 回答