Code coverage java idea

IntelliJ IDEA

The IntelliJ IDEA coverage engine in TeamCity is the same engine that is used within IntelliJ IDEA to measure code coverage. This coverage attaches to the JVM as a Java agent and instruments classes on the fly when they are loaded by the JVM. In particular, it means that classes are not changed on the disk and can be safely used for distribution packages.

The IntelliJ IDEA coverage engine currently supports Class, Method, and Line coverage. There is no Branch/Block coverage yet.

Make sure your tests run in the fork=true mode. Otherwise, the coverage data may not be properly collected.

Note that IDEA coverage is not currently supported for Android projects built via Gradle. See the related feature request in our tracker.

To configure code coverage using IntelliJ IDEA engine, follow these steps:

  1. While creating/editing Build Configuration, go to the Build Step page.
  2. Select the Ant, IntelliJ IDEA Project, Gradle or Maven build runner.
  3. In the Code Coverage section, select IntelliJ IDEA as a coverage tool in the Choose coverage runner drop-down menu.
  4. Set up the coverage options — refer to the description of the available options below.

Specify Java packages for which code coverage will be gathered. Use new-line delimited patterns that start with a valid package name and contain * . For example, org.apache.* .

Classes to exclude from instrumentation

Use newline-separated patterns for fully qualified class names to be excluded from the coverage, for example: *Test . Exclude patterns have priority over include patterns.

Источник

Configure coverage

Code Coverage settings page

  1. Press Ctrl+Alt+S to open the IDE settings and select Build, Execution, Deployment | Coverage .
  2. Define how the collected coverage data will be processed:
    • Show options before applying coverage to the editor : show the Code Coverage dialog every time you run a new run configuration with code coverage.
    • Do not apply collected coverage : discard the new code coverage results.
    • Replace active suites with the new one : discard the active suites and use the new one every time you launch a new run configuration with code coverage.
    • Add to the active suites : add new code coverage suites to the active suites every time you launch a new run configuration with code coverage.
    • Ignore implicitly declared default constructors : exclude the implicitly declared default constructors from coverage statistics. No-arg constructors that are declared explicitly will remain included.
    • Ignore empty private constructors of utility classes : exclude from coverage statistics empty private constructors in classes where all other methods are static.
  3. Select the Activate Coverage View checkbox to open the Coverage tool window automatically.
Читайте также:  Как считать посимвольно питон

Change colors of the coverage highlighting

Configure code coverage colors

  1. Press Ctrl+Alt+S to open the IDE settings and select Editor | Color Scheme | General .
  2. Alternatively, click in the popup that opens on clicking the coverage indication line in the gutter.
  3. In the list of components, expand the Line Coverage node and select a type of coverage: for example, Full , Partial or Uncovered .
  4. Click the Foreground field to open the Select Color dialog.
  5. Select a color, apply the changes, and close the dialog.

Источник

Run with coverage

In IntelliJ IDEA, you can create multiple run/debug configurations and specify coverage options for each of them depending on your needs.

Run configurations are currently being redesigned: some configurations are updated and some are still in the old design. Use the procedure that corresponds to the appearance of the required configuration.

  1. From the main menu, select Run | Edit Configurations and click the necessary configuration on the left panel. If you haven’t created the required configuration yet, refer to the Create a run/debug configuration from a template procedure that will guide you through the process. Code Coverage tab in the Run/debug Configuration dialog
  2. Open the Code Coverage tab and select a code coverage runner from the Choose coverage runner list: IntelliJ IDEA or JaCoCo .
  3. (For the IntelliJ IDEA runner) select Branch coverage to collect information for all branches of if and switch statements. Additionally, enable the Track per test coverage option that allows tracking individual code coverage produced by each test case. Enable this option if you want to know exactly what lines of code have been covered by specific tests. This will let see which tests are the most relevant for each piece of the code.
  4. The Packages and classes to include in coverage data and Packages and classes to exclude from coverage data areas allow you to narrow down the code coverage scope. Click the Add button, then click Add Class or Add Package , and select the necessary items.
  5. To collect code coverage statistics for tests, select the Enable coverage in test folders checkbox.
  1. From the main menu, select Run | Edit Configurations and click the necessary configuration on the left panel. If you haven’t created the required configuration yet, refer to the Create a run/debug configuration from a template procedure that will guide you through the process. Code Coverage in the Run/debug Configuration dialog
  2. Click Modify options , select Coverage settings , and select the options that you want to configure:
    • Specify classes and packages : select this option to configure classes and packages for which you want to see the coverage information.
    • Exclude classes and packages : select this option to configure classes and packages that you want to exclude from coverage.
    • Specify alternative coverage runner : select this option to set a coverage runner: the IntelliJ IDEA runner or JaCoCo.
    • Use tracing : (only for the IntelliJ IDEA runner) tracing enables the accurate collection of the branch coverage with the ability to track tests, view coverage statistics, and get additional information on each covered line.
    • Collect coverage in test folders : collect code coverage statistics for tests.

    Code Coverage in the Run/debug Configuration dialog

Run a configuration with coverage

Code coverage is supported only for the classes and sources that belong to your current project.

Running a test with coverage

  1. Make sure that you have created the necessary run/debug configuration. You can also use a temporary run configuration that you can later modify, save, and rerun.
  2. Select the configuration from the list on the toolbar and click or select Run | Run . with Coverage from the main menu.
  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 skip applying coverage data. Select whether you want to append the results of multiple runs and click OK .

Источник

Read the 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 configuration with coverage.

Code coverage results

Results of the code coverage analysis are saved to the coverage folder in the IDE system directory.

Coverage in the Project tool window

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

Coverage in the Coverage tool window

The Code Coverage tool windows appears right after you run a configuration 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 executed or covered by tests. You can see the coverage result for classes, methods, and lines.

Branch coverage shows the percentage of the 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 with the Tracing option enabled.

Coverage results in the Coverage tool window

Code Coverage tool window options

Show all packages on the same level.

If this option is on, IntelliJ IDEA automatically opens the selected item in the editor. Otherwise, you need to double-click items to open them.

If this option is on, IntelliJ IDEA automatically locates in the tool window the files that you open in the editor.

Generate a code coverage report and save it to the specified directory. Refer to section Save coverage data to a file for details.

Filter coverage results. You can display only the classes with uncommitted changes to focus on recent updates or hide the classes that are fully covered with tests.

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 Source F4 .

Coverage results in the editor

In the editor, lines of code are highlighted with regard to their 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. A popup that opens shows the statistic for the line at caret. For the lines with conditions, the popup also provides statistics.

Coverage results shown in the editor

For JUnit tests, you can open the test that covers a line in a separate dialog. To do so, click the icon in the popup. To be able to use this feature, enable the Tracing mode and Track per test coverage options for the current run/debug configuration in the Code Coverage area. For more information, refer to Set coverage in run configurations.

Click to open the bytecode of the current class in a separate dialog, or to change the colors of the coverage indicators in the gutter.

Источник

Coverage

Use this page to configure how coverage data is collected and processed.

This page describes controls. For instructions on how to configure code coverage and run tests with coverage, refer to Configure coverage and Run with coverage.

When new coverage is gathered

These options control how collected coverage data is processed.

Show options before applying coverage to the editor

Show the Code Coverage dialog every time you launch a new run configuration with code coverage. The coverage options dialog is displayed when different coverage data has been produced.

Do Not Apply Collected Coverage

Discard new code coverage results.

Replace active suites with the new one

Discard active suites and use the new one every time you launch a new run configuration with code coverage.

Append new code coverage suite to the active suites every time you launch a new run configuration with code coverage.

Show the Coverage tool window when an application or test is run with coverage.

Java coverage

These options control which Java code elements should be excluded from coverage statistics.

Ignore implicit constructors

Exclude implicitly declared default constructors from coverage statistics. Default constructors that are declared explicitly will remain included.

Ignore empty private constructors of utility classes

Exclude from coverage statistics empty private constructors in classes where all other methods are static.

Источник

Оцените статью