在中,如果同一类中有多个理论,NUnit
是否有任何方法表明 a应该只应用于一个理论?Datapoint(s)Attribute
TestFixture
我问的原因是,我通常遵循单元测试约定,其中测试类 (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)
{ ....