实践代码覆盖之JaCoCo

JaCoCo

覆盖计数

行覆盖率:度量被测程序的每行代码是否被执行,判断标准行中是否至少有一个指令被执行。

类覆盖率:度量计算class类文件是否被执行。

分支覆盖率:度量if和switch语句的分支覆盖情况,计算一个方法里面的。

总分支数,确定执行和不执行的 分支数量。

方法覆盖率:度量被测程序的方法执行情况,是否执行取决于方法中是否有至少一个指令被执行。

指令覆盖:计数单元是单个java二进制代码指令,指令覆盖率提供了代码是否被执行的信息,度量完全 独立源码格式。

圈复杂度:在(线性)组合中,计算在一个方法里面所有可能路径的最小数目,缺失的复杂度同样表示测 试案例没有完全覆盖到这个模块。

jacoco-maven-plugin

goal phrase  
help    
prepare-agent initialize Resulting coverage information is collected during execution and by default written to a file when the process terminates.【生成 jacoco.exec】
prepare-agent-integration pre-integration-test 【生成 jacoco-it.exec】
merge generate-resources merging a set of execution data files (*.exec) into a single file
report verify Creates a code coverage report for tests of a single project in multiple formats (HTML, XML, and CSV).【从jacoco.exec生成报告(site/jacoo),因而依赖prepare-agent
report-integration verify 【从jacoco-it.exec生成报告(site/jacoo),因而依赖prepare-agent-integration
report-aggregate    
check verify Checks that the code coverage metrics are being met.
dump post-integration-test Request a dump over TCP/IP from a JaCoCo agent running in tcpserver mode.
instrument process-classes Performs offline instrumentation. Note that after execution of test you must restore original classes with help of “restore-instrumented-classes” goal.
restore-instrumented-classes prepare-package Restores original classes as they were before offline instrumentation.

直接运行

1
$ mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent install org.jacoco:jacoco-maven-plugin:report

配置运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
			<plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.2</version>
                <executions>
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-prepare-agent-integration</id>
                        <goals>
                            <goal>prepare-agent-integration</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-report</id>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-report-integration</id>
                        <goals>
                            <goal>report-integration</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-check</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <rule>
                                    <element>BUNDLE</element>
                                    <limits>
                                        <limit>
                                            <counter>COMPLEXITY</counter>
                                            <value>COVEREDRATIO</value>
                                            <minimum>0.25</minimum>
                                        </limit>
                                    </limits>
                                </rule>
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

mvn clean install

report

样例:

jacoco-report-package

jacoco-report-class