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 !!