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

📝 面试求职: 「面试试题小程序」 ,内容涵盖 测试基础、Linux操作系统、MySQL数据库、Web功能测试、接口测试、APPium移动端测试、Python知识、Selenium自动化测试相关、性能测试、性能测试、计算机网络知识、Jmeter、HR面试,命中率杠杠的。(大家刷起来…)

📝 职场经验干货:

软件测试工程师简历上如何编写个人信息(一周8个面试)

软件测试工程师简历上如何编写专业技能(一周8个面试)

软件测试工程师简历上如何编写项目经验(一周8个面试)

软件测试工程师简历上如何编写个人荣誉(一周8个面试)

软件测试行情分享(这些都不了解就别贸然冲了.)

软件测试面试重点,搞清楚这些轻松拿到年薪30W+

软件测试面试刷题小程序免费使用(永久使用)


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

最后: 下方这份完整的软件测试视频教程已经整理上传完成,需要的朋友们可以自行领取【保证100%免费】

​​​

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值