Long awaited, JUnit 4.8 introduced support for categorizing test cases.
A category marker is simply a class or interface, e.g.
Tests can be marked using the
The annotation works both on methods and classes:
Test suites that include or exclude the
SlowTests category are defined by specifying the
Categories runner and using the
@IncludeCategory annotation, respectively:
In this example,
AllFastTests would execute only
AllSlowTests would ignore
A.a but run
However, there is a major issue in the above suite declarations: they violate the DRY principle. Both test suites list all test classes in the
@SuiteClasses annotation. While it seems feasible to maintain the list of test classes at two locations for a small number of classes, it certainly is not a viable option in a real-world setting, especially when there are multiple categories.
Fortunately, there is a simple solution: use inheritance. You can define the list of test classes once in a normal test suite …
… and declare subclasses that filter the list of classes by category: