Java Code Coverage Tool jacoco-Principle simple analysis _ continuous Integration CI

Source: Internet
Author: User

As a tester, to ensure that the product software quality is its primary goal, in order to this goal, testers often through a lot of means or tools to ensure that coverage is one of the more important link.

We typically divide test coverage into two parts, which are "requirements coverage" and "code coverage."

Requirements coverage: Refers to the tester's understanding of the requirements, according to the requirements of testability to split into the various sub-demand points, to write the appropriate test cases, and finally establish a demand and use case mapping relationship to the use case test results to verify the requirements of the implementation, can be understood as black box coverage.

Code coverage: For more comprehensive coverage, we may also need to understand the logic of the program under test, need to take into account the input and output of each function, the implementation of logical Branch code, this time our test performance is measured by code coverage, can be understood as white box coverage.

The above two can complement each other, using code coverage results to reverse check the requirements coverage (use case) test is fully complete.

If you do coverage testing. We can use a number of online popular coverage tools, this chapter mainly introduces Jacoco this tool.

A comparative analysis of Emma and Jacoco:

Java main code coverage tools on the market: EMMA, Jacoco

Summarize the individual's understanding of the Jacoco advantage:

(1) Jacoco supports branch coverage and introduces agent mode.

(2) Emma's official website has not maintained, Jacoco is its team developed, can be understood as an upgraded version.

(3) Jacoco community is more active, the official website is also in constant maintenance update.

Jacoco is an open source Coverage tool (website address: http://www.eclemma.org/JaCoCo/), the development language is Java, its use is flexible, can be embedded in ant, maven; can be used as an Eclipse plug-in, You can use its javaagent technology to monitor Java programs, and so on.

############################# #如果你不了解什么是java agent, followed by the introduction, or directly skip ####################################

Javaagent is introduced after JDK 1.5, or it can be called a Java proxy.

Javaagent is the interceptor that runs before the main method, its default method named Premain, which means that the Premain method is executed first and then the main method is executed.

So how to achieve a javaagent it. Very simple, only need to add Premain method.

Look at the following code and the comment description in the code:

Package com.shanhy.demo.agent;

Import java.lang.instrument.Instrumentation; /** * My Java Proxy * * @author Tan Hongyu (365384722) * @myblog http://blog.csdn.net/catoop/* @create March 30, 2016/Publi C class Myagent {/** * This method runs before the main method, runs in the same JVM as the main method * and is loaded with the same system ClassLoader * Unified security Policy (Securi Ty policy) and context management * * @param agentops * @param inst * @author shanhy * @create March 2016 30 Day/public static void Premain (String agentops, Instrumentation inst) {System.out.println ("=========pre
        Main method executes ======== ");
    System.out.println (Agentops);  /** * If no premain (string agentops, Instrumentation Inst) * is executed Premain (string agentops) * * @param agentops * @author shanhy * @create March 30, 2016/public static void Premain (String agentops)
        {System.out.println ("=========premain method executes 2========");
    System.out.println (Agentops); }
}
The 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 The 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 A. 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 The 27 28 29 30 31 32 33 34 35 36 37 38 39 40

After writing this class, we also need to do a step-by-step configuration work.

Add meta-inf/manifest under the SRC directory. MF file, the content is defined as follows:

manifest-version:1.0
premain-class:com.shanhy.demo.agent.myagent
can-redefine-classes:true
1 2 3 4 1 2 3 4

Pay special attention to four lines, line fourth is blank, and a space after the colon, as shown in the following screenshot:

Then we pack the code for Myagent.jar

Note that when packing, choose our own defined MANIFEST. Mf

We then create a main program project with the main method, with screenshots as follows:

Then package the main program as Myapp.jar

How to perform Myagent.jar. We use the-javaagent parameter to specify our Java Agent package, it is worth saying that the number of-javaagent this parameter is not limited, if more than one specified, will be executed in accordance with the specified successively, after the execution of each agent, will be the main program to execute the major method.

The order is as follows:

Java-javaagent:g:\myagent.jar=hello1-javaagent:g:\myagent.jar=hello2-javaagent:g:\myagent.jar=hello3-jar Myapp.jar
1 1

Output results:

G:\>java-javaagent:g:\myagent.jar=hello1-javaagent:g:\myagent.jar=hello2-javaagent:g:\myagent.jar=hello3- Jar Myapp.jar
=========premain method executes ========
Hello1 =========premain
method Execution ========
Hello2
====premain method executes ========
Hello3
=========main method Execution ========
1 2 3 4 5 6 7 8 1 2 3 4 5 6-7 8

Special reminder: If you put the-javaagent behind the-jar, it will not take effect. In other words, the agent placed behind the main program is invalid.

such as execution:

Java-javaagent:g:\myagent.jar=hello1-javaagent:g:\myagent.jar=hello2-jar myapp.jar-javaagent:g:\myagent.jar= Hello3
1 1

There will only be a previous entry into force, and the third one is invalid.
Output results:

G:\>java-javaagent:g:\myagent.jar=hello1-javaagent:g:\myagent.jar=hello2-jar myapp.jar-javaagent:g:\ Myagent.jar=hello3
=========premain method executes ========
Hello1 =========premain
method execution ========
Hello2
=========main Method Executes ========
1 2 3 4 5 6 1 2 3 4 5-6

The Hello1 in the command is the string argument passed to the Premain method.

At this point, we will use the javaagent, but only to see the effect of this operation, seems to have no practical significance.

What can we do with javaagent? In the next article, we'll describe how to apply javaagent to projects.

Finally, there is also a way to execute the agent after the main method is executed, because it is not commonly used, and the main program needs to configure Agent-class, so it is not commonly used, if you need to understand the next Agentmain (String Agentargs, Instrumentation Inst) method.

#################################################### Java Agent Introduction End ############################################# ########


Many Third-party tools provide integration of JACOCO, such as Sonar, Jenkins, and so on.
Jacoco includes a wide range of scale coverage counters, including instruction-level coverage (Instructions,c0coverage), branching (branches,c1coverage), cyclomatic complexity (cyclomaticcomplexity), row coverage ( Lines), Method Overlay (Non-abstract methods), class overlay (classes).

Line coverage: Measures whether every line of code in the program being tested is executed, and whether at least one instruction in the standard line is executed.

Class Coverage: The metric calculates whether the class file is executed.

Branch coverage: Measures the branch coverage of the IF and switch statements, and calculates a method inside the

Total number of branches to determine the number of branch execution and not performed.

Method coverage: The measurement of the method execution of the program being tested depends on whether or not at least one of the instructions in the method is executed.

Instruction overlay: The counting unit is a single Java binary code directive, which provides information about whether the code is executed and measures a fully independent source format.

Cyclomatic complexity: In a (linear) combination, the minimum number of possible paths in a method is computed, and the missing complexity also indicates that the test case does not fully cover the module.


Jacoco principle:

1. Introduction of Injection Method

This diagram contains several different methods of collecting coverage information, and each method is implemented differently, with the color part being the place where Jacoco is more characteristic.

Above each rank meaning (with color for Jacoco support):

The Jacoco Support section above is explained in detail below:

(1) Jacoco the ASM technique used in byte code to modify the bytecode method, you can modify the jar file, the class file byte code file.

(2) Jacoco supports two types of on-the-fly and offline. On-the-fly Insert Pile:

In the JVM, a specific jar file is specified by the-javaagent parameter to start the instrumentation agent, and the agent can determine whether to transform the class file and insert the statistic code into class, before loading a class with class loader. Test coverage analysis can be done during the JVM's execution of the test code. Offline Mode:

Before testing, the file is inserted into the pile, and then generate the class or jar packet inserted in the pile, test the class and Jar packet inserted, will generate dynamic coverage information to the file, and finally unified coverage information processing, and generate reports.

Related Article

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.