Jacoco collecting unit test, integration test, and system functional test coverage
2020-02-27
1 installed version
2 system under Code Example
3 collecting unit test coverage
4 collection and integration function test coverage
Code coverage can be used in unit testing, system testing and functional testing system
- Unit Testing: left, before the code is submitted, coverage requirements can be developed, if not met, can not be submitted
- Integration Testing: Testing Interface
- Test system functions: user interface operation
1 installed version
- jacoco-0.8.2.zip extract to: D: \ software \ jacoco
- apache-ant-1.10.6-bin.zip extract to: D: \ software \ ant
Code Example 2 SUT
Source: jacocodemo.zip
This example illustrates the backend system based springboot framework, mainly addition, subtraction of two numbers, provides the interface to the external call restful
1 system under test jacocoDemo
3 collecting unit test coverage
Configuring test project pom.xml, add plug-in settings:
<-! Maven test for the test phase of default life cycle. -> <-! Test phase and test target maven-surefire-plugin is bound to, which is a built-in binding. -> <-! Maven to execute JUnit and TestNG test case through plug-ins. -> < plugin > < the groupId > org.apache.maven.plugins </ the groupId > < the artifactId > Maven-Surefire-plugin </ the artifactId > </ plugin > <-! Unit test command: mvn the Test -> <! - the result of production jacoco-unit.exec file in the target directory, show the correct implementation of jacoco -> < plugin > < groupId > org.jacoco </ groupId > < the artifactId > jacoco-Maven-plugin </ the artifactId > < Version > 0.8.3 </ Version > < Configuration > <-! designated storage location .exec generated file -> < the destFile > target / coverages / jacoco- unit.exec </ the destFile > <! -Jacoco final report is generated according .exec file, so the need .exec specified storage path -> < dataFile > target / coverages / jacoco-unit.exec </ dataFile > </ Configuration > < Executions > < Execution > < ID > jacoco-the initialize </ ID > < Goals > < Goal > PREPARE-Agent </ Goal > </ Goals > </ Execution > < Execution > <id>jacoco-site</id> <phase>test</phase> <goals> <goal>report</goal> </goals> </execution> </executions> </plugin>
Maven command execution
mvn test
Command execution log is as follows:
D:\Code\jacocodemo>mvn test [INFO] Scanning for projects... [INFO] [INFO] -----------------------< com.example:jacocodemo >----------------------- [INFO] Building jacocodemo 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- jacoco-maven-plugin:0.8.3:prepare-agent (jacoco-initialize) @ jacocodemo --- [INFO] argLine set to -javaagent:D:\\software\\maven\\repo\\m2\\org\\jacoco\\org.jacoco.agent\\0.8.3\\org.jacoco.agent-0.8.3-runtime.jar=destfile=D:\\Code\\jacocodemo\\target\\coverage\\jacoco-unit.exec [INFO] [INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ jacocodemo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] Copying 1 resource [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ jacocodemo --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 6 source files to D:\Code\jacocodemo\target\classes [The INFO] / D: / Code / jacocodemo / the src / main / Java / COM / Example / Controller / MathController.java: D: \ Code \ jacocodemo \ the src \ main \ Java \ COM \ Example \ Controller \ MathController.java use unchecked or unsafe operation of the. [INFO] /D:/Code/jacocodemo/src/main/java/com/example/controller/MathController.java: For more information, please use - Xlint: unchecked recompiled. [INFO] [INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ jacocodemo --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory D:\Code\jacocodemo\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ jacocodemo --- [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to D:\Code\jacocodemo\target\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ jacocodemo --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.example.service.MathServiceTest [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.07 s - in com.example.service.MathServiceTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] [INFO] --- jacoco-maven-plugin:0.8.3:report (jacoco-site) @ jacocodemo --- [INFO] Loading execution data file D:\Code\jacocodemo\target\coverage\jacoco-unit.exec [INFO] Analyzed bundle 'jacocodemo' with 6 classes [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 5.227 s [INFO] Finished at: 2020-02-27T16:07:58+08:00 [INFO] ------------------------------------------------------------------------
Exec file and the report file generated as shown in FIG. 2
FIG 2 the unit test file one surface coverage
Since the class is called MathService add method, jacoco collect this information, as shown in FIG. 3
3 Coverage Details
4 collection integration and functional test coverage
A packaged system under test and renamed jacocodemo.jar
mvn package
2 Start the system under test, and by javaagentJavaAgent server listens on TCP port set, then the information (jacoco.exec) collected to write TCP connection
java -javaagent:D:/software/jacoco/lib/jacocoagent.jar=includes=*,output=tcpserver,port=6300,address=10.8.116.81,append=true -jar D:/Code/jacocodemo/target/jacocodemo.jar
3 配置ant的build.xml文件,并把它放到被测系统代码根目录
build.xml
<?xml version="1.0" encoding="UTF-8"?> <project name="JacocoDmo" xmlns:jacoco="antlib:org.jacoco.ant" default="jacoco"> <!--Jacoco 的安装路径--> <property name="jacocoantPath" value="D:\software\jacoco\lib\jacocoant.jar"/> <!--最终生成 .exec 文件的路径,Jacoco 就是根据这个文件生成最终的报告的--> <property name="jacocoexecPath" value="D:\Code\jacocodemo\target\coverage\jacoco-integation.exec"/> <!--生成覆盖率报告的路径--> <property name="reportfolderPath" value="D:\Code\jacocodemo\target\coverage\jacoco-report"/> <!--远程 Tomcat 服务的 ip 地址--> <property name="server_ip" value="10.8.116.81"/> <!--前面配置的远程 Tomcat 服务打开的端口,要跟上面配置的一样--> <property name="server_port" value="6300"/> <!--源代码路径--> <property name="checkOrderSrcPath" value="D:\Code\jacocodemo\src\main\java" /> <!--.class 文件路径--> <property name="checkOrderClasspath" value="D:\Code\jacocodemo\target\classes" /> <!--让 ant 知道去哪儿找 Jacoco--> <taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml"> <classpath path="${jacocoantPath}" /> </taskdef> <!--dump 任务: 根据前面配置的 ip 地址,和端口号, 访问目标 Tomcat 服务,并生成 .exec 文件。--> <target name="dump"> <jacoco:dump address="${server_ip}" reset="false" destfile="${jacocoexecPath}" port="${server_port}" append="true"/> </target> <!--jacoco 任务: 根据前面配置的源代码路径和 .class 文件路径, 根据 dump 后,生成的 .exec 文件,生成最终的 html 覆盖率报告。--> <target name="report"> <delete dir="${reportfolderPath}" /> <mkdir dir="${reportfolderPath}" /> <jacoco:report> <executiondata> <file file="${jacocoexecPath}" /> </executiondata> <structure name="JaCoCo Report"> <group name="Check Order related"> <classfiles> <fileset dir="${checkOrderClasspath}"> <!-- 过滤不必要的文件 --> <exclude name="**/R.class"/> <exclude name="**/R$*.class"/> <exclude name="**/*$ViewInjector*.*"/> <exclude name="**/BuildConfig.*"/> <exclude name="**/Manifest*.*"/> </fileset> </classfiles> <sourcefiles encoding="UTF-8"> <fileset dir="${checkOrderSrcPath}" /> </sourcefiles> </group> </structure> <html destdir="${reportfolderPath}" encoding="UTF-8" /> <csv destfile="${reportfolderPath}/coverage-report.csv" encoding="UTF-8"/> <xml destfile="${reportfolderPath}/coverage-report.xml" encoding="UTF-8"/> </jacoco:report> </target> </project>
4 用postman调用被测系统接口,如下图4所示
图4 postman调用被测系统接口
5 执行ant dump拉取jacoco.exec覆盖率信息,执行ant report生成覆盖率报告
D:\Code\jacocodemo>ant dump Buildfile: D:\Code\jacocodemo\build.xml dump: [jacoco:dump] Connecting to /10.8.116.81:6300 [jacoco:dump] Dumping execution data to D:\Code\jacocodemo\target\coverage\jacoco-integation.exec BUILD SUCCESSFUL Total time: 0 seconds D:\Code\jacocodemo>ant report Buildfile: D:\Code\jacocodemo\build.xml report: [mkdir] Created dir: D:\Code\jacocodemo\target\coverage\jacoco-report [jacoco:report] Loading execution data file D:\Code\jacocodemo\target\coverage\jacoco-integation.exec [jacoco:report] Writing bundle 'Check Order related' with 6 classes BUILD SUCCESSFUL Total time: 0 seconds
从覆盖率报告可以看到subtract方法被覆盖,如图5所示
图5 接口测试覆盖方法subtract
参考:
[1] Jacoco远程统计tomcat服务(Windows系统)的代码覆盖率