使用参数化测试的多个实例创建 JUnit 测试套件
我想从多个文本文件创建一个 TestSuite。每个文本文件应该是一个测试并包含该测试的参数。我创建了一个测试,如下所示:
@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);
}
}
现在我想创建一个多次运行此测试的套件。但因为 Parameterized、BeforeClass 和 AfterClass 都只运行一次,所以这似乎有点不可能。
所以,总结一下:
- 我想多次运行测试。
- 每次我需要一个输入参数(如文本文件的名称)时,
- 每次应调用 BeforeClass、AfterClass 和Parameters 函数时,
- 我宁愿不为每个文本文件创建一个子类。
这可能吗?
I want to create a TestSuite from multiple text files. Each textfile should be one Test and contains the parameters for that test. I have created a Test like:
@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);
}
}
Now I want to create a Suite which runs this test multiple times. But because the Parameterized, BeforeClass and AfterClass all runs only once, this seems a bit impossible.
So, to sum it up:
- I want to run a Test multiple times.
- Each time I need an input parameter (Like the name of a textfile)
- Each time the BeforeClass, AfterClass and Parameters functions should be called
- I rather not make a subclass for each text file.
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您可以使用 @Before 和 @After,但它们在每个测试之前或之后运行。如果您可以忍受它,请使用它们。如果您需要在所有测试方法之后执行该操作,我认为没有类似的事情。
您可以通过在 @After 方法中使用一些条件来模拟它吗?
I think you can use @Before and @After, but they run before or after each test. If you can live with it, use them. If you need to execute that after all test methods, I don't think there's anything like that.
Could you perhaps simulate it by using some conditionals in an @After method?