使用JUnit,您可以使用@RunWith(Parameterized.class)
提供一组参数传递给测试构造函数,然后对每个对象运行测试。
我正在尝试将尽可能多的测试逻辑转移到数据中,但是有些测试不会轻易转换为数据驱动的测试。有没有一种方法可以使用JUnit的Parameterized
运行器运行带有参数的某些测试,然后还添加对于每个测试对象构造都不会重复运行的非数据驱动的测试?
最佳答案
我的解决方法是创建一个单一的类,并将程序化和数据驱动的测试放在两个单独的子类中。子类必须是静态的,JUnit才能运行其测试。这是一个骨架:
@RunWith(Enclosed.class) // needed for working well with Ant
public class MyClassTests {
public static class Programmatic {
@Test
public void myTest(){
// test something here
}
}
@RunWith(Parameterized.class)
public static class DataDriven {
@Parameters
public static Collection<Object[]> getParams() {
return Collections.emptyList();
}
private String data;
public DataDriven(String testName, String data){
this.data = data;
}
@Test
public void test() throws AnalyzeExceptionEN{
// test data string here
}
}
}
关于java - 混合参数化和程序化单元测试,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33770185/