How to run JUnit tests by category in Maven?

Using JUnit 4.8 and the new @Category annotations, is there a way to choose a subset of categories to run with the Maven Surefire plugin?

For example, I have:

 @Test public void a() { } @Category(SlowTests.class) @Test public void b() { } 

And I would like to run all the slow tests, as in: (note that -Dtest.categories was compiled by me ...).

 mvn test -Dtest.categories=!SlowTests // run non-slow tests mvn test -Dtest.categories=SlowTests // run only slow tests mvn test -Dtest.categories=SlowTests,FastTests // run only slow tests and fast tests mvn test // run all tests, including non-categorized 

So the thing is, I donโ€™t want to create test suites (Maven just selects all the unit tests in the project, which is very convenient), and I would like Maven to be able to select tests by categories. I think I just compiled -Dtest.categories, so I was wondering if there is a similar tool that I can use?

+63
java maven junit maven-surefire-plugin categories
Jun 23 '10 at
source share
6 answers

Maven has since been updated and can use categories.

Example from 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 @Category(com.mycompany.SlowTests.class) annotation @Category(com.mycompany.SlowTests.class)

+72
Feb 07 '12 at 15:03
source share

Based on this blog post - and simplify it - 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 on the command line

 mvn install -P SlowTests mvn install -P FastTests mvn install -P FastTests,SlowTests 
+43
Aug 18 '13 at 9:14
source share

I had a similar case when I want to run all EXCEPT tests for a certain category (for example, because I have hundreds of outdated tests without a heading, and I cannot / do not want to change each of them)

The maven surefire plugin allows you to exclude categories, for example:

 <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> 
+18
03 Sep '14 at 8:10
source share

You can use

 mvn test -Dgroups="com.myapp.FastTests, com.myapp.SlowTests" 

But make sure you configure the maven surefire plugin correctly

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.11</version> <dependencies> <dependency> <groupId>org.apache.maven.surefire</groupId> <artifactId>surefire-junit47</artifactId> <version>2.12.2</version> </dependency> </dependencies> </plugin> 

See Docs at: https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html.

+8
Feb 17 '14 at 13:51
source share

I lost a lot of time on this error "groups / excludedGroups require TestNG or JUnit48 + in the path to the test project path" because I thought I was using a bad version of junit or a bad version of the surefire plugin or a combination that does not work.

It wasnโ€™t: in my project I had a โ€œconfigโ€ module that was created before the module I wanted to test. This module did not have junit dependencies -> it did not have junit on the way to classes ...

This error may help others ...

+4
May 19 '15 at 12:17
source share

Not exactly the same, but using the surefire plugin, test classes can be selected based on the file name. You are not using Junit categories.

An example to run only DAO tests.

 <executions> <execution> <id>test-dao</id> <phase>test</phase> <goals> <goal>test</goal> </goals> <configuration> <excludes> <exclude>none</exclude> </excludes> <includes> <include>**/com/proy/core/dao/**/*Test.java</include> </includes> </configuration> </execution> 

http://maven.apache.org/plugins/maven-surefire-plugin/examples/inclusion-exclusion.html

+1
Mar 30 '11 at 13:59
source share



All Articles