Aggregating JUnit Tests in Test Suites
Using Suite
as a runner allows you to build a suite containing tests. To create a suite, annotate a class with @RunWith(Suite.class)
. To add test classes to that suite, annotate the same class and add @SuiteClasses(TestClass.class, ...)
. If this suite is executed, it automatically executes all the registered test classes.
The @RunWith(Suite.class)
annotation registeres the Suite
class as a test runner. Aggregating JUnit tests is done by annotating the class with @Suite.SuitClasses
. All classes will be executed in the same suite. Note that if you register the same class multiple times, then this class is run multiple times.
package com.memorynotfound.test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
TestFeatureA.class,
TestFeatureA.class,
TestFeatureB.class
})
public class FeatureTestSuite {
}
A simple feature test a.
package com.memorynotfound.test;
import org.junit.Test;
public class TestFeatureA {
@Test
public void featureATest(){
System.out.println("testing feature a");
}
}
A simple feature test b.
package com.memorynotfound.test;
import org.junit.Test;
public class TestFeatureB {
@Test
public void featureBTest(){
System.out.println("testing feature b");
}
}
When you run the FeatureTestSuite
the registered test classes are run. Because we registered the TestFeatureA
class two times, it is executed also two times.
testing feature a
testing feature a
testing feature b