在NUnit
,有沒有什麼方法可以表明Datapoint(s)Attribute
只應用於一個理論,如果在同一TestFixture
類有多個理論?在NUnit中,我怎麼能指出'數據點'只適用於一個理論?
原因我想問的是,我通常遵循一個單元測試慣例,其中一個測試類(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)
{ ....
事實證明,[TestCaseSource](http://www.nunit.org/index.php?p=testCaseSource&r=2.5 )也可以用於理論(例如'[TestCaseSource(「_ pointsForAdd」)]'),a雖然似乎需要精確的簽名匹配。上面的例子不是理論用法的一個很好的例子,因爲'Tuple'依賴於x和y綁定 – StuartLC 2015-01-20 07:16:38
類似於[XUnit的PropertyData](http://www.tomdupont.net/2012/04/xunit-theory-data- driven-unit-test.html) – StuartLC 2015-05-06 07:31:04