Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make integration tests and unit tests run separately through maven?

Refer following links - GitHub discussion on how to separate Integration Tests and Unit Tests

As a result, I tried this --

     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <includes>
            <include>**/*Tests.java</include>
            <include>**/*Test.java</include>
          </includes>
          <excludes>
            <exclude>**/Abstract*.java</exclude>
            <exclude>**/IT*.java</exclude>
            <exclude>**/*IT.java</exclude>
            <exclude>**/*ITCase.java</exclude>
            <exclude>**/*IntegrationTest.java</exclude>
          </excludes>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>integration-test</goal>
              <goal>verify</goal>
            </goals>
            <configuration>
              <includes>
                <include>**/IT*.java</include>
                <include>**/*IT.java</include>
                <include>**/*ITCase.java</include>
                <include>**/*IntegrationTest.java</include>
              </includes>
            </configuration>
          </execution>
        </executions>
      </plugin>

This is working good to some extent. Meaning, surefire doesn't execute Integration tests and Failsafe doesn't execute unit tests.

But, when I run, mvn verify or mvn integration-test, the sure-fire plugin is also used.


Required Outcome: When run mvn integration-test, Unit test shouldn't be run.


The below three images are for mvn verify

Integration Test:

Integration Test

Tests Run

Unit Tests:

Unit Test

The below image is when I ran mvn test

Unit Tests

like image 859
Tarun Maganti Avatar asked Apr 18 '17 10:04

Tarun Maganti


People also ask

Does mvn test run integration tests?

The Maven build lifecycle now includes the "integration-test" phase for running integration tests, which are run separately from the unit tests run during the "test" phase. It runs after "package", so if you run "mvn verify", "mvn install", or "mvn deploy", integration tests will be run along the way.

How do I exclude test cases in Maven?

To skip running the tests for a particular project, set the skipTests property to true. You can also skip the tests via the command line by executing the following command: mvn install -DskipTests.

Should integration-test be in a separate project?

The integration tests for the components of a single project should be within the project. The tests testing the integration of multiple projects with each other should be a separate project.


2 Answers

Maven has a build lifecycle made up out of several phases. When you call a particular one, all phases before that one will be executed first. See https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

There are two ways how you could solve what you want:

  • use -DskipTests or -Dmaven.test.skip=true (https://www.mkyong.com/maven/how-to-skip-maven-unit-test/)
  • call the plugin goal directly mvn clean test-compile failsafe:integration-test
like image 126
ipper Avatar answered Nov 12 '22 21:11

ipper


Both goals verify and integration-test defined in maven-failsafe-plugin runs integration test cases with surefire. Here things are working as expected and as per guideline provided. pls refer this link for more details:

like image 23
Arun Avatar answered Nov 12 '22 21:11

Arun