How to run JUnit tests by category in Maven?
Maven has since been updated and can use categories.
An example from the Surefire documentation:
<plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.11</version> <configuration> <groups>com.mycompany.SlowTests</groups> </configuration></plugin>
This will run any class with the annotation @Category(com.mycompany.SlowTests.class)
Based on this blog post - and simplifying - add this to your pom.xml:
<profiles> <profile> <id>SlowTests</id> <properties> <testcase.groups>com.example.SlowTests</testcase.groups> </properties> </profile> <profile> <id>FastTests</id> <properties> <testcase.groups>com.example.FastTests</testcase.groups> </properties> </profile></profiles><build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.13</version> <dependencies> <dependency> <groupId>org.apache.maven.surefire</groupId> <artifactId>surefire-junit47</artifactId> <version>2.13</version> </dependency> </dependencies> <configuration> <groups>${testcase.groups}</groups> </configuration> </plugin> </plugins></build>
then at the command line
mvn install -P SlowTestsmvn install -P FastTestsmvn install -P FastTests,SlowTests
I had a similar case where I want to run all test EXCEPT a given category (for instance, because I have hundreds of legacy uncategorized tests, and I can't / don't want to modify each of them)
The maven surefire plugin allows to exclude categories, for instance:
<profiles> <profile> <id>NonSlowTests</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <excludedGroups>my.category.SlowTest</excludedGroups> </configuration> </plugin> </plugins> </build> </profile></profiles>