Didi open source Super-jacoco: java code coverage collection platform documentation

Jumei’s guide: Super-Jacoco is a one-stop JAVA code full/diff coverage collection platform based on the secondary development of Jacoco and git, which can collect code coverage data at low cost and without intrusion; Super-Jacoco not only supports JVM running In addition to the coverage collection of the time period; it can also seamlessly connect with the environment, collect the full/incremental coverage of the server-side custom time period code; and provide a visual html coverage report to assist coverage analysis and support accurate testing.

Analysis and product iteration work must be carried out around it, so that the operation behavior on the mobile terminal is particularly important. If it is used well, it can improve efficiency and create user value and business value. How can we use it better? what about them

0. 

background

In the process of software production and delivery, we use unit testing, interface testing, functional testing and other means to ensure software quality; no matter what kind of testing means, whether the case design is comprehensive and streamlined is particularly important. During the actual project testing process, the design of the case often has the following problems:

1. The development students wrote a lot of unit tests, and a piece of code logic was repeatedly executed, and a few scenarios or abnormal code logic were not executed;

2. The test cases designed by the test students have been reviewed repeatedly, but there are still abnormal scenarios that are not covered, and there are missed tests;

3. As a daily regression method, the interface automation test case cannot determine whether it covers all code logic, and its reliability cannot be evaluated.

So, how can we use the most streamlined case to ensure the comprehensiveness of the test? At present, the industry is more recognized that the method is to supplement the response case by analyzing the coverage rate of the changed code; we investigated the industry's open source java code coverage statistics tools jacoco and EMMA, and found that both jacoco and EMMA only support the collection of full code coverage and cannot Meet the demands of accurate analysis of incremental code coverage. Therefore, we urgently need a tool that can collect code coverage for changes.

1. 

Introduction to Super-jacoco

Super-Jacoco is a one-stop JAVA code full/diff coverage collection platform based on the secondary development of Jacoco and git, which can collect code coverage data at low cost and without intrusion. In addition to supporting coverage collection during the JVM runtime, Super-Jacoco can also seamlessly interface with the environment to collect full/incremental coverage of the server-side custom time period code. It also provides a visual coverage report in html format to assist in the accurate analysis of coverage and support the implementation of accurate testing.

Single test code full/incremental coverage collection:

In on-the-fly mode, coverage data can be collected without any modifications to the development code;

Functional test full/incremental coverage collection:

Integrated with the environment deployment platform ebase, you only need to add -javaagent:jacocoagent.jar=includes=com.* to the JAVA startup command to collect functional test coverage data;

Visual report:

The visualized html coverage rate report assists in the precise analysis of the coverage situation and supports the implementation of precise testing.

2. 

Super-jacoco principle

2 . 1.  Overall process

In order to support incremental coverage collection, we need to do two things: 1) Obtain different version code diff files; 2) Secondary development of jacoco to support incremental method list parameters.

2.2.   Get incremental code

Main process: pull the master (reference branch) and feature (testing branch) codes, and then use JGit to compare the source codes of the two branches to obtain incremental codes. The following are some code snippets:

2.3. The second transformation of jacoco supports incremental method list parameters  

JaCoCo's analysis of exec is mainly in the analyzeClass(final byte[] source) method of the Analyzer class. The createAnalyzingVisitor method will be called to generate an ASM class visitor for parsing. Continue to follow the code and find that the method-level probe calculation logic is in the visitMethod method of the ClassProbesAdapter class. So we only need to modify the visitMethod method so that it only analyzes the newly added or changed methods of each extracted class, and does not process non-specified classes and methods. The modified core code snippet is as follows: 

2.4. Execution  


Just add -Djacoco.diffFile=change method list to the executed mvn command to collect the code coverage of the change method. If -Djacoco.diffFile is not passed in or the Djacoco.diffFile parameter is empty, full coverage is collected by default.

2.5. Report output  


The coverage report is as shown in the figure below. In the figure, it is the implementation class of a certain service. There are 23 methods in the latest code, but only 5 methods that have been changed or added will be covered and displayed:

3. 

characteristic

  • General : Support both unit test coverage collection and manual test coverage collection; support both full coverage collection and diff coverage collection;

  • No intrusion : the on-the-fly mode is adopted, and the coverage data can be collected without any modification to the development code;

  • High availability : Distributed architecture, the task machine can be expanded infinitely, avoiding performance bottlenecks when the task machine is down or there are too many tasks;

  • Visualization : Provide coverage reports in html format, which is highly readable.

4. 

architecture

5. 

how to use

▍5.1.  Database installation and initialization

5.1.1 Install the mysql database, and execute the table-building SQL in the sql/db.sql file after creating the database

▍5.2.  Compile and package

5.2.1 Install JDK1.8, mavan3

5.2.2 clone code, change the database and gitlab configuration in the application.properties file:

spring.datasource.url=jdbc:mysql://IP:port/database name?useUnicode=true&characterEncoding=utf8

    spring.datasource.username=

    pring.datasource.password=

    gitlab.username=

    gitlab.password=

5.2.3 Execute mvn package -Dmaven.test.skip=true to generate super-jacoco.jar

▍5.3. Deployment 

5.3.1 Execute "nohup java -jar super-jacoco.jar &" to start the code coverage service, the default port is 8899

▍5.4. Coverage collection interface 

5.4.1 Single test coverage interface

1)启动覆盖率收集
URL:/cov/triggerUnitCover
调用方法:POST
参数(body方式传入):{"uuid":"uuid","type":1,"gitUrl":"git@git","subModule":"","baseVersion":"master","nowVersion":"feature","envType":"-Ptest"}
返回:{"code":200,"data":true,"msg":"msg"}
备注:


2)获取覆盖率结果
URL:/cov/getUnitCoverResult
调用方法:GET
参数:uuid(String)
返回:
{"code":200,"data":{"coverStatus":1,"errMsg":"msg","lineCoverage":100.0,"branchCoverage":100.0,"logFile":"file content","reportUrl":"http://"},"msg":"msg"}
备注:

5.4.2 Environment coverage interface

1)启动覆盖率收集
URL:/cov/triggerEnvCov
调用方法:POST
参数(body方式传入):{"uuid":"uuid","type":1,"gitUrl":"git@git","subModule":"","baseVersion":"master","nowVersion":"feature","address":"127.0.0.1","port":"8088"}
返回:{"code":200,"data":true,"msg":"msg"}
备注:IP和port为模块部署服务器的IP和端口,在dump jacoco.exec时使用,需要提前把org.jacoco.agent-0.8.5-runtime.jar包拷贝到服务器:/home/xxx/目录,服务启动时需要添加启动参数:-javaagent:/home/xxx/org.jacoco.agent-0.8.5-runtime.jar=includes=*,output=tcpserver,address=*,port=18513


2)获取覆盖率结果
URL:/cov/getEnvCoverResult
调用方法:GET
参数:uuid(String)
返回:{"code":200,"data":{"coverStatus":1,"errMsg":"msg","lineCoverage":100.0,"branchCoverage":100.0,"logFile":"file content","reportUrl":"http://"},"msg":"msg"}
备注:

6. 

Summarize

In the context of rapid business iteration, accurate testing will be the development trend of efficient testing, and code coverage is an important part of it. Super-jacoco makes java code coverage statistics convenient and fast, which is helpful for accurate testing Promote and develop, welcome to join!

7. 

GitHub project address

https://github.com/didi/super-jacoco

open source team

The team members are all from the technical team of Didi Car Service

further reading


内容编辑 | Teeo联系我们 | [email protected]

Guess you like

Origin blog.csdn.net/DiDi_Tech/article/details/111399149#comments_27749267