Java Code Coverage Tool jacoco-principle
1.2 Java Coverage Tool introduction
1.3.3 Apache maven Way
1.3.4 Eclipse Ecldmma Plugin mode
Java Code Coverage Tool jacoco-Practice Chapter
I. Introduction to the use of coverage items
1.5 perform tests, collect coverage results files
Modification of 1.5.1AndroidManifest files
1.5.2 Build Coverage apk tool and Jacoco-cov-sdk.jar pack
Ii. combination of coverage and BVT testing
2.1 Inserting coverage methods in the BVT use case framework
2.2 Execute BVT use case, get coverage
2.3 Generate coverage reports in batches, resolve inbound
2.4 Analyze coverage results to derive use case and code mapping relationships
Iii. differential coverage and full-scale coverage
Java Code Coverage Tool jacoco-stepping on the pit
1.1 Coverage pack fails to install on some phones 6.0
1.2 Coverage package in section 4. Failed to generate EC file on X version of phone
1.3 Coverage report does not see the source coverage after it is generated
Ii. coverage of some areas needing attention
Java Code Coverage Tool jacoco-principle 1.1 coverage definition
As a tester, to ensure that the software quality of the product is the primary goal of its work, in order to this goal, testers often through a number of means or tools to ensure that coverage is one of the more important link.
We typically divide the test coverage into two parts, namely "requirement coverage" and "code coverage."
Requirements coverage: Refers to the needs of testers to understand the level, according to the requirements of testability to split into sub-demand points, to write the corresponding test cases, and finally establish a mapping between requirements and use cases, with the test results of use cases to verify the implementation of requirements, 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, we need to take into account the input and output of each function, the execution of the logical branch code, this time our test execution is measured in code coverage, can be understood as white box coverage.
Both of these can be mutually reinforcing, with the code covering the results in reverse to check whether the requirements coverage (use case) test is fully complete.
What if we do coverage testing? We can use a number of popular online coverage tools, this chapter mainly introduces Jacoco this tool.
1.2 Java Coverage Tool introduction
Java main code coverage tools on the market: EMMA, Jacoco
Summarize the individual's understanding of Jacoco advantages:
(1) Jacoco supports branch coverage and introduces agent mode.
(2) Emma's official website has not been maintained, Jacoco is developed by its team, can be understood as an upgrade version.
(3) The Jacoco community is more active, the official website also maintains the update unceasingly.
Our early use of Emma, also did the full amount, the difference coverage, and the precise coupling also combined together, but later considered the advantage of Jacoco, also all switched over.
Brief introduction of 1.2.1 Jacoco
Jacoco is an open source coverage tool (official website address: http://www.eclemma.org/JaCoCo/), its development language is Java, its use is very 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 more.
Many third-party tools provide integration of JACOCO, such as Sonar, Jenkins, and so on.
The Jacoco contains coverage counters of various scales, including instruction-level overrides (Instructions,c0coverage), Branch (branches,c1coverage), cyclomatic complexity (cyclomaticcomplexity), Row overlay ( Lines), method overrides (Non-abstract methods), class overrides (classes), are described later.
Let's take a look at the coverage results as shown in 1-1, which makes it easy for the reader to have a general understanding first.
Figure 1-1 Coverage report Results section
Marked green for row coverage full, marked red for the lines that are not covered, yellow diamonds for branches partially covered, green diamonds for branches completely covered.
The results of this report will allow us to know the actual execution of the code, so that we can analyze the evaluation results.
1.2.2 Jacoco Basic Concepts
Line coverage: Measures whether each line of code for the program being tested is executed, and whether at least one instruction in the standard row is executed.
Class Coverage: The measure calculates whether the class file is executed.
Branch coverage: Measures the branch coverage of if and switch statements, calculates the total number of points within a method, and determines the number of branches that are executed and not executed.
Method Coverage: Measure the method execution of the program under test and whether execution depends on whether or not at least one of the instructions in the method is executed.
Instruction overlay: The count unit is a single Java binary Code directive, and the instruction coverage provides information about whether the code is executed and measures the fully independent source format.
Cyclomatic complexity: In the (linear) combination, the minimum number of all possible paths in a method is calculated, and the missing complexity also indicates that the test case is not fully covered by the module.
1.2.3 Jacoco principle
1. Introduction of Injection method
This diagram contains several different methods for collecting coverage information, each of which is not the same, and the color part is the place where Jacoco is more distinctive.
Above each rank meaning (with color for Jacoco support):
The above table Jacoco supported sections, explained in detail below:
(1) Jacoco the ASM technique used in byte code to modify the bytecode method, you can modify the jar file, class file bytecode file.
(2) Jacoco supports both On-the-fly and offline.
On-the-fly Insert Pile:
The JVM uses the-javaagent parameter to specify a specific jar file to start the instrumentation agent, the agent before loading a class through class loader to determine whether to convert the modified class file, the statistical code into class, Test coverage analysis can be done during the JVM execution of the test code.
Offline Mode:
Before testing, the file is inserted into the pile, and then generated by the pile of class or jar package, after testing the pile's class and jar package, will generate dynamic overwrite information to the file, finally unified the coverage information processing, and generate reports.
On-the-fly and offline Comparison:
The On-the-fly mode makes it easier to do code coverage analysis without having to advance bytecode insertion, regardless of the classpath setting.
The following conditions are not suitable for on-the-fly, and need to use offline in advance to the bytecode insertion pile:
(1) The operating environment does not support Java agents.
(2) The deployment environment does not allow you to set JVM parameters.
(3) Bytecode needs to be converted to other virtual machines such as Android Dalvik VMs.
(4) Dynamic modification of bytecode during the conflict with other agents.
(5) Unable to customize user load class.
2.JaCoCo executes the smallest Java version
Minimum required Java1.5
3. Byte code processing method
Jacoco modifies and generates Java bytecode by injection, using an ASM library.
4.java Method Control Flow Analysis
How is the Jacoco injected into the byte code?
We look at the following with questions:
First, for example, there is a Java method:
After compiling the code into bytecode, the content is as follows:
We know that Jacoco is a byte-code injection method, which is injected through a probe probe, as follows:
The probe is a byte instruction set inserted into a Java method, which can be logged after the program executes, and it does not alter the behavior of the original code.
Let's look at the insertion comparison before and after the probe:
The color part is where the probe is injected.
The Jacoco is based on the control flow type to adopt different probe insertion strategies.
The control flow graph of a Java method defined with Java bytecode may have the following type, each of which is connected to a source directive and a target instruction, and the injection strategy of the different probe type is different, as follows:
The probe does not change the behavior of the method, but records the fact that they have been executed, theoretically, a probe can be inserted on each side of the control flow graph, as the probe implementation itself requires multiple bytecode instructions, which will increase the size and execution speed of the class file several times.
In fact, only a few probes are required, depending on the method of control flow for each method, the following shows how to add additional instructions in the case of different edge types:
A instrumented class can retrieve an instance of its probe array with the following code:
Jacoco is a Boolean array that implements the probe, with each probe corresponding to the item in the array. When the following four bytecode instructions are triggered, the probe input is set to true:
Jacoco the line probe is handled this way, when adding an additional probe between two lines of instruction, the subsequent line contains at least one method call.
The above is the Jacoco interpolation pile principle, if you want to know more, you can go to see its source code implementation.
1.3 Jacoco How to use
There are many ways to use, the corresponding reference links posted here, according to the different projects can be flexible for the needs of readers to learn.
1.3.1 Apache ant Mode
See http://eclemma.org/jacoco/trunk/doc/ant.html
Mainly has the following several, the specific use does not introduce, the application Bao is uses this way, the follow-up has introduced.
Task coverage, Task agent, Task Dump, Task Merge, Task report, task instrument
1.3.2 Command line mode
See http://www.eclemma.org/jacoco/trunk/doc/agent.html
Usage Description:
Mainly in the java_opts, for example:
It is loaded by the Getvmargument method of Agentoptions, and the corresponding parameters of each parameter are entered into agentoptions for subsequent operation.
Here is a description of all the parameters of the official website:
The system dumps the coverage information when the JVM is stopped.
The key core code here, Agent.java in a piece of code
Runtime.getruntime (). Addshutdownhook This method means to add a closed hook to the JVM, and when the JVM shuts down, it executes all the hooks that have been set in the system by means of Addshutdownhook. The JVM shuts down when the system finishes executing these hooks. So these hooks can be used for memory cleanup, object destruction and so on when the JVM shuts down.
That is, when the JVM shuts down, call Agent.shutdown (), which is write coverage data.
1.3.3 Apache maven Way
See http://www.eclemma.org/jacoco/trunk/doc/maven.html
This approach is suitable for MAVEN projects.
Here is a simple way to invoke the principle:
Take the official offline example, which reads as follows:
Note The blue part, the above configuration mainly does the following things:
(1) The project has been packaged in jar packages and introduced into JUnit and Jacoco.
(2) When build is executed instrument, report, check.
(3) Coverage generation to Target/jacoco.exec
Let's see how he triggers the call.
In the Jacoco Source: jacoco-maven-plugin\target\classes\meta-inf\maven\org.jacoco\ There is a plugin-help.xml file in the Jacoco-maven-plugin directory, which indicates the specific method of invocation.
Cut out the instrument, the key place is the blue part below.
Description of the parameters on the official website:
Give a sorted table:
Give a jacoco to the MAVEN part of the code directory:
Here, you should know how to call it.
1.3.4 Eclipse Ecldmma Plugin mode
The steps are as follows:
(1) In the Eclipse menu, select Help→install New software ...
(2) In the Installation box, enter http://update.eclemma.org/, tick the version that appears.
(3) Check the version and click Next.
(4) Complete the installation according to the wizard.
(5) The use is not said.
1.3.5 and Jekins Integration
(1) to install the Jacoco plugin on Jenkins First, you can add this option (1-2) to the job configuration item after installation is complete:
Figure 1-2
(2) The selection appears after (Figure 1-3):
Figure 1-3
The first entry box is your coverage file (EXEC), the second is the class file directory, and the third is the source code file directory.
(3) After the configuration has been built, the job home page will appear after the completion of the trend map coverage (Figure 1-4), the mouse click on the trend chart can see the coverage details (Figure 1-5), including specific coverage data and source coverage:
Figure 1-4 Trend Chart
Figure 1-5 Coverage Details
Not to be continued:
Jacoco principle of the introduction here, follow-up and project practice and stepping on the pit, the practice of the main introduction of Jacoco in the actual operation of the use of the situation, the pit contains a few of the more difficult problems encountered at the time of the solution, interested children shoes please pay attention to.
Java Code Coverage Tool jacoco-principle