Principles of Jacoco (interpretation of each coverage)

Source: Internet
Author: User

Coverage counter

Jacoco uses a series of different counters to measure calculation of coverage. All of these counters get information from the Java class file, which can optionally contain debugging information inside. This method can measure and analyze the application in real time, even without the source code. In most cases, the information collected can be mapped to the source code, and visualized to the granularity of each line. But there are some limitations to this approach. These class files must be compiled with debug information in order to calculate the line coverage and provide source highlighting. But not all of the Java language constructs can be compiled directly into consistent binary code. In this case, the Java compiler creates the so-called "composition" code, which can result in some undesirable coverage results.

Directive (C0 Coverage)

Jacoco the smallest count unit is a single Java binary code directive. The instruction coverage provides information about whether the code is executed. This metric is completely independent of the source format and is always available, even if there is no debug information in the class file.

Branch (C1 Coverage)

Jacoco also calculates the coverage of the branch, including all the IF and switch statements. This metric calculates the total number of points in a method, and determines how many branches are executed and not executed. Branch coverage is always available, even if there is no debug information in the class file. Note that exception handling is not counted in the branch metrics.

If the class file is compiled with debug information, the resulting coverage can be mapped to the source line and highlighted:

    • No overrides: No branches are executed in this row (red squares)
    • Partial overrides: Only a subset of the branches of this line are executed (yellow squares)
    • Full overwrite: All branches of this line are executed (green squares)
Cyclomatic complexity

Jacoco can also compute complexity for each non-abstract method, ultimately calculating the complexity of classes, packages, and groups. According to the definition of McCabe1996 cyclomatic complexity, in the (linear) combination, the minimum number of all possible paths within a method is computed. Therefore, complexity can be used as a basis for measuring whether a unit is fully covering all scenarios. Complexity can be computed even when there is no debug information.

The formal definition of cyclomatic complexity V (G) is a forward graph representation of the control flow graph based on the method:

V (G) = E–n + 2

E is the number of boundaries, and N is the number of nodes. Jacoco calculates the complexity based on the following equation, B is the number of branches, and D is the number of decision points:

V (G) = b–d + 1

Based on the coverage of each branch, Jacoco also calculates the complexity of coverage and missing for each method. The missing complexity also means that the test case is not completely covered by this module. Note that Jacoco does not use exception handling as a branch, and Try/catch blocks also do not add complexity.

Yes

After all class files have been compiled with debug information, you can calculate the coverage information for the rows. Whether a line of source code is executed depends on whether at least one instruction is executed in this line.

Because the actual line of code is generally compiled into multiple binary code directives, so that the source is highlighted, it will be displayed in 3 different states:

    • No overrides: No instruction is executed in this line (red background)
    • Partial overrides: Only a subset of the instructions are executed in this line (yellow background)
    • Full overwrite: All instructions in this line are overwritten (green background
Method

Each non-abstract method consists of at least one instruction. Whether a method executes depends on whether at least one of the instructions in the method is executed. In Jacoco, constructors and static initialization are also counted as methods. Some of these methods may not have direct corresponding source code, such as the default constructor or the initialization of a constant command.

Class

Whether or not a method executes depends on whether there is at least one method in the class being executed. Note Jacoco that constructors and static initialization are all methods. Java interfaces generally contain static initialization, so the interface is also considered an executable class.

Principles of Jacoco (interpretation of each coverage)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.