编程语言
首页 > 编程语言> > java – Maven Cobertura:单元测试失败但成功

java – Maven Cobertura:单元测试失败但成功

作者:互联网

我在我的pom中配置了cobertura代码覆盖:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.4</version>
<configuration>
    <instrumentation>
        <excludes>
            <exclude>**/*Exception.class</exclude>
        </excludes>
    </instrumentation>
    <formats>
        <format>xml</format>
        <format>html</format>
    </formats>
</configuration>
</plugin>

并按以下命令开始测试:

mvn clean cobertura:cobertura

但是,如果其中一个单元测试失败,Cobertura只会记录此信息并且不会标记构建失败.

Tests run: 287, Failures: 1, Errors: 1, Skipped: 0

Flushing results...
Flushing results done
Cobertura: Loaded information on 139 classes.
Cobertura: Saved information on 139 classes.
[ERROR] There are test failures.

.................................

[INFO] BUILD SUCCESS

如何配置Cobertura标记构建失败的单元测试失败?

提前致谢.

解决方法:

如果你从cobertura插件运行一个特殊的目标,如果没有成功通过测试,你就不能强迫maven失败.插件目标将成功.

您可以将cobertura运行绑定到生命周期阶段(例如测试).这将使cobertura目标在此阶段(mvn clean test)运行,如果此阶段失败则失败.

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>cobertura-maven-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <formats>
                    <format>xml</format>
                    <format>html</format>
                </formats>
            </configuration>
            <executions>
                <execution>
                    <phase>test</phase>
                    <goals>
                        <goal>cobertura</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

这种解决方案的缺点是cobertura目标将在每个测试阶段运行.

标签:java,maven-2,cobertura
来源: https://codeday.me/bug/20190630/1339973.html