为什么我的集成在调用 "mvn test" 命令时不测试 运行?

Why don't my integration tests run while invoking the "mvn test" command?

目前我的集成测试仅在 运行 运行 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 时,如何进行集成测试 运行?

与单元测试不同,集成测试默认不执行。 您必须在 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

中创建的配置文件名称

其实还有特殊的阶段 对于 运行ning 集成测试:

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

他们 运行 按顺序,所以如果你调用

mvn integration-test

它失败了,post-集成测试阶段将不会被调用。

但是如果你想在 "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>