Close

JUnit - Using Suite runner to run a group of test classes together

[Last Updated: Apr 26, 2017]

The Suite class can be used as a runner to run multiple test classes together. Suite is a subclass of ParentRunner, that means we can use it with @RunWith. We are also required to use Suite.SuiteClasses annotation in the same class to specify the classes to be run when this test class is run.

Let's see an example to learn how to use it.

Example

@RunWith(Suite.class)
@Suite.SuiteClasses({MyTest2.class, MyTest3.class})
public class MyTest {

  @BeforeClass
  public static void beforeClass() {
      System.out.println("in MyTest#beforeClass");
  }

  @Before
  public void before() {
      System.out.println("in MyTest#before");
  }

  @Test
  public void testMethod1() {
      System.out.println("in MyTest#testMethod1");
  }

  @Test
  public void testMethod2() {
      System.out.println("in MyTest#testMethod2");
  }
}
public class MyTest2 {

  @BeforeClass
  public static void beforeClass() {
      System.out.println("in MyTest2#beforeClass");
  }

  @Before
  public void before() {
      System.out.println("in MyTest2#before");
  }

  @Test
  public void testMethod3() {
      System.out.println("in the MyTest2#testMethod3");
  }

  @Test
  public void testMethod4() {
      System.out.println("in the MyTest2#testMethod4");
  }
}
public class MyTest3 {

  @BeforeClass
  public static void beforeClass() {
      System.out.println("in MyTest3#beforeClass");
  }

  @Before
  public void before() {
      System.out.println("in MyTest3#before");
  }

  @Test
  public void testMethod5() {
      System.out.println("in the MyTest3#testMethod5");
  }

  @Test
  public void testMethod6() {
      System.out.println("in the MyTest3#testMethod6");
  }
}
mvn -q test -Dtest=MyTest

Output

d:\example-projects\junit\junit-suite-example>mvn -q test -Dtest=MyTest

-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.logicbig.example.MyTest
in MyTest#beforeClass
in MyTest2#beforeClass
in MyTest2#before
in the MyTest2#testMethod3
in MyTest2#before
in the MyTest2#testMethod4
in MyTest3#beforeClass
in MyTest3#before
in the MyTest3#testMethod5
in MyTest3#before
in the MyTest3#testMethod6
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.033 sec

Results :

Tests run: 4, Failures: 0, Errors: 0, Skipped: 0

Note that the class which is annotated with @RunWith(Suite.class) doesn't run any tests of its own. The suite class is meant to group multiple test classes into a single test class and it's not meant to run it's own tests, so we shouldn't add any test methods in it.

Example Project

Dependencies and Technologies Used:

  • junit 4.12: JUnit is a unit testing framework for Java, created by Erich Gamma and Kent Beck.
  • JDK 1.8
  • Maven 3.3.9

Junit Suie Example Select All Download
  • junit-suite-example
    • src
      • test
        • java
          • com
            • logicbig
              • example
                • MyTest.java

    See Also