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

房东的猫 提交于 2020-12-01 07:10:09

问题


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:

Unit Tests:

The below image is when I ran mvn test


回答1:


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



回答2:


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:



来源:https://stackoverflow.com/questions/43469938/how-to-make-integration-tests-and-unit-tests-run-separately-through-maven

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!