Authors
- Jithin Paul ([email protected])
- Nandita Balasubramanian ([email protected])
Testwell CTC is a tool for code coverage checking originally from Testwell which further acquired by Verifysoft Technology GmbH for C and C code. It can also be used for C# and Java programs. Apart from all other code coverage tools, CTC is used to measure function execution costs. Java Code Coverage Tools has 9 repositories available. Follow their code on GitHub.
University of Texas at Dallas (UTD)
March 2017
An Automated coverage collection tool that can capture the statement coverage for the program under test.
The tool uses ASM byte-code manipulation framework to manipulate the bytecode. Bytecode manipulation is performed on the fly by aJava Agent whcih makes use of the Intrumentation API. A JUnit listener is used to to capture the start and end events for each JUnittest method. The agent jar file and the JUnit listener class can be integrated with any maven project to perform code coverage. This is done by updating the pom.xml file present in the maven project rool directory.
Following needs to be added to the pom.xml file.
- Replace [path-to-your-agent.jar] with your java agent jar’s absolute path, andreplace [YourListener] with your JUnit listener’s full name.
- Add the agent.jar file as a dependency as it is used by the 'Listener.java' file.(The following dependency was written for my 'JPAgent.jar' file which is attachedin the repository for your reference)
- Add the asm package as a dependency if needed as it is used by multiple files.
- Add the junit package as a dependency if needed.
When firing “mvn test”, tool will output a file named “stmt-cov.txt” under the project under test. This file will includestatement coverage information for each test method. Each line in the file represents a test method or a covered statement, while the testmethod line will have the [TEST] prefix. Each test method will be followed by the set of statements covered by it.Each test method isrepresented by the full name of the test class +”:”+the test method name, while each statement is represented by thefull name of the class +”:”+the line number.
PS:- A 'JPAgent.jar' file has been attached for reference. This jar file includes MethodPrinter.class, ClassPrinter.class, MyClassFileTransformer.class, StatementCoverageData.class and Agent.class
- Make sure that you have created the necessary run/debug configuration for your tests.You can also run the necessary tests to generate a temporary run configuration that you can later modify and save.
- Select the correct configuration is selected in the list on the toolbar and click , or select Run | Run ... with Coverage from the main menu.
- (If the Show options before applying coverage to the editor option is enabled) The IDE prompts you to select whether you want to replace the active coverage suites, add the collected data to the active suites, or not to apply coverage data.Select whether you want to append the results of multiple runs, and click OK.
Read the code coverage report
Code coverage results are displayed in the Coverage tool window, in the Project tool window, and in the editor after you run at least one test with coverage.
Coverage results in the tool windows
The Project tool window displays the percentage of the covered classes and lines for directories and the percentage of the covered methods and lines for classes.
The Code Coverage tool windows appears right after you run a test with coverage and displays the coverage report. If you want to reopen the Coverage tool window, select Run | Show Code Coverage Data from the main menu, or press Ctrl+Alt+F6.
The report shows the percentage of the code that has been covered by the tests. You can see the coverage result for classes, methods, and lines.
Branches coverage shows the percent of executed branches in the source code (normally, these are the
if
/else
and switch
statements). This information is available for the JaCoCo runner and for the IntelliJ IDEA runner if you enable the Tracing option. If you want to jump to the source code in the editor, right-click the necessary class in the Code Coverage tool window and select Jump to SourceF4.
Item | Description |
---|---|
Go up one level. | |
Show all the packages on the same level. | |
When this button is pressed, source code of the class selected in the tool window, automatically opens in a separate editor tab, and gains the focus. | |
When this button is pressed, when source code of certain class gets the focus in the editor, the corresponding node is automatically highlighted in the tool window. | |
Generate a code coverage report and save it to the specified directory. Refer to section Save coverage data to a file for details. |
Coverage results in the editor
In the editor, lines of code are highlighted with regard to its code coverage status in the gutter:
- Green: lines that have been executed during simulation
- Red: lines that haven't been executed during simulation
- Yellow: lines covered with conditions tracing mode
To find out how many times a line has been run, click the color indicator in the gutter. The popup that opens shows the statistic for the line at caret. For lines with conditions, the popup also provides statistics.
For JUnit tests, you can open the test that covers the line in a separate dialog. To do so, click the in the popup. This option is available in the Tracing mode, and with the Track per test coverage checkbox selected for the current run/debug configuration on the Code Coverage tab.
Click to open the bytecode of the current class in a separate dialog, or to change the colors of coverage indicators in the gutter.
If you want to see how it works
Watch this video if you want to understand code coverage in IntelliJ IDEA better.
Last modified: 24 December 2019