【问题标题】:mvn integration-test vs mvn failsafe:integration-testmvn 集成测试与 mvn 故障安全:集成测试
【发布时间】:2012-08-21 15:17:06
【问题描述】:

我在使用 maven 故障安全插件运行集成测试时遇到了问题。 我有两个类,一个是 TestUnitTest.java,另一个是 TestIntegrationIT.java。 在 pom.xml 中,我配置如下:

<plugin>                
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <excludes>
            <exclude>**/*IT.java</exclude>
        </excludes>
    </configuration>
    <executions>
        <execution>
            <id>unit-tests</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <configuration>
        <includes>
            <include>**/*IT.java</include>
        </includes>
    </configuration>                    
    <executions>
        <execution>
            <id>integration-test</id>
            <phase>integration-test</phase>
            <goals>
                <goal>integration-test</goal>                           
            </goals>
        </execution>
    </executions>
</plugin>

当我运行 mvn:integration-test 时,将执行两个测试,当我运行 mvn failsafe:integration-test 时,只运行“TestIntegrationIT”。为什么输出不同的结果?

【问题讨论】:

    标签: java maven maven-failsafe-plugin


    【解决方案1】:

    maven-surefire-plugin 的包含定义为 *Test、*TestCase,而 maven-failsafe-plugin 定义为 IT.java、IT.java 或 *ITCase.java。所以你不需要为 maven-surefire-plugin 或 maven-failsafe-plugin 定义包含使用默认值。如果您不想命名集成测试,只需将其命名为 NameIT.java,而单元测试可以命名为 NameTest.java。 要运行单元测试和/或集成测试,您应该使用生命周期:

    mvn package
    

    它将运行单元测试,而

    mvn verify
    

    将运行单元测试和集成测试。

    【讨论】:

    • 我知道原因与您提到的不同。由于 Maven 有默认的生命周期,当运行 mvn integration-test 时,这句话会先运行 mvn test。所以需要添加自定义的maven生命周期来实现。
    • 您应该运行 mvn verfiy 而不是 mvn integration-test,因为集成后阶段不会运行。此外,我建议根据规则命名您的测试并检查 maven-surefire-plugin 的文档,因为它有可能跳过测试。
    猜你喜欢
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    • 2015-03-09
    • 2017-12-12
    • 2018-03-29
    • 1970-01-01
    相关资源
    最近更新 更多