Java 使用参数化测试的多个实例创建JUnit testsuite
我想从多个文本文件创建一个TestSuite。每个文本文件应该是一个测试,并包含该测试的参数。我创建了一个测试,如:Java 使用参数化测试的多个实例创建JUnit testsuite,java,unit-testing,junit,junit4,Java,Unit Testing,Junit,Junit4,我想从多个文本文件创建一个TestSuite。每个文本文件应该是一个测试,并包含该测试的参数。我创建了一个测试,如: @RunWith(Parameterized.class) public class SimpleTest { private static String testId = "TestCase 1"; private final String parameter; @BeforeClass public static void beforeClass() {
@RunWith(Parameterized.class)
public class SimpleTest {
private static String testId = "TestCase 1";
private final String parameter;
@BeforeClass
public static void beforeClass() {
System.out.println("Before class " + testId);
}
@AfterClass
public static void afterClass() {
System.out.println("After class " + testId);
}
@Before
public void beforeTest() {
System.out.println("Before test for " + testId + ":" + parameter);
}
@After
public void afterTest() {
System.out.println("After test for " + testId + ":" + parameter);
}
@Parameters
public static Collection<String[]> getParameters() {
//Normally, read text file here.
return Lists.newArrayList(new String[] { "Testrun 1" }, new String[] { "Testrun 2" });
}
public SimpleTest(final String parameter) {
this.parameter = parameter;
}
@Test
public void simpleTest() {
System.out.println("Simple test for " + testId + ":" + parameter);
}
@Test
public void anotherSimpleTest() {
System.out.println("Another simple test for " + testId + ":" + parameter);
}
}
现在我想创建一个套件,它可以多次运行这个测试。但是由于参数化的BeforeClass和AfterClass只运行一次,这似乎有点不可能
总而言之:
我想运行一个测试多次。
每次我都需要一个输入参数,比如一个文本文件的名称
每次都应调用BeforeClass、AfterClass和Parameters函数
我宁愿不为每个文本文件创建一个子类。
这可能吗?我认为可以使用@Before和@After,但它们在每次测试之前或之后运行。如果你能接受它,就使用它们。如果你需要在所有的测试方法之后执行它,我认为没有类似的东西 您可以在@After方法中使用一些条件来模拟它吗