A TestSuite contains a list of of Tests.
Following is the declaration for org.junit.TestSuite class:
public class TestSuite extends Object implements Test
A TestSuite is a Composite of Tests. It runs a collection of test cases.
The following list has some frequently used methods from TestSuite class.
The following code shows how to use the TestSuite Class
import junit.framework.*; public class Main { public static void main(String[] a) { // add the test's in the suite TestSuite suite = new TestSuite(TestJunit1.class, TestJunit2.class, TestJunit3.class ); TestResult result = new TestResult(); suite.run(result); System.out.println("Number of test cases = " + result.runCount()); } }
The following code shows how to use annotation to create Test Suite.
Create a java class and attach @RunWith(Suite.class) Annotation with class.
Add reference to Junit test classes using @Suite.SuiteClasses annotation
import org.junit.runner.RunWith; import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({ TestJunit1.class, TestJunit2.class }) public class JunitTestSuite { }
Here is the code to run the Test Suite above.
import org.junit.runner.JUnitCore; import org.junit.runner.Result; import org.junit.runner.notification.Failure; public class TestRunner { public static void main(String[] args) { Result result = JUnitCore.runClasses(JunitTestSuite.class); for (Failure failure : result.getFailures()) { System.out.println(failure.toString()); } System.out.println(result.wasSuccessful()); } }