Run tests with coverage
-
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 in the list on the toolbar and click , or select
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 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.
Code Coverage tool window options
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.