Visual Studio参数化单元测试就像java一样

bay*_*ncu 6 c# java tdd junit unit-testing

在java测试环境中我可以使用参数化单元测试,如下面的代码,

@RunWith(value = Parameterized.class)
public class JunitTest6 {

    private int number;

    public JunitTest6(int number) {
        this.number = number;
    }

    @Parameters
    public static Collection<Object[]> data() {
        Object[][] data = new Object[][] { { 1 }, { 2 }, { 3 }, { 4 } };
        return Arrays.asList(data);
    }

    @Test
    public void pushTest() {
        System.out.println("Parameterized Number is : " + number);
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我怎么能在visual studio单元测试项目中做到这一点.我找不到参数化属性或这样的任何样本.

Kja*_*tan 8

使用NUnit框架,您可以将参数传递给测试,如下所示:

[TestCase(1, 2, 3)]
[TestCase(10, 20, 30)]
public void My_test_method(int first, int second, int third)
{
    // Perform the test
}
Run Code Online (Sandbox Code Playgroud)

这将分两次1, 2, 3运行,在第一次运行中传递值,10, 20, 30在第二次运行中传递值.

编辑:有关NUnit的可用测试运行器的概述,请参阅此SO问题