【问题标题】:How to ignore the specific failed unit test result in Maven build?如何忽略 Maven 构建中特定的失败单元测试结果?
【发布时间】:2020-07-21 14:07:08
【问题描述】:

我在Integration.java 下进行了一些单元测试。我希望 Maven 忽略此类的测试结果,因为它们很少失败(由于外部服务器维护)。但我不想忽略它们运行,因为我需要它们来进行代码覆盖。

我试过这个配置

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <includes>
            <include>IntegrationTest.java</include>
        </includes>
        <testFailureIgnore>true</testFailureIgnore>
    </configuration>
</plugin>

这似乎通过忽略失败的测试来工作。但是上述配置的问题是,它也忽略了其他类的失败测试。

我希望Integration.java 下的所有测试都需要执行,但它不应该对确定成功/失败的构建产生任何影响。 但是,如果任何测试用例在 Integration.java 以外的任何其他测试类下失败,则构建应该会失败

【问题讨论】:

  • 使用 JUnit,您可以使用 @BeforeAll 方法中的假设来验证服务器连接并有条件地执行测试。如果服务器不可用,则不会执行测试。
  • 第一件事是集成测试应该由 maven failsafe 插件而不是 maven surefire 插件处理...另一方面,maven failsafe 插件有一个目标verify 如果测试失败修复测试直到工作,否则它们是无用的。
  • @khmarbaise 我不知道 maven-failsafe-plugin,这可能会有所帮助。谢谢

标签: java maven testng maven-surefire-plugin


【解决方案1】:

就像@khmarbaise提到的,它需要由maven-failsafe而不是surefire plugin处理

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

通过使用maven-failsafe-plugin,我确保测试用例运行并获得良好的代码覆盖率,即使上游关闭,构建也不会失败。

【讨论】:

    【解决方案2】:

    Surefire 默认包含名称以 Test 开头/以 Test/Tests/TestCase 结尾的所有测试类。

    您可以根据需要使用排除和包含参数:

    <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.21.0</version>
        <configuration>
            <excludes>
                <exclude>DataTest.java</exclude>
            </excludes>
            <includes>
                <include>DataCheck.java</include>
            </includes>
        </configuration>
    </plugin>
    

    【讨论】:

    • 即使测试用例在其他类中失败,构建也会成功 例如:DataCheck.java
    • 这不是你想要的吗?
    • 我希望 DataTest.java 下的所有测试都需要执行,但它不应该对确定成功/失败的构建产生任何影响。但是,如果任何测试用例在 DataCheck.java 或 DataTest.java 以外的任何其他测试类下失败,则构建应该会失败
    猜你喜欢
    • 1970-01-01
    • 2015-04-25
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    相关资源
    最近更新 更多