问题描述
JUnit中是否有任何注释可以排除参数化测试类中的非参数测试?
Is there any annotation in JUnit to exclude a non param test in parameterized test class?
推荐答案
JUnit 5
从Junit 5.0.0开始,您现在可以使用@ParameterizedTest
注释测试方法.因此,不需要内部类.除了ValueSource之外,还有很多方法可以为参数化测试提供参数,如下所示.有关详细信息,请参见官方junit用户指南. :
JUnit 5
As of Junit 5.0.0 you can now annotate your test methods with @ParameterizedTest
. So no need for inner classes. There are many ways to supply the arguments to the parameterized test apart from ValueSource as shown below. See the official junit user guide for details:
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
public class ComponentTest {
@ParameterizedTest
@ValueSource(strings = { "racecar", "radar", "able was I ere I saw elba" })
public void testCaseUsingParams(String candidate) throws Exception {
}
@Test
public void testCaseWithoutParams() throws Exception {
}
}
JUnit 4
如果您仍在使用Junit 4(我在v4.8.2上进行了测试),则可以将封闭式流道与内部类和参数化流道一起使用:
JUnit 4
If you are still using Junit 4 (I tested with v4.8.2) you can use the Enclosed runner in conjunction with inner classes and the Parameterized runner:
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
@RunWith(Enclosed.class)
public class ComponentTest {
@RunWith(Parameterized.class)
public static class ComponentParamTests {
@Parameters
...
@Test
public void testCaseUsingParams() throws Exception {
}
}
public static class ComponentSingleTests {
@Test
public void testCaseWithoutParams() throws Exception {
}
}
}
这篇关于排除参数化测试类中的非参数测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!