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