views:

376

answers:

2

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:

  1. I want to run a Test multiple times.
  2. Each time I need an input parameter (Like the name of a textfile)
  3. Each time the BeforeClass, AfterClass and Parameters functions should be called
  4. I rather not make a subclass for each text file.

Is this possible?

+1  A: 

Not sure if this helps, but have you looked into TestNG. It could be the right tool for this.

er4z0r
I have not taken a look at it, but I rather not want to introduce another test framework. If nothing else is possible I will take a look at it.
Nick Stolwijk
A: 

As far as I understand the Parameterized class, your parameters are the names of the text files. Or am I wrong? Perhaps you can elaborate your problem a bit more?

Frank Meißner
No, my parameters are the content of each file. For example,I have 2 files with 2 lines each.I want:- Run BeforeClass for file 1- Run Parameters to get a String[] { "File 1 - Line 1", "File 1 - Line 2"}- Run Constructor, Before, Tests, After for parameter 1- Run Constructor, Before, Tests, After for parameter 2- Run AfterClass for file 1- Repeat for file 2.I know how to read the file, but how to call one parametrized class multiple times with different file names?
Nick Stolwijk
Hm, I do not think that's possible. But why don't you put the filenames in your parameters? Are they not known at compile time? If your constructor gets the filenames, you can then read the contents of those.
Frank Meißner