Description: the original work is to do C language test coverage statistics, but I only know Java, or it is necessary to understand the Java test coverage statistics. This tool also does take me a little time, here is a record, in case I forget one day, documented.
An overview,
Test coverage is a platitude, but also a basic method to quantify the test work, here the specific test coverage theory is no longer described. This article focuses on Java backend coverage. Due to some reasons, the author’s department basically does not do UT, so for testing, we are most concerned about manual execution, interface execution (manual Postman, etc.), interface automation, WebUI automation on the coverage of an application system. Thanks to the increased complexity of activity logic a few years ago, development also has a sense of crisis. And finally I remembered to do the UT of this piece. Originally, Jacoco has been popular for many years, with various documents and posts describing it perfectly, but most articles summarize and use a particular form. Because I have used the coverage statistics tool of C, I thought it would be similar, but I still took many detours when I actually used it.
Second, the coverage principle
I have learned about the coverage tool principle of C before, and the coverage is straightforward, that is, piling. In the compilation of the time to configure the parameters, in the generation of assembly file staking, each pile point into 3-4 assembly statements, the final assembly file generation assembly generation target file, at the same time to generate the associated BB and ARC file, this piece of online information is also a lot, interested can find their own. Jacoco can be either compile-time piling or run-time piling, which is offline mode and on-the-fly mode. Jacoco’s on-the-fly mode actually involves Jacoco in the deployment process of the test environment, i.e. the loading of the class file. During the loading of the class file, the structure of the bytecode is dynamically modified and the probe of Jacoco is inserted.
Iii. Project Description
The test project is a Maven multi-module built Springboot project. The specific project structure is as follows:
4. Specific configuration
4.1 jar startup
1. Download the jacocoAgent. jar and jacococli.jar files
2. Pack your projects
3. Run the following command to start all jar files that you want to run:
Java - javaagent: "D: \ jacoco - 0.8.6 \ lib \ jacocoagent jar" = includes = *, output = file, append = true, destfile = D: \ jacoco exec - jar Demo - 0.0.1. JarCopy the code
Parameters explanation, refer to the following address www.cnblogs.com/kingzzm/p/3…
- -javaagent
The parameters added after JDk5 are mainly used to intervene in the bytecode loading process in a way that can be found when the JAR package is running. Note that there is a colon behind it, which is required if jacocoAgent. jar is in double quotes on Win, but not on Linux
- includes=*
This represents the packages that need to be staked bytecode, and * represents all class file loads are staked
- output=file
Output = tcpServer, which means to start and insert piles in tcpServer mode. Port and address need to be specified in tcpServer mode. It is better to use tcpServer. Address is configured as the accessible address of the server so that data can be dumped on any machine
Java-javaagent :"D:\jacoco-0.8.6\lib\jacocoagent.jar"=includes=*,output= tcpServer,port= available port number,address= your server address, Append =true - jar demo - 0.0.1. JarCopy the code
After using the tcpServer mode, dump data first
Java -jar jacococli.jar dump --address Address of your server --port number set at startup --destfile d:\jacoco.execCopy the code
- destfile
Specifies the location where the. Exec file is generated
- jar
Specify the JAR files that you want to start and start them separately with the above parameters
4. Review the report
Method 1: Ant to report, not using Ant, using Ant to configure build. XML, etc., which is not shown here
Method 2: Development tools view exec filesMethod 3: Jacococli.jar transfer report (multi-module)
Java-jar d:\jacoco-0.8.6\lib\jacococli.jar report.\ jacoco.exec --classfiles f:\workspace\springtest\module1\target\classes --sourcefiles f:\workspace\springtest\module1\src\main\java --classfiles f:\workspace\springtest\moduletest\target\classes --sourcefiles f:\workspace\springtest\moduletest\src\main\java --html report --xml report.xmlCopy the code
The results are as follows:Personal attempts:
- Classfiles must be com in classes after compilation
- — Sourcefiles theory can not be passed, not passed to the file level display, then need specific code and line number need this
4.2 war startup, Maven plug-in startup, and other startup methods
Through the tomcat, to start the war file, need to implement jacoco pile, it can refer to the following address: zhuanlan.zhihu.com/p/161787871