JUnit test suites help to grouping and executing tests in bulk. Executing tests separately for all test classes is not desired in most cases. Test suites help in achieving this grouping.
In JUnit, test suites can be created and executed with these annotations.
- @RunWith
- @SuiteClasses
Read More : JUnit 5 Test Suite
1. JUnit test suite example
1.1. Test classes
Given below are JUnit test classes.
package com.howtodoinjava.junit; import junit.framework.Assert; import org.junit.Test; public class TestFeatureOne { @Test public void testFirstFeature() { Assert.assertTrue(true); } }
package com.howtodoinjava.junit; import junit.framework.Assert; import org.junit.Test; public class TestFeatureTwo { @Test public void testSecondFeature() { Assert.assertTrue(true); } }
1.2. Create junit test suite
To run above features only, we can write a suite like this.
package com.howtodoinjava.junit.suite; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; import com.howtodoinjava.junit.TestFeatureOne; import com.howtodoinjava.junit.TestFeatureTwo; @RunWith(Suite.class) @SuiteClasses({ TestFeatureOne.class, TestFeatureTwo.class }) public class TestFeatureSuite { // }
1.3. Execute junit test suite
You can use JUnitCore
to run test suite from application code.
Result result = JUnitCore.runClasses(testCase); for (Failure failure : result.getFailures()) { System.out.println(failure.toString()); }
Happy Learning !!
Vivek
How to run test suite with maven sure fire plugin?
Fan
Nice setup, but what if I want to print out which Test Feature failed specifically. Using your solution, I get that TestFeatureSuite Failed and then it says which test case failed, for example @Test testSecondFeature failed, but it does not say that the testSecondFeature test case failed in TestSecondFeature class. What I am interested in is to know that TestSecondFeature class failed.
Lokesh Gupta
Will this help : https://howtodoinjava.com/junit/how-to-add-listner-in-junit-testcases/
Chindu
How can we add a condition before running the test cases?
That is, I have added say two test cases in @SuiteClasses and I want to run one of them only if a certain condition is met?
Lokesh Gupta
I do not find the concept of “run one of them only if” any good. Good written testcases should all be independent units. They should not be dependent on or reply on the result/output of any other testcase in application. Having dependent testcases mostly turnout to be maintenance nightmare in future.
If you want something to assert before executing a testcase, then use asserts. They are present for this purpose only.
Chindu
In Junit 3 we can add the test cases conditionally like below:
public class MyTestsuite extends TestCase
{
public static Test suite()
{
TestSuite suite = new TestSuite();
if(some_condition_true)
{
suite.addTest(A.suite);
}
suite.addTest(B.suite);
return suite;
}
}
I wanted the same conditional adding using @SuitClasses
Lokesh Gupta
Using annotations, you can’t do it.
Chindu
We can use Filter.shouldRun but that is a complex way of filtering
I am looking for a simple way.
Anyway thanks for replying Lokesh
Lokesh Gupta
Thanks for pointing out “Filter.shouldRun()”. It’s new concept for me. I will checkout it soon and share more on it.
raga saleh
Hi
is this mean that the tests will run in this order? (first test is TestFeatureOne and after it TestFeatureTwo)
my goal is to build a suite that execute the tests in order should this help me?
thanks
Lokesh Gupta
NO. it will not run the testcases in order. For maintaining the ordering, follow the information given here: https://howtodoinjava.com/junit/ordered-testcases-execution-in-junit-4/
Micheal
Hi Lokesh, nice article but i have a doubt can we execute TestFeatureOne.class, TestFeatureTwo.class in specific order?
Lokesh Gupta
Not sure if it may solve you need. https://howtodoinjava.com/junit/ordered-testcases-execution-in-junit-4/
Apart from this, testcases which needs certain ordering, are not considered good.
Read more: https://howtodoinjava.com/best-practices/unit-testing-best-practices-junit-reference-guide/