mardi 7 mars 2017

Run Parameterized JUnit Test Suite on Loop for Duration

I have a test JUnit Suite that runs a set of Test Classes via the Suite Annotations. I need to run the Suite on a loop until a defined amount of time has elapsed (I get this length of time from config or a custom Duration annotation). The tests themselves make use of JUnitParams to provide data. How can I immediately rerun the suite after it finishes?

My initial attempt was to create a custom runner which has extends the JUnit Suite runner:

@RunWith(Suite.class)
@Suite.SuiteClasses({Suite1.class,Suite2.class})
public class RepeatingSuite extends Suite {

    // Other stuff...

    @Override
    public void run(RunNotifier notifier){
         /* hasDurationElapsed is a simple method 
          * comparing current time vs the start time 
          * which is set in the constructor */
         while (!hasDurationElapsed()) {
             super.run(notifier);
        }
    }
}

This succeeds on running the test on a loop, however it appears to conflict with JUnitParams for some reason because my tests use the @FileParameters annotation and the parameters are always null after the first iteration of the suite in the loop. I assume therefore it does not restart at the beginning of the csv data file for each iterations of the suite, but actually just continues from the place it left off (though I'm not sure how this is actually possible...).

Is there a simpler way than creating a custom test runner to achieve this? Or am I making an obvious mistake in my test runner? The method I've used appears to work in principle, however it doesn't play well with JUnit Params.

I realise this could be done via an external script very easily however I unfortunately have to do it inside the "test framework".

Aucun commentaire:

Enregistrer un commentaire