IntelliJ IDEA 2020.1 Help

Run tests with coverage

  1. 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.

  2. Select the correct configuration in the list on the toolbar and click the Run with Coverage button, or select Run | Run ... with Coverage from the main menu.

    Running a test with coverage

  3. (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.

Code coverage results in the Project tool window

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.

Coverage results in the Coverage tool window

Code Coverage tool window options

Item

Description

the Up button

Go up one level.

the Flatten Packages button

Show all the packages on the same level.

the Navigate with Single Click button

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.

the Always Select Opened Element button

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.

the Generate report button

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.

Coverage results shown in the editor

For JUnit tests, you can open the test that covers the line in a separate dialog. To do so, click the the Show Tests Covering Line 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 the Show Bytecode button to open the bytecode of the current class in a separate dialog, or the Edit Coverage Colors button 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: 15 April 2020