In JUnit, you can organize the test cases into different categories, and run those categorized test cases with @Categories.ExcludeCategory or @Categories.IncludeCategory
This
@Categories annotation is available since JUnit 4.12
1. Category = Marker Interface
In JUnit, you need to create marker interfaces to represent the categories:
package com.mkyong.category;
//category marker interface
public interface PerformanceTests {
}
package com.mkyong.category;
public interface RegressionTests {
}
2. @Category Examples
Organizing the test cases into different categories.
2.1 @Category on method level.
package com.mkyong.category;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public class ClassA {
@Category(PerformanceTests.class)
@Test
public void test_a_1() {
assertThat(1 == 1, is(true));
}
@Test
public void test_a_2() {
assertThat(1 == 1, is(true));
}
}
2.2 @Category on class level.
package com.mkyong.category;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
@Category({PerformanceTests.class, RegressionTests.class})
public class ClassB {
@Test
public void test_b_1() {
assertThat(1 == 1, is(true));
}
}
2.3 Multiple @Category examples.
package com.mkyong.category;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public class ClassC {
@Category({PerformanceTests.class, RegressionTests.class})
@Test
public void test_c_1() {
assertThat(1 == 1, is(true));
}
@Category(RegressionTests.class)
@Test
public void test_c_2() {
assertThat(1 == 1, is(true));
}
}
3. Suite Test
Examples to run the categorized test cases.
3.1 Include category example, run PerformanceTests category.
package com.mkyong.category;
import org.junit.experimental.categories.Categories;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Categories.class)
@Categories.IncludeCategory(PerformanceTests.class)
//Include multiple categories
//@Categories.IncludeCategory({PerformanceTests.class, RegressionTests.class})
@Suite.SuiteClasses({ClassA.class, ClassB.class, ClassC.class})
public class PerformanceTestSuite {
}
Output
ClassA.test_a_1() ClassB.test_b_1() ClassC.test_c_1()
3.2 Include category example, run RegressionTestSuite category.
package com.mkyong.category;
import org.junit.experimental.categories.Categories;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Categories.class)
@Categories.IncludeCategory(RegressionTests.class)
@Suite.SuiteClasses({ClassA.class, ClassB.class, ClassC.class})
public class RegressionTestSuite {
}
Output
ClassB.test_b_1() ClassC.test_c_1() ClassC.test_c_2()
3.3 Exclude category example.
package com.mkyong.category;
import org.junit.experimental.categories.Categories;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Categories.class)
@Categories.ExcludeCategory(PerformanceTests.class)
@Suite.SuiteClasses({ClassA.class, ClassB.class, ClassC.class})
public class ExcludePerformanceTestSuite {
}
Output
ClassA.test_a_2() ClassC.test_c_2()
This is similar with the TestNG group test.
when you combine running categories – is it possible to specify the order that the tests are run in ? Using your example, if Class A and Class B have been marked as PerformanceTests and Class C and Class D are marked as Regression Tests – and Class A and B have to be run before Class C and D – is it possible to create an overall TestSuite to specify the order something like, run all tests marked as PerformanceTestsSuite first and then those marked as RegressionTestsSuite second ?
Hi Mykong. You have a mistake in section 3 for sub section 3.1. The output for classC -> test_c_2 is not visible even though it is marked with the category RegressionTests.class. Can that be correct?
Worked great, thanks!
How do you select which category of tests to run?
See RegressionTestSuite.java and ExcludePerformanceTestSuite.java
But I like to know: Is this possible with maven?
this is very cool. thanks!