2012-07-26 30 views
4

我可以使用参数化单元测试像下面的代码,Visual Studio的参数化单元测试就像Java测试环境中的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); 
    } 
} 

但我怎么能做到这一点在Visual Studio单元测试项目。我找不到参数化属性或任何这样的示例。

+1

在这里看:http://stackoverflow.com/questions/2367033/mstest-equivalent-for-nunits-parameterized-tests – 2012-07-26 11:29:12

回答

5

使用NUnit framework,你会传递参数,像这样的测试:

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

这将运行两个不同的时代,在值1, 2, 3通过在第一次运行,并在第二10, 20, 30

编辑:对于可用的测试跑步者的NUnit的概述,see this SO question

+0

TestCase属性是不是退出NUnit或Visual Studio默认测试工具 – bayramucuncu 2012-07-27 07:45:40

+0

也是! ;):http://www.nunit.org/index.php?p=testCase&r=2.6请注意,这是一个与VS中的buildt-in不同的测试框架。请参阅此处的入门指南:http://www.nunit.org/index.php?p=getStarted&r=2.6 NUnit-test本身不能直接在VS中运行。然而,其他测试运行者可以为您运行它们。我个人使用Resharper testrunner,如果可能的话,会推荐它。看到这个更多的信息:http://stackoverflow.com/questions/336655/what-is-the-best-nunit-test-runner-out-there – Kjartan 2012-07-27 08:25:56

0

如果您能够接受引用NUnit的,检查出的页面Parameterized Tests。支持内联静态和动态数据值。

如果您不想使用NUnit,MSTest或VS单元会测试supports getting inputs from CSV,XML或DB。内联支持可通过an extension获得。动态支持尚未..如果您想动态计算输入/输出,您必须将动态代码添加到测试方法。