我正在寻找与MbUnit的[DynamicTestFactory]最接近的NUnit,所以我可以在运行时创建动态测试。 NUnit中有等价物吗?谢谢。
答案 0 :(得分:2)
我没有使用过MbUnit,但我在NUnit中与DynamicTestFactory
最接近的是TestCaseSource
。
我找到了DynamicTestFactory
的例子(来自here):
[DynamicTestFactory]
public IEnumerable<Test> Should_Create_And_Execute_Dynamic_Tests()
{
IEnumerable<int> list = new[] {1, 2, 3, 4, 5};
foreach (int i in list)
{
yield return new TestCase(string.Format("Test {0}",i),
() => { Assert.IsTrue(MyFunction(i)); });
}
}
这就是你如何使用NUnit的TestCaseSource
(见here)来完成同样的事情:
[Test, TestCaseSource("SourceList")]
public void MyFunctionTest(int i)
{
Assert.IsTrue(MyFunction(i));
}
private static readonly IEnumerable<int> SourceList = new[] { 1, 2, 3, 4, 5 };