如何忽略Maven构建中特定的失败单元测试结果?

时间:2020-07-21 14:07:08

标签: java maven testng maven-surefire-plugin

我在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

以外的任何其他测试类下失败,则构建应该会失败

2 个答案:

答案 0 :(得分: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,我确保测试用例能够正常运行并确保良好的代码覆盖率,即使上游关闭,构建也不会失败。

答案 1 :(得分:0)

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

您可以根据需要使用excludes和include参数:

<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>