我有兩個單元測驗如下:
[DataTestMethod]
[DataRow(1, 1, 2)]
[DataRow(2, 2, 4)]
[DataRow(3, 3, 6)]
public void AddTest1(int x, int y, int expected)
{
Assert.AreEqual(expected, x y);
}
[DataTestMethod]
[DataRow(new int[] { 1, 2, 3, 4 }, new int[] { 1, 3, 6, 10 })]
[DataRow(new int[] { 1, 1, 1, 1, 1 }, new int[] { 1, 2, 3, 4, 5 })]
[DataRow(new int[] { 3, 1, 2, 10, 1 }, new int[] { 3, 4, 6, 16, 17 })]
public void AddTest2(int[] input, int[] expectedOutput)
{
Assert.AreEqual(input[0], expectedOutput[0]);
}
第一個單元測驗運行 3 次,每個資料行一次。第二個單元測驗只為第一個 DataRow 運行一次。
如何也為每個 DataRow 使用陣列運行第二個 UnitTests 一次?
uj5u.com熱心網友回復:
大多數測驗框架要求測驗用例使用編譯時常量作為引數。
這是有效的,因為引數在編譯時是恒定的:
[DataTestMethod]
[DataRow(1, 1, 2)]
[DataRow(2, 2, 4)]
[DataRow(3, 3, 6)]
public void AddTest1(int x, int y, int expected)
{
Assert.AreEqual(expected, x y);
}
這不起作用,因為您沒有使用編譯時常量:
[DataTestMethod]
[DataRow(new int[] { 1, 2, 3, 4 }, new int[] { 1, 3, 6, 10 })]
[DataRow(new int[] { 1, 1, 1, 1, 1 }, new int[] { 1, 2, 3, 4, 5 })]
[DataRow(new int[] { 3, 1, 2, 10, 1 }, new int[] { 3, 4, 6, 16, 17 })]
public void AddTest2(int[] input, int[] expectedOutput)
{
Assert.AreEqual(input[0], expectedOutput[0]);
}
帶有陣列的new關鍵字不允許測驗框架進行區分(查看您鏈接的影像,它只是說System.Int32[])。
一種解決方法可能是這樣的:
[DataTestMethod]
[DataRow(0, new int[] { 1, 2, 3, 4 }, new int[] { 1, 3, 6, 10 })]
[DataRow(1, new int[] { 1, 1, 1, 1, 1 }, new int[] { 1, 2, 3, 4, 5 })]
[DataRow(2, new int[] { 3, 1, 2, 10, 1 }, new int[] { 3, 4, 6, 16, 17 })]
public void AddTest2(int run, int[] input, int[] expectedOutput)
{
Assert.AreEqual(input[0], expectedOutput[0]);
}
這樣,不同的測驗用例可以通過編譯時常數來區分。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/511491.html
上一篇:在單元測驗中使用IEnumerable<Guid>進行單元測驗
下一篇:SimpleTestCase.settings()和django.test.override_settings有什么區別?
