问题描述
是否可以在junit 4中参数化TestSuite?
Is it possible to parameterize a TestSuite in junit 4 ?
为了将类声明为测试套件,我需要注释 @RunWith( Suite.class)
,但是也需要相同的注释来将测试声明为参数化: @RunWith(Parameterized.class)
所以我无法添加两个人都在同一个班级。
For declaring a class as a test suite I need the annotation @RunWith(Suite.class)
, but the same annotation is also needed to declare the test as parameterized: @RunWith(Parameterized.class)
so I cannot add both to the same class.
我发现了一个类似的。
I believe the basic answer is No, because as you said, the @RunsWith only take one parameter. I found a blog posting that got a bit creative in how to handle this situation.
我们不使用参数化测试,但您可以创建一个单独的套件,就像我们那样只列出测试类,参数化测试可能是其中的一部分。我修改了我们的测试套件,将参数化的测试类包含在套件的一部分中,运行正常。我们创建我们的套件,如下所示PrimeNumberCheckerTest是一个简单的网站。
We don't use the parameterized tests, but may you could create a separate suite like we do that only lists the test classes and the parameterized test could be part of that. I modified our test suite to include a parameterized test class to part of the suite and it ran fine. We create our suite like below where PrimeNumberCheckerTest was a simple I pulled from the web.
package com.jda.portfolio.api.rest.server;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ com.mycompany.api.rest.server.resource.TestCartResourceJava.class,
com.mycompany.api.rest.server.resource.TestCustomerResource.class,
com.mycompany.api.rest.server.resource.TestWizardProfileResource.class,
com.mycompany.api.rest.server.interceptor.TestBaseSearchInterceptor.class,
com.mycompany.api.rest.server.resource.TestQueryParameters.class,
com.mycompany.api.rest.server.expression.TestCartExpressionGenerator.class,
com.mycompany.api.rest.server.expression.TestPreferenceExpressionGenerator.class,
com.mycompany.api.rest.server.PrimeNumberCheckerTest.class,
})
public class AllTests {}
以下是参数化测试用例的来源;
Here's the source for the parameterized test case;
package com.jda.portfolio.api.rest.server:
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Parameterized.class)
@SuiteClasses({PrimeNumberCheckerTest.class})
public class PrimeNumberCheckerTest {
private Integer inputNumber;
private Boolean expectedResult;
private PrimeNumberChecker primeNumberChecker;
@Before
public void initialize() {
primeNumberChecker = new PrimeNumberChecker();
}
// Each parameter should be placed as an argument here
// Every time runner triggers, it will pass the arguments
// from parameters we defined in primeNumbers() method
public PrimeNumberCheckerTest(Integer inputNumber,
Boolean expectedResult) {
this.inputNumber = inputNumber;
this.expectedResult = expectedResult;
}
@Parameterized.Parameters
public static Collection primeNumbers() {
return Arrays.asList(new Object[][] {
{ 2, true },
{ 6, false },
{ 19, true },
{ 22, false },
{ 23, true }
});
}
// This test will run five times since we have as many parameters defined
@Test
public void testPrimeNumberChecker() {
System.out.println("Parameterized Number is : " + inputNumber);
assertEquals(expectedResult,
primeNumberChecker.validate(inputNumber));
}
这篇关于如何参数化junit Test Suite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!