为什么在调用“mvn test”命令时我的集成测试没有运行?

时间:2016-08-11 13:48:06

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

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

,如何运行集成测试?

2 个答案:

答案 0 :(得分:1)

实际上有特殊的阶段 用于运行集成测试:

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

他们按顺序运行,所以如果你打电话

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>

答案 1 :(得分:-1)

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

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