Junit使用标有@Parameter的参数运行所有Test

时间:2015-09-23 10:41:28

标签: junit junit4 parameterized-unit-test

在以下代码中,我想使用标有TestMethod1

的参数运行@Parameters
    @RunWith(Parameterized.class)
public class Foo{

private boolean input;
private boolean expected;

public Foo(boolean input, boolean expected{
this.input=input;
this.expected=expected;
}

@Parameters
public static List<Object[]> data() {
        return Arrays.asList(new Object[][]{{false, false}, {false, false}});
    }

@Test
public void TestMethod1(){
assertEquals(expected, Baar.StaticMethod(input);
}

@Test
public void TestMethod2(){
assertEquals(expected, Baar.StaticMethod2(false);
}

问题是当我运行junittes时,两个方法TestMethod1和TestMethod2都使用这些参数运行。如何告诉testrunner只运行带有@Parameters标记参数的TestMethod1?

1 个答案:

答案 0 :(得分:0)

不确定纯junit是否允许它,但有很多插件。在您的情况下(所有参数已预先知道),最简单的方法是parametrized testing with zohhak

@RunWith(ZohhakRunner.class)
public class TestMyClass {      

    @TestWith({
        "true, false".
        "false, true"
    })
    public void test1(int actual, int expected) { //test }

    @TestWith({
        "false, false".
        "true, true"
    })
    public void test2(int actual, int expected) { //test }

    @Test
    public void test3() { //test }
}

如果您需要在运行时构建参数(生成,从文件读取等),那么您可以查看junit-dataproviderjunit-params

等内容