java - How to run unit tests in excludedGroups in Maven -


i have junit 4.12 slowtests test suite want exclude execution unless requested on maven command line.

i have added following pom file:

<plugin>     <groupid>org.apache.maven.plugins</groupid>     <artifactid>maven-surefire-plugin</artifactid>     <version>2.19</version>     <configuration>         <excludedgroups>com.example.slowtests</excludedgroups>         <includes>             <include>**/*testsuite.class</include>         </includes>         <excludes>             <exclude></exclude>         </excludes>     </configuration> </plugin> 

i have defined category slowtests , applied myslowtests class.

i have annotated test suite follows:

@runwith(categories.class) @includecategory(slowtests.class) @suiteclasses({ myslowtests.class }) public class myslowtestsuite 

when run mvn package unit tests except myslowtests executed.

however, looking @ various answers such https://stackoverflow.com/a/25639372/820657 , https://stackoverflow.com/a/21830866/820657 expected following command:

mvn package -dgroups=com.example.myslowtests 

to run excluded myslowtests tests don't run. in fact no tests run.

what doing wrong?

the maven surefire plugin has issues w.r.t categories in versions < 2.13 (iirc) long using surefire >= 2.13 following run classes annotated @category(com.yourcompany.slowtests.class):

<plugin>       <artifactid>maven-surefire-plugin</artifactid>       <version>2.13</version>       <configuration>         <groups>com.yourcompany.slowtests</groups>       </configuration> </plugin> 

this approach used profiles, following configuration ...

<profiles>     <profile>         <id>slow</id>         <properties>             <testcategories>com.yourcompany.slowtests</testcategories>         </properties>     </profile>     <profile>         <id>fast</id>         <properties>             <testcategories>com.yourcompany.fasttests</testcategories>         </properties>     </profile> </profiles>  <build>     <plugins>         <plugin>             <groupid>org.apache.maven.plugins</groupid>             <artifactid>maven-surefire-plugin</artifactid>             <version>2.13</version>             <configuration>                 <groups>${testcategories}</groups>             </configuration>         </plugin>     </plugins> </build> 

... can used run:

  • mvn install -p slow: runs slow tests only
  • mvn install -p fast: runs fast tests only
  • mvn install -p fast,slow: runs fast , slow tests

update 1: question: "is there way use approach can run fast tests default?"

you can define 2 properties:

<properties>     <includedcategories></includedcategories>     <excludedcategories>com.yourcompany.slowtests</excludedcategories> </properties> 

then update surefire plugin definition so:

    <plugin>         <artifactid>maven-surefire-plugin</artifactid>         <version>2.13</version>         <configuration>             <groups>${includedcategories}</groups>             <excludedgroups>${excludedcategories}</excludedgroups>         </configuration>     </plugin> 

and, finally, add profile:

    <profile>         <id>slow</id>         <properties>             <includedcategories>com.yourcompany.slowtests</includedcategories>             <excludedcategories></excludedcategories>         </properties>     </profile> 

this toggles includedcategories , excludedcategories properties. default, include except tests annotated com.yourcompany.slowtests (i.e. except 'slow' tests). when run -p slow exclude except tests annotated com.yourcompany.slowtests (i.e. except 'slow' tests).

note: said in original answer surefire versions < 2.13 misbehaving categories still stands make work need using version of maven surefire plugin >= 2.13.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -