2

目前,我正在执行模块级别的最低 JUnit 总覆盖率。因此,如果总线路覆盖率低于某个值(在我的情况下为 80%),则构建失败。为此,我将 maven-surefire-plugin 与 maven-enforcer-plugin、JUnit 和 JMockit 一起使用。pom 文件部分看起来像这样。

<build>
<plugins>
....
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit4</artifactId>
            <version>2.18.1</version>
        </dependency>
    </dependencies>
    <configuration>
        <systemPropertyVariables>
            <coverage-outputDir>${basedir}/target/coverageReport</coverage-outputDir>
            <coverage-output>html,merge</coverage-output>               
            <coverage-check>80</coverage-check>
        </systemPropertyVariables>
    </configuration>
</plugin>
<plugin>
    <artifactId>maven-enforcer-plugin</artifactId>
    <executions>
        <execution>
            <id>coverage.check</id>
            <goals>
                <goal>enforce</goal>
            </goals>
            <phase>test</phase>
            <configuration>
                <rules>
                    <requireFilesDontExist>
                        <files>
                            <file>coverage.check.failed</file>
                        </files>
                    </requireFilesDontExist>
                </rules>
            </configuration>
        </execution>
    </executions>
</plugin>
....
</build>
<dependencies>
    ....
        <dependency>
            <groupId>org.jmockit</groupId>
            <artifactId>jmockit</artifactId>
            <version>1.17</version>
        </dependency>
        <dependency>
            <groupId>org.jmockit</groupId>
            <artifactId>jmockit-coverage</artifactId>
            <version>1.17</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
    ....    
  </dependencies>   

我的问题是,有些类的覆盖率可能远低于 80%,只要总覆盖率不低于 80%,构建仍然可以通过。

那么,有什么方法可以在班级级别上强制执行我的覆盖范围吗?

4

1 回答 1

3

给你一个固执的非答案:不要那样做。不要让您的构建因错过的覆盖数字而失败!

以下是支持这一点的理由:您正在尝试使用技术手段解决社会问题。

你真正的问题是:你想达到一定的代码质量,你认为你可以通过惩罚不遵守规定的人来实现。那真的行不通。迟早,人们会开始绕过这个想法。他们会寻找快速“让构建”快乐的方法;忽略您通过要求合理的代码覆盖率来提高代码质量的想法。或者一些经理来找你,指出新功能/修复现在比当前的覆盖目标更重要。然后呢?暂时禁用检查?允许更低的数字?

当然,测量单元测试的覆盖率是一件好事。您应该经常这样做,并使您的开发人员可以轻松访问此信息(例如,将其放在某个仪表板上)。要求您的 DEV 团队致力于某些目标也是公平的。但是强迫每个人的喉咙里都有一些报道数字......不会奏效。

(好吧:如果您在高级环境中工作,每个开发人员不仅接受您的想法,而且完全支持您;那么情况可能会有所不同。但如果您生活在这样的环境中,那么高覆盖率自然会出现; 不强制执行)。

于 2017-02-08T10:37:02.417 回答