Code coverage test summary:
Generally, there are three processes: plug-in, execution, and report.
Common statistical data for code coverage testing:
1. Row coverage
2. branch coverage rate
Code coverage:
1. Clearly know which codes have not been tested, and discuss with the developer what needs to be tested in the untested code
2. One of the important criteria for measuring the quality of Test Cases
How to view the code coverage statistical report:
1. Do not over-value statistics. Even if the coverage rate is 100%, this Code cannot be guaranteed to have no bugs.
Example:
Our code
Package com. vanward. Coverage. example01;
Public class pathcoverage {
Public String pathexample (Boolean condition ){
String value = NULL;
If (condition ){
Value = "" + condition + "";
}
Return Value. Trim ();
}
}
The test case:
When condition = true, execute this code
Test results of coverage rate:
Line coverage: 100%
Branch coverage: 100%
But when condition = false, our code throw exception.
2. We can focus on code with low coverage to supplement our case as needed, but we will not avoid excessively pursuing 100% reports.
The following describes how to use the coverage test tool.
1. Coverage tool for Java program: Emma
The reason why Emma was selected is that it is completely open-source and free, and it contains most of the features of the coverage tool without any support for extension Lib. I only learned about it.
1.1 Working Principle of Emma:
Emma tracks and records information about the code to be run by inserting bytecode into the. Class file.
1.2 Emma version
Currently, two versions are popular: 2.0 and 2.1.
The commands in 2.0 include:
-Bash-3.00 # Java Emma
Emma usage: Emma <command> [Command Options],
Where <command> is one:
Run application runner {same as 'emmarrun' tool };
Instr offline instrumentation processor;
Report offline Report Generator;
Merge offline data file merge processor.
{Use '<command>-H' To See Usage help for a given command}
[Emma V2.0, build 5312]
The commands in 2.1 include:
More than 2.0 CTL commands, which are mainly used to actively export coverage running data collected by Emma to a file; in version 2.0, coverage running data is exported to the file only when the program exits naturally or exits through Ctrl-C.
Here we will focus on 2.0
1.3 Emma supports two methods
1.3.1 on-the-fly
On the fly mode, adding bytecode to the loaded class is equivalent to replacing the original application Class Loader with the application Class Loader implemented by Emma.
1.3.2 offline
The offline mode adds bytecode before the class is loaded.
On the fly mode is more convenient and has obvious disadvantages. For example, it cannot generate a coverage report for Classes loaded by the boot class loader, you cannot generate a coverage report for a class that has a unique class loader like a J2EE container. In this case, we can turn to the offline mode.
Here, we will mainly introduce offlien Mode
1.4 statistical data types collected by Emma
Data collected by Emma includes class coverage, method coverage, block coverage, and row coverage. As a function test, we can focus on class coverage and method coverage. Block coverage and row coverage can be used as our reference.
1.5 working methods
Command Line and ant Methods
The command line method is introduced here.
1.6 Installation
It is easy to install. First, download the zip package of 2.0133 from http://emma.sourceforge.net/index.html, decompress it, and put emma.jar under jdkhome/JRE/lib/Ext.
1.7 command line
Generally, the command line can be completed in three steps: instr, run, and report)
Emma command line format: Emma command-Option
1.7.1 instr
This is the plug-in command, which is run before the program starts. We will find that after the plug-in, our class file is obviously increased. This is how Emma inserts the test code into our class file.
Its Parameters include:
-
-ip, -cp, -instrpath instrumentation path
...
-
SpecifyThe path of the plug-in. Multiple values are allowed. Each value is separated by a comma.
CP is used to specify a folder, and IP is used to specify a separate file or jar package
-
-d, -dir, -outdir directory
-
The location to store instrumented class files (in
fullcopy
Mode instrumented classes are stored in
destdir
/classes
And instrumented archives are stored in
destdir
/lib
Subdirectories, respectively). ignored if
-m
Is
overwrite
.
-
-out, -outfile metadata file
-
The location to store class coverage metadata (default: File
coverage.em
In the current directory). Neither particle file name nor extension are required.
-
-merge (y[es]
|n[o]
)
-
This flag indicates whether the metadata shocould be merged into the destination
-out
File, if any (default:
true
). Any existing data is clobbered otherwise.
-
-m, -outmode (copy
|overwrite
|fullcopy
)
-
Specifies the instrumentation output mode. Valid values for this property are:
copy
(Default): copy only instrumented class files and archive entriesdestdir
Directory.
overwrite
: Overwrite input class files and archives.
fullcopy
: Copy all (instrumented or not) class files and archivesdestdir
/classes
Anddestdir
/lib
, Respectively.
-
-ix, -filter filter patterns
...
It is used to specify the class inclusion and exclusion relationships. + Include,-exclude
Example:
Java Emma. Jar Emma instr-IP ../test. jar-M overwrite-Ix-Com. Test. *-dmetadata. Out. File =/test. Em
1.7.2 run our program. After the program exits, coverage is generated in the current directory. EC file. This file is the file used to store coverage running data. We need *. em and *. EC (metadata and coverage running data) files to Generate Reports
1.7.3 report
Its Parameters include:
-in, -input meta/coverage data files
...
Specify the metadata and coverage Data Directories
-
-r, -report (txt
|html
|xml
)
...
-
Report generation type
-
-sp, -sourcepath list of source directories
...
-
Specifies the path of the Java file. This will play a role in subsequent HTML reports. It links to the corresponding Java file and uses color to identify which statements have been executed and which statements have not been executed.