你可以将迭代传递给像pytest这样的Nuint测试吗?

时间:2018-05-22 20:34:56

标签: nunit

使用Pytest,您可以将迭代传递给一个测试方法,它将运行多个测试:

the_data = [1,2,3,4,5,a,6,7,8]

@pytest.mark.parameterize('arg', the_data)
def test_data(arg):
    assert arg.isnumeric()

你能用Nunit(使用c#)吗?

1 个答案:

答案 0 :(得分:1)

是的,您可以使用[TestCaseSource],如下所示;

[TestCaseSource(nameof(TheData))]
public void TestData(object i)
{
    Assert.That(i is int);
}

public static IEnumerable TheData => new object[] { 1, 2, 3, 4, 5, 'a', 6, 7, 8 };

请注意,测试数据必须是静态的。以上结果使用NUnit测试适配器在Visual Studio中进行了以下测试。

enter image description here