JUnit Categories Test
By:Roy.LiuLast updated:2019-08-17
In JUnit, you can organize the test cases into different categories, and run those categorized test cases with @Categories.ExcludeCategory or @Categories.IncludeCategory
Note
This @Categories annotation is available since JUnit 4.12
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:
PerformanceTests.java
package com.mkyong.category; //category marker interface public interface PerformanceTests {
RegressionTests.java
package com.mkyong.category; public interface RegressionTests {
2. @Category Examples
Organizing the test cases into different categories.
2.1 @Category on method level.
ClassA.java
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.
ClassB.java
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.
ClassC.java
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.
PerformanceTestSuite.java
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.
RegressionTestSuite.java
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.
ExcludePerformanceTestSuite.java
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()
Note
This is similar with the TestNG group test.
This is similar with the TestNG group test.
References
From:一号门
Previous:JUnit + Spring integration example
COMMENTS