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

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

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

pom.xml

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

<limit>
  <counter>LINE</counter>
  <value>COVEREDRATIO</value>
  <minimum>0.90</minimum>
</limit>

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

构建成功情形

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>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是没用的

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

pytest --cov --cov-fail-under=80

Go语言

go test -coverprofile=coverage.out && goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN

C#语言

dotnet test /p:CollectCoverage=true /p:Threshold=80

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

目录
相关文章
|
7天前
|
人工智能 运维 安全
|
5天前
|
人工智能 异构计算
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
|
6天前
|
机器学习/深度学习 人工智能 自然语言处理
B站开源IndexTTS2,用极致表现力颠覆听觉体验
在语音合成技术不断演进的背景下,早期版本的IndexTTS虽然在多场景应用中展现出良好的表现,但在情感表达的细腻度与时长控制的精准性方面仍存在提升空间。为了解决这些问题,并进一步推动零样本语音合成在实际场景中的落地能力,B站语音团队对模型架构与训练策略进行了深度优化,推出了全新一代语音合成模型——IndexTTS2 。
606 21
|
12天前
|
人工智能 JavaScript 测试技术
Qwen3-Coder入门教程|10分钟搞定安装配置
Qwen3-Coder 挑战赛简介:无论你是编程小白还是办公达人,都能通过本教程快速上手 Qwen-Code CLI,利用 AI 轻松实现代码编写、文档处理等任务。内容涵盖 API 配置、CLI 安装及多种实用案例,助你提升效率,体验智能编码的乐趣。
969 110
|
6天前
|
人工智能 测试技术 API
智能体(AI Agent)搭建全攻略:从概念到实践的终极指南
在人工智能浪潮中,智能体(AI Agent)正成为变革性技术。它们具备自主决策、环境感知、任务执行等能力,广泛应用于日常任务与商业流程。本文详解智能体概念、架构及七步搭建指南,助你打造专属智能体,迎接智能自动化新时代。