简体   繁体   English

每个类的JUnitParams

[英]JUnitParams for each method of class

Is it possible to run parameterized test for each method of test class with JUnitParams and class level annotations? 是否可以为每个带有JUnitParams和类级别注释的测试类方法运行参数化测试? Something like: 就像是:

@RunWith(JUnitParamsRunner.class)
@Parameters(method = "paramsGenerator")
public class TestMethod {

    public Integer[] paramsGenerator() {
        return new Integer[] {1, 2, 3};
    }

    @Test
    public void test1(Integer myInt) {
        ......
    }

    @Test
    public void test2(Integer myInt) {
        ......
    }
}

No, you cannot have a class-level parameters annotation that would cover all test methods. 不可以,您不能具有涵盖所有测试方法的类级参数注释。 You must declare @Parameters(method = "paramsGenerator") on each test method. 您必须在每个测试方法上声明@Parameters(method = "paramsGenerator") Your use case is pretty rare - most often different test methods require different parameters (eg. you have one method for valid and one for invalid input). 您的用例非常少见-大多数情况下,不同的测试方法需要不同的参数(例如,您有一种方法有效,而另一种方法无效)。

The test class should be like this: 测试类应如下所示:

@RunWith(Parameterized.class)
public class Testcase {

    private int myInt;

    public Testcase(int myInt) {
        this.myInt = myInt;
    }

    @Parameters(name = "{0}")
    public static Collection<Integer> data() {
        Integer[] data = new Integer[] {1,2,3,4};

        return Arrays.asList(data);
    }

    @Test
    public void test1() {
        // TODO
    }

    @Test
    public void test2() {
        // TODO
    }
}

I don't know where you got JUnitParamsRunner from. 我不知道您从哪里获得JUnitParamsRunner的。 As you can see in my example, JUnit 4 defines Parameterized . 如您在我的示例中所见,JUnit 4定义了Parameterized

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM