【问题标题】:Why don't my integration tests run while invoking the "mvn test" command?为什么我的集成测试在调用“mvn test”命令时不运行?
【发布时间】:2016-08-11 13:48:05
【问题描述】:

目前我的集成测试仅在我运行mvn install 时运行。我想让它们在我做mvn test时运行。

我的<pluginManagement> 部分包含:

<pluginManagement>
    <plugins>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.18.1</version>
            <executions>
                <execution>
                    <id>integration-test</id>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    ...
</pluginManagement>

当我只给出目标test时,如何让集成测试运行?

【问题讨论】:

  • 为什么?它们在integration-test 阶段调用,如果需要,您可以调用mvn verify
  • (修正了这个问题的标题)。我想知道这是否可能。以及如何。
  • 我的问题还是一样:为什么您希望在运行mvn test 时调用您的 IT?它们通常在integration-test 阶段调用,您通常希望调用mvn verify 来运行IT,以便运行post-integration-test 阶段中​​的东西。
  • 如果想避免安装但仍执行本地测试,应该养成运行mvn verify 而不是maven testmvn package 的习惯。

标签: maven integration-testing maven-failsafe-plugin


【解决方案1】:

其实有特殊阶段 用于运行集成测试:

  • 预集成测试 - 配置测试环境。
  • 集成测试 - 运行测试。
  • post-integration-test - 停止集成测试环境。
  • 验证 - 检查结果。

它们是按顺序运行的,所以如果你调用

mvn integration-test

如果失败,则不会调用集成后测试阶段。

但如果你想在“测试”阶段调用它,只需将测试移动到适当的阶段:

    <plugins>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.18.1</version>
            <executions>
                <execution>
                    <id>integration-test</id>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                <phase>test</phase>
                </execution>
            </executions>
        </plugin>
    </plugins>

【讨论】:

    【解决方案2】:

    与单元测试不同,默认情况下不执行集成测试。 您必须在 pom.xml 中使用集成测试相关配置创建一个单独的配置文件,并在指定目标时使用此配置文件。 例如:

    <profiles>
        <profile>
            <id>integration-test</id>     // name of the profile
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>build-helper-maven-plugin</artifactId>
                        <version>1.9.1</version>
                        <executions>
                            <execution>
                                <id>add-integration-test-source</id>
                                <phase>generate-test-sources</phase>
                                <goals>
                                    <goal>add-test-source</goal>
                                </goals>
                                <configuration>
                                    <sources>
                                        <source>src/integration/java</source>     // location where your code related to integration tests are present
                                    </sources>
                                </configuration>
                            </execution>
                            <execution>
                                <id>add-integration-test-resources</id>
                                <phase>generate-test-resources</phase>
                                <goals>
                                    <goal>add-test-resource</goal>
                                </goals>
                                <configuration>
                                    <resources>
                                        <resource>
                                            <directory>src/integration/resources</directory>       // location where resources related to your integration tests are present
                                        </resource>
                                    </resources>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <version>2.19</version>
                        <configuration>
                            <failIfNoTests>false</failIfNoTests>
                            <redirectTestOutputToFile>true</redirectTestOutputToFile>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>integration-test</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    

    现在为了运行集成测试,您可以发出 maven 命令,如下所示:

    mvn clean install -P 集成测试

    这里 -P 选项用于指定配置文件,integration-test 是我们之前在 pom.xml 中创建的配置文件名称

    【讨论】:

    • 分析繁重的集成测试是一个很好的技巧,但如果它们很快,我会坚持在验证阶段之前无条件地运行它们。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-16
    • 2021-03-04
    • 1970-01-01
    • 2013-06-29
    • 1970-01-01
    • 2020-06-08
    • 1970-01-01
    相关资源
    最近更新 更多