首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >单元测试覆盖率的自动控制技术

单元测试覆盖率的自动控制技术

作者头像
顾翔
发布2025-08-14 15:22:10
发布2025-08-14 15:22:10
1710
举报

1 通过Jacoco + mvn控制Java程序覆盖率

Jacoco是Java程序覆盖率工具,可以在pom.xml通过配置来自动控制程序的覆盖率。

pom.xml

代码语言:javascript
复制
<build>
  <plugins>
      <plugin>
    		<groupId>org.jacoco</groupId>
    		<artifactId>jacoco-maven-plugin</artifactId>
    		<version>0.8.8</version>
    		<executions>
        		<execution>
            		<id>pre-test</id>
            		<goals><goal>prepare-agent</goal></goals>
        		</execution>
        		<execution>
            		<id>post-test</id>
            		<phase>test</phase>
            		<goals><goal>report</goal></goals>
        		</execution>
        		<execution>
            		<id>check-coverage</id>
            		<goals><goal>check</goal></goals>
            		<configuration>
                		<rules>
                    		<rule>
                        		<element>BUNDLE</element>
                        		<limits>
                            		<limit>
                                		<counter>LINE</counter>
                                		<value>COVEREDRATIO</value>
                                		<minimum>0.90</minimum>
                            		</limit>
                        		</limits>
                    		</rule>
                		</rules>
            		</configuration>
        		</execution>
        		<execution>
            		<id>pre-unit-test</id>
            		<goals>
                	<goal>prepare-agent</goal>
            		</goals>
         			<configuration>
            			<destFile> target/coverage-reports/jacoco.exec</destFile>
             			<propertyName>surefireArgLine</propertyName>
             		</configuration>
        		</execution>
        		<execution>
<id>post-unit-test</id>
             		<phase>test</phase>
             		<goals>
                		<goal>report</goal>
             		</goals>
             		<configuration>
                		<dataFile> target/coverage-reports/jacoco-ut.exec</dataFile>
                		<outputDirectory>target/jacoco-ut</outputDirectory>
              		</configuration>
           		</execution>
    		</executions>
        </plugin>
      </plugins>
</build>

在pom.xml中

代码语言:javascript
复制
<limit>
  <counter>LINE</counter>
  <value>COVEREDRATIO</value>
  <minimum>0.90</minimum>
</limit>

0.90,即90%为最小行覆盖率,当被测程序行覆盖率<=90%,则运行mvn verify构建失败,否则构建成功。

构建成功情形

代码语言:javascript
复制
C:\Code\MyJava\javawork\ChatGPTEbusiness>mvn verify
…
Results:
[INFO]
[INFO] Tests run: 83, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- jacoco:0.8.8:report (post-test) @ ChatGPTEbusiness ---
[INFO] Loading execution data file C:\Code\MyJava\javawork\ChatGPTEbusiness\target\jacoco.exec
[INFO] Analyzed bundle 'ChatGPTEbusiness' with 12 classes
[INFO]
[INFO] --- jacoco:0.8.8:report (post-unit-test) @ ChatGPTEbusiness ---
[INFO] Skipping JaCoCo execution due to missing execution data file.
[INFO]
[INFO] --- jar:3.3.0:jar (default-jar) @ ChatGPTEbusiness ---
[INFO] Building jar:

构建成功失败情形

代码语言:javascript
复制
C:\Code\MyJava\javawork\ChatGPTEbusiness>mvn verify
…
Results:
[INFO]
[INFO] Tests run: 83, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- jacoco:0.8.8:report (post-test) @ ChatGPTEbusiness ---
[INFO] Loading execution data file C:\Code\MyJava\javawork\ChatGPTEbusiness\target\jacoco.exec
[INFO] Analyzed bundle 'ChatGPTEbusiness' with 12 classes
[INFO]
[INFO] --- jacoco:0.8.8:report (post-unit-test) @ ChatGPTEbusiness ---
[INFO] Skipping JaCoCo execution due to missing execution data file.
[INFO]
[INFO] --- jar:3.3.0:jar (default-jar) @ ChatGPTEbusiness ---
[INFO] Building jar: C:\Code\MyJava\javawork\ChatGPTEbusiness\target\ChatGPTEbusiness-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- jacoco:0.8.8:check (check-coverage) @ ChatGPTEbusiness ---
[INFO] Loading execution data file C:\Code\MyJava\javawork\ChatGPTEbusiness\target\jacoco.exec
[INFO] Analyzed bundle 'ChatGPTEbusiness' with 12 classes
[WARNING] Rule violated for bundle ChatGPTEbusiness: lines covered ratio is 0.82, but expected minimum is 0.90
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:
 
11.775 s
[INFO] Finished at: 2025-08-09T15:59:32+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.jacoco:jacoco-maven-plugin:0.8.8:check (check-coverage) on project ChatGPTEbusiness: Coverage checks have not been met. See log for details. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

注意,这里使用mvn test是没用的

代码语言:javascript
复制
C:\Code\MyJava\javawork\ChatGPTEbusiness> mvn test
…
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:
 
11.718 s
[INFO] Finished at: 2025-08-09T16:02:47+08:00
[INFO] ------------------------------------------------------------------------

2自动控制其他程序覆盖率

Python语言

代码语言:javascript
复制
pytest --cov --cov-fail-under=80

Go语言

代码语言:javascript
复制
go test -coverprofile=coverage.out && goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN

C#语言

代码语言:javascript
复制
dotnet test /p:CollectCoverage=true /p:Threshold=80

顾翔凡言:人工智能未来的发展瓶颈在于对知识的更新。唯一不变的是变化,知识发生了变化,人工智能软件能否及时跟进变化,可能阻碍人工智能的使用。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2025-08-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档