[英]Run parameterized JUnit tests with different Parameters
I have a JUnit-Test that looks like this: 我有一个看起来像这样的JUnit-Test:
private final User user1;
private final User user2;
public UserTest(User user1, User user2) {
this.user1 = user1;
this.user2 = user2;
}
@Parameterized.Parameters
public static Collection<Object[]> userData() {
return Arrays.asList(new Object[][] {
{ new User("Tim", "Burton", "tim.burton"),
new User("tim", "burton", "timothy.burton") },
{ new User("Vincent", "van Gogh", "vincent.van.gogh"),
new User("vincent", "van-gogh", "vincent.vangogh") } });
}
@Test
public void testPositive() {
checkMatching(user1, user2);
}
Now all the tests are successful, but I want to create a second List of @Parameterized.Parameters for the negative tests. 现在所有测试都成功了,但是我想为负测试创建第二个@ Parameterized.Parameters列表。 The new method should look like this:
新方法应如下所示:
@Test
public void testNegative() {
checkFalseMatching(wrongUser1, wrongUser2);
}
Is it possible to use a method-specific parameters? 是否可以使用特定于方法的参数? I would use the parameters I've already created for the testPositive() method and the second list of parameters for the testNegative() method.
我将使用我已经为testPositive()方法创建的参数和testNegative()方法的第二个参数列表。
How can I do that? 我怎样才能做到这一点? Can I use a "scope" for my parameters or something similar?
我可以为我的参数或类似的东西使用“范围”吗?
A couple options here. 这里有两个选择。 First take in 3 or 4 parameters in your constructor...
首先在构造函数中接受3或4个参数...
public UserTest(User equalUser1, User equalUser2,
User notEqualUser1, User notEqualUser2);
Another option would be to use the @Enclosed running and separate the different set of Parameters / tests into separate inner classes. 另一个选择是使用@Enclosed运行并将不同的参数/测试集分成不同的内部类。 This is generally a good practice anyway since if you have a mix of parameterized tests and non-parameterized tests you would not want to run the non-parameterized tests multiple times.
这通常是一种很好的做法,因为如果你有参数化测试和非参数化测试的混合,你不希望多次运行非参数化测试。
Another option is to use Theories and to use the assumeThat method to check that the arguments are in a specific set. 另一种选择是使用Theories并使用assumeThat方法来检查参数是否在特定集合中。
You can also try junitparams . 你也可以试试junitparams 。 With junitparams you can parameterise tests via a method that returns parameter values.
使用junitparams,您可以通过返回参数值的方法对测试进行参数化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.