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 for JVM runtime periods, super-Jacoco supports It is also able to seamlessly integrate with the environment and collect full/incremental coverage of server custom time period code; And provide visual HTML coverage report to assist coverage analysis and support accurate test landing.

Background of 0.

In the process of software production and delivery, we guarantee software quality by means of unit test, interface test and function test. No matter which kind of test means, case design is comprehensive and concise, it is particularly important. In the actual project testing process, the design of case often appears the following problems:

1. The development student wrote a large number of single tests and repeatedly executed a piece of code logic, but a few scenes or exception code logic was not executed;

2. After repeated review of test cases designed by students, there are still abnormal scenarios that are not covered, resulting in missed testing;

3. As a means of daily regression, it is impossible to determine whether all code logic is covered and its reliability cannot be evaluated.

So, how to use the most concise case to ensure the comprehensiveness of the test? At present, the industry is more recognized by means of analyzing the coverage of the changed code to supplement the response case; 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, which cannot meet the demands of accurate analysis of incremental code coverage. Therefore, we desperately need a tool that can collect coverage of code changes.

1. Super – jacoco profile

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 for JVM runtime periods, super-Jacoco supports It is also able to seamlessly integrate with the environment and collect full/incremental code coverage for server-side custom time periods. Visual coverage report in HTML format is provided to assist accurate analysis of coverage and support accurate test landing.

Single-test code full/incremental coverage collection:

On-the-fly mode, which can collect coverage data without making any changes to the development code;

Full/Incremental coverage collection for functional tests:

Ebase deployment platform and environment integration, only added in the JAVA startup command – javaagent: jacocoagent. Jar = includes = com. * can collect function test coverage data;

Visual report:

Visual HTML coverage report to assist accurate analysis of coverage and support accurate test landing.

2. Super – jacoco principle

2.1. Overall process

To support incremental coverage collection, we need to do two things: **1) ** get diff files for different versions of code; **2) ** Secondary development of Jacoco to support incremental method list parameters.

2.2. Access to incremental code

Main process: pull the code of master (reference branch) and feature(test branch), and then compare the source code of the two branches through JGit to obtain the incremental code. Here are some code snippets:

2.3 jacoco secondary transformation, support incremental method list parameters

JaCoCo’s parsing of exec is primarily in the analyzeClass(final Byte [] source) method of the Analyzer class. The createAnalyzingVisitor method is called to generate an ASM class visitor for parsing. Continue with the code and find that the method-level probe calculation logic is in the visitMethod method of the ClassProbesAdapter class. So we just need to modify the visitMethod method so that it only parses new or changed methods for each class that we extract, and doesn’t handle any classes or methods that aren’t specified. The core code snippet after transformation is as follows:

▍ 2.4. Execution

To collect code coverage for the change method, simply add -djacoco. diffFile= list of change methods to the MVN command executed. If -djacoco. diffFile is not passed or the djacoco. diffFile parameter is empty, the full coverage is collected by default.

2.5. Report output

The coverage report is shown in the following figure, which shows the implementation class of a service. There are 23 methods in the latest code, but only 5 methods that have been changed or added will be counted and displayed in coverage statistics:

3. The characteristics of

  • Universal: support for both unit test coverage collection and manual test coverage collection; Support for both full coverage collection and DIFF coverage collection;

  • Non-invasive: the coverage data can be collected without any modification to the development code by using on-the-fly mode;

  • 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: Provides coverage reports in HTML format for high readability.

4. Architecture

5. How to use it

5.1 database installation and initialization

5.1.1 Install the mysql database. After creating the database, run the SQL /db. SQL file to create a table

5.2 compilation and packaging

5.2.2 Clone code to change database and Gitlab configuration in 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 Run MVN package-dmaven.test. skip=true to generate super-jaco.jar

▍ 5.3 deployment

5.3.1 Run 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) Start coverage collection URL: /cov/triggerUnitCover call method: POST argument (body passed) : {"uuid":"uuid","type":1,"gitUrl":"git@git","subModule":"","baseVersion":"master","nowVersion":"feature","envType":"-Ptes T "} Returned: {"code":200,"data":true," MSG ":" MSG "} Note :2) Obtain coverage result URL: /cov/getUnitCoverResult Call method: GET Parameter: uuid(String) Returned: {" code ": 200," data ": {" coverStatus" : 1, "errMsg" : "MSG", "lineCoverage" : 100.0, "branchCoverage" : 100.0, "logFile" : "the file Content ", "reportUrl" : "http://"}, "MSG" : "MSG"} remarks:Copy the code

5.4.2 Environment Coverage Interface

1) Start coverage collection URL: /cov/triggerEnvCov call method: POST parameter (body passed) : {" uuid ":" uuid ", "type" : 1, "gitUrl" : "git @ git", "subModule" : ""," baseVersion ":" master, "" nowVersion" : "feature", "Address", "127.0.0.1", "port" : "8088"} returns: {" code ": 200," data ": true," MSG ":" MSG "} remarks: The IP address and port are the IP address and port of the module deployment server, which are used when jacoo. exec is dumped. Copy org.jaco.agent-0.8.5-Runtime. jar to the /home/xxx/ directory of the server in advance. - javaagent: / home/XXX/org. Jacoco. Agent - 0.8.5 - runtime. Jar = includes = *, output = tcpserver, address = *, port = 18513 (2) to obtain coverage results URL: /cov/getEnvCoverResult Call method: GET Parameter: uuid(String) Returned: {" code ": 200," data ": {" coverStatus" : 1, "errMsg" : "MSG", "lineCoverage" : 100.0, "branchCoverage" : 100.0, "logFile" : "the file Content ", "reportUrl" : "http://"}, "MSG" : "MSG"} remarks:Copy the code

6. Summary

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 conducive to the promotion and development of accurate testing. Welcome to join us!

7. GitHub project address

Github.com/didi/super-…

8. Open source teams

The team members are all from didi car service technology team

Content editing | Teeo

Contact us | [email protected]

Welcome to search “Didi Technology” on wechat and follow the public account of the same name to get more technical dry goods.