简体   繁体   English

如何通过maven分别运行集成测试和单元测试?

[英]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 请参阅以下链接 - GitHub讨论如何分离集成测试和单元测试

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. 意思是,surefire不执行集成测试,而Failsafe不执行单元测试。

But, when I run, mvn verify or mvn integration-test , the sure-fire plugin is also used. 但是,当我运行, mvn verifymvn integration-test ,也会使用sure-fire插件。


Required Outcome: When run mvn integration-test , Unit test shouldn't be run. 必需结果:运行mvn integration-test ,不应运行单元测试。


The below three images are for mvn verify 以下三张图片用于mvn verify

Integration Test: 整合测试:

集成测试

测试运行

Unit Tests: 单元测试:

单元测试

The below image is when I ran mvn test 下图是我运行mvn test

单元测试

Maven has a build lifecycle made up out of several phases. Maven的构建生命周期由几个阶段组成。 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 请参阅https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

There are two ways how you could solve what you want: 有两种方法可以解决你想要的问题:

Both goals verify and integration-test defined in maven-failsafe-plugin runs integration test cases with surefire. maven-failsafe-plugin定义的目标verifyintegration-test运行与surefire的集成测试用例。 Here things are working as expected and as per guideline provided. 这里的事情按预期工作,并按照指南提供。 pls refer this link for more details: 请参阅此链接以获取更多详细信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM