[Use jacoco to verify interface automation code coverage] Super detailed! ! !

jacoco verification interface automation code coverage

jacoco download

Official download address: https://central.sonatype.com/?smo=true
csdn download address: https://download.csdn.net/download/m0_43550804/88228839

The jacoco tool mainly includes:

jacocoagent.jar,jacocoant.jar,jacococli.jar

jacocoagent.jar is an agent program used to collect execution information and generate code coverage data when a request or JVM exits

jacocoant.jar is to add jacoco to the ant project task, start the Java program, record the data and create a coverage report, through coverage, agent, dump and merge. Tasks such as report create reports in different formats

jacococli.jar provides some command line interfaces to generate exec files, merge or generate html reports, etc.

Preparation:

1. Download and decompress jacoco
2. Make jar package of the project under test, you can find the development jar package
3. Download the project project package, because it needs to be used when converting the coverage file into an html file. A directory \target\classes (this directory will be generated after the project is compiled), \src\main\java.

Begin execution

1. Run the jar package locally (jacoco automatically inserts piles at runtime)

java -javaagent:D:\jacoco\test\jacoco-0.8.8\lib\jacocoagent.jar=includes=*,output=tcpserver,port=6301,address=localhost,append=false -jar D:\jacoco\test\services-edu-web.jar

Interpretation of parameters:
–includes: class name, such as [com.*] represents the class name starting with com
–classdumpdir: directory where class files are stored
–output: tcpserver method, obtain data through address and port
–append: true is to execute the data file already exists, the coverage data will be appended to the existing file
–address: the access address of tcpserver

Note: -address sets the access address of tcpserver. It can be configured as 127.0.0.1, or it can be configured so that when the actual access ip
is configured as 127.0.0.1, the dump data can only be dumped on this server, and the data cannot be dumped remotely.
When configured as the actual ip address, you can dump data through ant xml or api on any machine (the premise is that the ip must be connected, and it will be useless if it fails).

Two, generate dump

java -jar D:\jacoco\test\jacoco-0.8.8\lib\jacococli.jar dump --address localhost --port 6301 --destfile D:\jacoco\test\jacoco.exec --reset

After execution, the exec file jacoco.exec specified in the command will be generated in the directory
insert image description here

Notice! Before generating the file, execute the interface automation use case first!

Interpretation of parameters:
–reset: After the coverage data is generated, reset the previous statistics. If you want to get the statistics again, you need to delete the previously generated exec file
–address: The IP of the host running jacocoagent.jar
–port: The host running jacocoagent.jar Host monitoring port

3. Convert the generated file to html

java -jar D:\jacoco\test\jacoco-0.8.8\lib\jacococli.jar report D:\jacoco\test\jacoco.exec --classfiles  D:\jacoco\services-edu-web\target\classes --sourcefiles D:\jacoco\services-edu-web\src\main\java --html D:\jacoco\test\report --name 实训平台

Generate report in specified file
insert image description here

Interpretation of parameters:
–name: name in html
–classfiles: Compiled file storage directory, maven project is under target
–sourcefiles: original file storage directory, maven project is src\main\java
-sourcefiles can not be passed, if not passed, overwrite The rate report is displayed at the file level. No matter how detailed it is, if you need specific codes and line numbers, you need to pass sourcefiles.
After generating the report, click to enter index.html to view it
insert image description here

end

Through the above steps, you can complete the measurement of your own interface automation coverage.
The above methods are purely manual operations. Through the above principles, shell scripts can be executed through the Jenkins pipeline.
Subsequent supplements using the Jenkins method. On the one hand, it is used for personal memos. On the other hand, I hope to bring you some help to help you understand the principle of the precise test-interface automation coverage scheme.

Guess you like

Origin blog.csdn.net/m0_43550804/article/details/129585730