jacoco测试代码覆盖率
In the first article, we discovered one of the two key Gradle commands which come with Jacoco plugin — jacocoTestReport. As you now know, it can be used to measure code coverage in your project by generating detailed reports in HTML, XML, or CVS formats. This time I’d like to share my experience with the second piece of this puzzle — jacocoTestCoverageVerification.
在第一篇文章中,我们发现了Jacoco插件随附的两个重要Gradle命令之一-jacocoTestReport 。 如您所知,它可以通过生成HTML,XML或CVS格式的详细报告来衡量项目中的代码覆盖率。 这次,我想与这个难题的第二部分( jacocoTestCoverageVerification)分享我的经验。
As per the documentation, this task does the following:
根据文档,此任务执行以下操作:
Task for verifying code coverage metrics. Fails the task if violations are detected based on specified rules.
验证代码覆盖率指标的任务。 如果根据指定规则检测到违规,则使任务失败。
So as you might have already guessed, this tool can be used to check whether some coverage threshold is reached or not. Basically, you specify the minimum coverage measured in % and run the task. If your unit tests “visit” the desired % of your codebase then this task will silently complete. Otherwise, it will fail. This can be really helpful for development teams since it allows you to have a kind of enforcement for writing unit tests and all you need to do for that is to setup Jacoco and then simply ask your CI/CD to execute jacocoTestCoverageVerification along with other tasks for your pipelines.
因