JUnit 中是否有任何注释可以排除参数化测试类中的非参数测试?
JUnit 5
从 Junit 5.0.0 开始,您现在可以使用以下方式注释您的测试方法@ParameterizedTest
。所以不需要内部类。除了 ValueSource 之外,还有多种方法可以向参数化测试提供参数,如下所示。请参阅官方 junit 用户指南 http://junit.org/junit5/docs/current/user-guide/#writing-tests-parameterized-tests详情:
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 进行了测试),您可以将封闭运行器与内部类和参数化运行器结合使用:
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 {
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)