使用参数化测试的多个实例创建JUnit测试套件 [英] Creating a JUnit testsuite with multiple instances of a Parameterized test

查看:74
本文介绍了使用参数化测试的多个实例创建JUnit测试套件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从多个文本文件创建一个TestSuite.每个文本文件应该是一个测试,并包含该测试的参数.我创建了一个测试,例如:

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);
  }
}

现在,我想创建一个套件,该套件可以多次运行此测试.但是由于Parameterized,BeforeClass和AfterClass都只运行一次,所以这似乎有点不可能.

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.

所以,总结一下:

  1. 我想多次运行测试.
  2. 每次我都需要一个输入参数(例如文本文件的名称)
  3. 每次都应调用BeforeClass,AfterClass和Parameters函数
  4. 我宁愿不为每个文本文件创建一个子类.

这可能吗?

推荐答案

我认为您可以使用@Before和@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.

您也许可以通过在@After方法中使用一些条件条件来模拟它?

Could you perhaps simulate it by using some conditionals in an @After method?

这篇关于使用参数化测试的多个实例创建JUnit测试套件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆