在NUnit
中,如果在同一个Datapoint(s)Attribute
类中存在多个理论,是否有任何方法表明仅将TestFixture
应用于一种理论?
我问的原因是,我通常遵循单元测试约定,其中测试类(CUT)的所有方法都由多个[Test]
方法测试,这些方法被卷成一个
单一测试夹具类,现在正尝试从参数化测试转向[Theory]
。
还是我应该继续将参数化测试的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)));
}
}
编辑
上面给出的示例不是
Theory
用法的一个很好的示例-它更适合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)));
}
}