【问题标题】:How can I have builds fail if new code is below coverage threshold?如果新代码低于覆盖率阈值,如何让构建失败?
【发布时间】:2018-04-14 06:46:41
【问题描述】:

是否有任何 Java 测试框架(最好是 Jacoco 和/或 Sonar)允许代码覆盖率较差的团队原谅现有的代码库,但要求新代码高于阈值,(直到他们能够获得所有旧代码覆盖!)

【问题讨论】:

标签: java sonarqube automated-tests code-coverage jacoco


【解决方案1】:

您可以在 pom.xml 中添加构建插件。这个正在对至少 80% 的线路覆盖率进行检查。请参阅Jacoco doc 了解更多信息。

<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.8.0</version>
            <executions>
                <execution>
                    <id>default-prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>jacoco-site</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                </execution>
                <execution>
                    <id>jacoco-check</id>
                    <phase>test</phase>
                    <goals>
                        <goal>check</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <rule>
                                <element>BUNDLE</element>
                                <limits>
                                    <limit>
                                        <counter>LINE</counter>
                                        <value>COVEREDRATIO</value>
                                        <minimum>0.80</minimum>
                                    </limit>
                                </limits>
                            </rule>
                        </rules>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

【讨论】:

    【解决方案2】:

    我希望您可以使用 Sonar Quality Gates 执行此操作,并将其与您的 CI 服务器集成。在 Jenkins 中,您可能想尝试 Quality Gates Plugin

    【讨论】:

    • 我只想放在这里,我们为此目的使用 Sonar Quality Gates,不是直接在 jenkins 上,而是通过 bitbucket 集成和拉取请求合并检查来防止此类事情 - 它实际上工作得很好伟大的!因为您可以定义“错误”和“警告”阈值
    猜你喜欢
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 2021-05-20
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多