Is it possible to run parameterized test for each method of test class with JUnitParams and class level annotations? Something like:
是否可以使用JUnitParams和类级别注释为每个测试类方法运行参数化测试?就像是:
@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) {
......
}
}
2 个解决方案
#1
2
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. 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).
不,您不能拥有涵盖所有测试方法的类级参数注释。您必须在每个测试方法上声明@Parameters(method =“paramsGenerator”)。您的用例非常罕见 - 大多数情况下,不同的测试方法需要不同的参数(例如,您有一种有效方法和一种无效输入方法)。
#2
0
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. As you can see in my example, JUnit 4 defines Parameterized
.
我不知道你从哪里得到JUnitParamsRunner。正如您在我的示例中所看到的,JUnit 4定义了Parameterized。
#1
2
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. 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).
不,您不能拥有涵盖所有测试方法的类级参数注释。您必须在每个测试方法上声明@Parameters(method =“paramsGenerator”)。您的用例非常罕见 - 大多数情况下,不同的测试方法需要不同的参数(例如,您有一种有效方法和一种无效输入方法)。
#2
0
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. As you can see in my example, JUnit 4 defines Parameterized
.
我不知道你从哪里得到JUnitParamsRunner。正如您在我的示例中所看到的,JUnit 4定义了Parameterized。