【问题标题】:Unit tests getting executed during integration test phase even after excluding them即使在排除它们之后,单元测试也会在集成测试阶段执行
【发布时间】:2016-08-15 15:56:11
【问题描述】:

我的 maven 应用程序中有一个单元测试 (ProductDaoTest.java) 和一个集成测试 (ProductDaoIT.java)。

我想在mvn verify 命令调用期间只执行集成测试,但即使在使用maven-failsafe-plugin 配置中的<exclude> 标记将其排除后,单元测试也会执行。

我该如何解决这个问题?

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
            <excludes>
                <exclude>**/*Test.java</exclude>
            </excludes>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>integration-test</goal>
                    <goal>verify</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

更新的 POM(带有解决方案):

 <!-- For skipping unit tests execution during execution of IT's -->
    <profiles>
        <profile>
            <id>integration-test</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <version>2.19.1</version>
                        <!-- Skips UTs -->
                        <configuration>
                            <skipTests>true</skipTests>
                        </configuration>
                    </plugin>
                    <!-- Binding the verify goal with IT -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <version>2.19.1</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>integration-test</goal>
                                    <goal>verify</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.tomcat.maven</groupId>
                        <artifactId>tomcat7-maven-plugin</artifactId>
                        <version>2.2</version>
                        <configuration>
                            <port>5000</port>
                            <path>${project.artifactId}</path>
                        </configuration>
                        <executions>
                            <execution>
                                <id>start-tomcat</id>
                                <phase>pre-integration-test</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <fork>true</fork>
                                </configuration>
                            </execution>
                            <execution>
                                <id>stop-tomcat</id>
                                <phase>post-integration-test</phase>
                                <goals>
                                    <goal>shutdown</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>    
                </plugins>
            </build>
        </profile>
    </profiles>

mvn clean install - 默认只运行单元测试

mvn clean install -Pintegration-test - 默认只运行集成测试

【问题讨论】:

  • 在故障安全中排除单元测试没有意义,因为它具有命名架构:*IT.java,因此可以删除此配置。你可以使用mvn verify -Dmaven.test.skip=true ....

标签: java maven unit-testing integration-testing


【解决方案1】:

在 Maven 中,test 步骤在生命周期中的 verify 步骤之前。
所以如果你不跳过这一步,它一定会执行。

如果您想跳过测试,请按照 khmarbaise 的建议使用 -Dmaven.test.skip=tr‌​ue,或者为 IT 创建一个专用的 Maven 配置文件,您将在其中以这种方式忽略单元测试:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <configuration>
          <skipTests>true</skipTests>
        </configuration>
      </plugin>

通常,您会为集成测试创建一个 Maven 配置文件,因此如果是这种情况,将所有配置集中在一个地方比分散它更好。

【讨论】:

  • 终于能够解决使用配置文件的问题。用上面的答案更新了我的问题。
  • 良好的主动性。它可能对其他人有用。
猜你喜欢
  • 2012-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多