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

huangapple go评论88阅读模式
英文:

How to ignore the specific failed unit test result in Maven build?

问题

我有一些位于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之外的任何其他测试类中的测试用例失败,构建应该失败。

英文:

I have some unit tests under Integration.java. I want Maven to ignore the test results of this class as they fail rarely (due to external server maintenance). But I don't want to ignore them from running as I need them for code coverage.

I've tried this configuration

&lt;plugin&gt;
	&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
	&lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
	&lt;configuration&gt;
		&lt;includes&gt;
			&lt;include&gt;IntegrationTest.java&lt;/include&gt;
		&lt;/includes&gt;
		&lt;testFailureIgnore&gt;true&lt;/testFailureIgnore&gt;
	&lt;/configuration&gt;
&lt;/plugin&gt;

This seems to be working by ignoring the failed tests. But the problem with the above configuration is, it is ignoring the failed tests at other classes as well.

I want all the tests under Integration.java needs to be executed but It shouldn't have any impact on the build to determine success/failure.
But, the build should fail if any test cases fail under any other Test classes other than Integration.java

答案1

得分: 1

@khmarbaise所提到的那样,这需要由maven-failsafe而不是surefire插件来处理。

<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,我确保了测试用例的运行以及良好的代码覆盖率,即使上游出现问题,构建也不会失败。

英文:

Like @khmarbaise has mentioned, It needs to be handled by maven-failsafe not surefire plugin

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

By using maven-failsafe-plugin I made sure the test cases run and for the good code coverage and the build will not fail even if the upstream is down.

答案2

得分: 0

**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>
英文:

Surefire by default includes all test classes whose name starts with Test/ends with Test/Tests/TestCase.

You can use excludes and includes parameters as per your need :

&lt;plugin&gt;
    &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
    &lt;version&gt;2.21.0&lt;/version&gt;
    &lt;configuration&gt;
        &lt;excludes&gt;
            &lt;exclude&gt;DataTest.java&lt;/exclude&gt;
        &lt;/excludes&gt;
        &lt;includes&gt;
            &lt;include&gt;DataCheck.java&lt;/include&gt;
        &lt;/includes&gt;
    &lt;/configuration&gt;
&lt;/plugin&gt;

huangapple
  • 本文由 发表于 2020年7月21日 22:07:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63016363.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定