Skip to content

Getting started

Daniel Palme edited this page Mar 19, 2022 · 22 revisions

General

In general you need a tool that instruments your test code and collects coverage information.
This information is typically stored in a XML file (e.g. in Cobertura format).
ReportGenerator uses this file and generates a report in HTML format (other formats are available).

Use the online configuration tool to get started quickly.

.NET

For .NET you can use coverlet or altcover for instrumenting your test code.

After adding the dependencies to your project, you can execute your tests and generate the coverage report.

Add dependencies to your *.csproj file:

<ItemGroup>
  <PackageReference Include="coverlet.collector" Version="3.1.0">
    <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    <PrivateAssets>all</PrivateAssets>
  </PackageReference>
  <PackageReference Include="ReportGenerator" Version="5.1.2" />
  <PackageReference Include="xunit" Version="2.4.1" />
  <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
    <PrivateAssets>all</PrivateAssets>
    <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
  </PackageReference>
</ItemGroup>

Execute tests and create coverage report

dotnet test --collect:"XPlat Code Coverage"

"%UserProfile%\.nuget\packages\reportgenerator\5.1.2\tools\net6.0\ReportGenerator.exe" -reports:*\TestResults\*\coverage.cobertura.xml -reporttypes:Html;TeamCitySummary -targetdir:coveragereport

Java

For Java you can use JaCoCo for instrumenting your test code. After adding JaCoCo to your project (here with Maven), you can execute your tests and generate the coverage report.

Add dependencies to your pom.xml file:

<project>
  <build>
    <plugins>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.6</version>
        <executions>
          <execution>
            <goals>
              <goal>prepare-agent</goal>
            </goals>
          </execution>
          <execution>
            <id>report</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>report</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <reporting>
    <plugins>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <reportSets>
          <reportSet>
            <reports>
              <report>report</report>
            </reports>
          </reportSet>
        </reportSets>
      </plugin>
    </plugins>
  </reporting>
</project>

Execute tests and create coverage report

mvn test jacoco:report

dotnet tool update dontnet-reportgenerator-globaltool --tool-path tools --version 5.1.2
tools\reportgenerator -reports:target\site\jacoco\jacoco.xml -targetdir:coveragereport -sourcedirs:src\main\java

NodeJS

For NodeJS you can use Istanbul for instrumenting your test code. After installing Istanbul, you can execute your tests and generate the coverage report.

npm i nyc --save-dev
nyc --reporter=coberatura mocha

dotnet tool update dontnet-reportgenerator-globaltool --tool-path tools --version 5.1.2
tools\reportgenerator -reports:coverage/cobertura-coverage.xml -targetdir:coveragereport
Clone this wiki locally