Code coverage
Code coverage in IntelliJ IDEA allows you to see the extent to which your code has been executed. It also lets you verify the extent to which your code is covered by unit tests, so that you can estimate how effective these tests are.

Code coverage is supported only for the classes and sources that belong to your current project.
The following code coverage runners are available in IntelliJ IDEA:
- IntelliJ IDEA code coverage runner (recommended)
- JaCoCo
Read in this section:
- Configure coverage
- Run with coverage
- Manage coverage suites
- Save coverage data to a file
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.

- Go to 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.
- Open the Code Coverage tab and select a code coverage runner from the Choose coverage runner list: IntelliJ IDEA or JaCoCo .
- (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.
- 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 , then click Add Class or Add Package , and select the necessary items.
- To collect code coverage statistics for tests, select the Enable coverage in test folders checkbox.
- Go to 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.

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

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

- 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.
- Select the configuration from 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 skip applying coverage data. Select whether you want to append the results of multiple runs and click OK .
Как узнать процент покрытия кода тестами JUnit в IntelliJ IDEA?

В правом боку «Editor» откроется таблица в которой показано покрытие кода в процентах и внизу появится пункт «Coverage».
Отслеживать
ответ дан 2 июл 2017 в 11:35
1,250 9 9 серебряных знаков 17 17 бронзовых знаков
Edit Configurations -> Add new configuration -> JUnit

Test kind: All in package и в поле package выберите корневой пакет проекта (Если вам нужно прогнать все тесты)

На закладке «Code Coverage» выберите корневой пакет проекта (опять же, если надо прогнать все тесты).

Конфигурацию настроили. Её необходимо запускать кнопкой «Run with Coverage» (см. картинку ниже). В результате вы получите процент покрытия, а в подарок увидите детализацию, какие именно строки покрыты, а какие — нет.
About the Java Hands-on Labs
Azure DevOps Services, Azure DevOps Server, and Team Foundation Server (TFS) provide an integrated set of services and tools to manage your software projects, from planning and development through testing and deployment to speed the development and delivery of your software applications across platforms, including iOS, Android, Java, Linux or Windows.
These hands-on labs are provided to give you a first-hand, technical experience on how you can leverage the Microsoft DevOps platform for Java development. The labs cover:
- Creating a new organization in Azure DevOps
- Using the Agile Boards to plan and track work items
- Using Azure DevOps Services with your Eclipse and IntelliJ
- Running Junit tests and analyzing code coverage with Jacoco and Cobertura
- Continuous Integration with Azure Pipelines or Jenkins
- Deploying Docker containers to Azure with an Automated delivery pipeline
Virtual Machine
Our partner NorthWest Cadence has built a virtual machine image that is pre-configured with all the software you require to run through the labs. You can find instructions on provisioning and connecting to the virtual machine here.
Target Audience
The image and the accompanying hand-on-labs is for a technical audience. As such, familiarity with Azure DevOps Services, Java and Linux operating system would be preferred although it is not a strict prerequisite.
Provisioning a project using Azure DevOps Demo Generator
You can use Azure DevOps Demo Generator to provision a project with pre-defined data on to your organization to follow the hands-on-labs. Choose MyShuttle2 template from the dropdown.
Hands on Labs
The labs should be followed in the following order, though there are some equivalent labs that allow you a «choose your adventure» experience:
| Lab Name |
|---|
| Setting up a new project on Azure DevOps Services |
| Creating an Agile workflow |
| Set up a Docker build agent |
| Cloning an Azure DevOps Services Repo — IntelliJ |
| Cloning an Azure DevOps Services Repo — Eclipse |
| Maven Package Management with Azure DevOps Services and Jenkins |
| Maven Package Management with Azure Pipelines |
| Build Docker containers with Azure DevOps Services |
| Release Management with Azure DevOps Services |
| End to End Workflow — IntelliJ |
| End to End Workflow — Eclipse |