Maven Cobertura:单元测试失败但构建成功

发布于 2024-10-19 13:47:09 字数 1063 浏览 1 评论 0原文

我已经在我的 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 在单元测试失败之一中标记构建失败?

提前致谢。

I've configured cobertura code coverage in my pom:

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

And start test by following command:

mvn clean cobertura:cobertura

But if one of unit test fail Cobertura only log this information and doesn't mark build fail.

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

How to configure Cobertura marks build failed in one of unit test fail?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巾帼英雄 2024-10-26 13:47:09

如果您从 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 目标将在每个 test 阶段运行。

If you run a special goal from the cobertura plugin you can not force maven to fail the build if a test was not passed successfully. The plugin goal will succeed.

You can bind the cobertura run to a lifecycle phase (e.g. test). This will make the cobertura goal run with this phase (mvn clean test) and fail if this phase fails.

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

The disadvantage of this solution is that the cobertura goal will run ich each test phase.

孤独陪着我 2024-10-26 13:47:09

您可以将 haltOnFailure 属性设置为 true。

<configuration>
    ...
    <check>
        ...
        <haltOnFailure>true</haltOnFailure>
        ...
    </check>
</configuration>

You could set haltOnFailure property to true.

<configuration>
    ...
    <check>
        ...
        <haltOnFailure>true</haltOnFailure>
        ...
    </check>
</configuration>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文