How can I switch between two test suites in Maven 2?

We use the maven-surefire-plugin to run our Java tests. Tests are divided into two categories:

  • Quick tests
  • Slow tests

All "fast" dialing is done in a couple of seconds, and slow tests take half an hour.

During development, I want to run only quick tests. When I make transactions, I would also like to run slow tests, so running slow tests should be an option, while fast tests should be the default.

On the CI server, I want to run both.

This is normal (and even preferable) when slow tests include fast ones.

How do I configure Maven, JUnit, and Surefire for this scenario?

+4
source share
2 answers

In a commercial project that I did from scratch on my own, I divided the tests into a unit (which I called *Test.java ) and integration ( *IT.java ), in accordance with the policies of the Surefire and Failsafe plugin that I used to run the tests. Of course, they work much slower than UT.

This makes it possible to run a group of tests using simple commands: mvn test for UT and mvn integration-test for UT and IT, as well as the ability to skip only IT using mvn install -DskipITs .

Another good thing is the ability to be weaker with the results of integration tests, because they fail more often than unit tests due to environmental problems (i.e. the database starts too long, the message browser starts too soon and soon ) By default, a Failsafe test failure does not kill the assembly unless you explicitly specify "verify":

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <version>2.6</version> <executions> <execution> <id>integration-test</id> <goals> <goal>integration-test</goal> </goals> </execution> <!-- Uncomment this in order to fail the build if any integration test fail --> <!-- execution> <id>verify</id> <goals><goal>verify</goal></goals> </execution --> </executions> </plugin> 
+1
source

You can use Category from junit: Junit category

First decision

Configure maven-surefire-plugin version at least 2.11

  <profile> <id>normal</id> <activation> <activeByDefault>true</activeByDefault> </activation> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <excludedGroups>com.test.SlowTests</excludedGroups> </configuration> </plugin> </plugins> </build> </profile> 

Second solution

In the configuration section, you can add a regular expression with files to support only classes (default setting):

  <configuration> <includes> <include>**/*Test*.java</include> <include>**/*Test.java</include> <include>**/*TestCase.java</include> </includes> </configuration> 
+3
source

Source: https://habr.com/ru/post/1399382/


All Articles