Creating a JUnit testuite with multiple instances of the Parameterized Test

I want to create TestSuite from several text files. Each text file must be one test and contain parameters for this test. I 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 that runs this test several times. But since Parameterized, BeforeClass, and AfterClass are only executed once, this seems a little impossible.

So to summarize:

  • I want to run the test several times.
  • Every time I need an input parameter (As the name of a text file)
  • Each time the BeforeClass, AfterClass, and Parameters functions should be called
  • I prefer not to subclass for each text file.

Is it possible?

+1
source share
1 answer

I think you can use @Before and @After, but they work before or after each test . If you can live with him, use them. If you need to do this after all the testing methods, I don't think there is anything like that.

Perhaps you could simulate it using some conditional expressions in the @After method?

0
source

Source: https://habr.com/ru/post/1386709/


All Articles