Reputation: 5737
I have some tests (assume Test1, Test2) under one suite AllTests, see the code:
@RunWith(Suite.class)
@SuiteClasses({ Test1.class, Test2.class})
public class AllTests {
public static void main(String[] args) {
for (int i = 0; i < 4; i++) {
Result result = JUnitCore.runClasses(AllTests.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
}
}
@BeforeClass
public static void runBeforeClass() {
System.out.println("BEFORE:");
}
@AfterClass
public static void runAfterClass() {
System.out.println("AFTER:");
}
}
When I put Result result = JUnitCore.runClasses(AllTests.class);
under loop the suite runs only one sequence [Test1, Test2].
Upvotes: 1
Views: 1147
Reputation: 5737
I found the answer to my question:
1. I just removed the main function from AllTests class and created another class MasterTest that is a JUnit suite of suites.
AllTest class:
@RunWith(Suite.class)
@SuiteClasses({ Test1.class, Test2.class})
public class AllTests {
// This class used for suiting the test classes
}
And I can run AllTests in MasterTest many times:
@RunWith(Suite.class)
@SuiteClasses({ AllTests.class, AllTests.class, AllTests.class})
public class MasterTest {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(AllTests.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
}
}
2. About parameters: the parametrized test doesn't good for me because throu one run of AllTests if I give for example to Test1 3 parameters I get the following sequence: [Test1, Test1, Test1, Test2], So I created a singletone class that contains all parameters and use a static counter to return different value for each call.
Upvotes: 0
Reputation:
the suite runs only one sequence [Test1, Test2].
If you look at Junit wiki https://github.com/junit-team/junit/wiki/Aggregating-tests-in-suites
// the class remains empty, used only as a holder for the above annotations
hence the code inside class is never executed and suite runs one sequence due to following line in your code
@SuiteClasses({ Test1.class, Test2.class})
In suite the tests are executed independently and so u can execute Test1 and Test2 one after another using for loop.
Regarding passing parameters to Test1 only.
I think its not possible in Junit. But if you want to test methods of Test1 for various values of input parameters, may be you can use parameterized constructors.
Upvotes: 2