Coverlet is a cross platform code coverage library for .NET Core, with support for line, branch and method coverage.
Available on NuGet
Visual Studio:
PM> Install-Package coverlet.msbuild
.NET Core CLI:
dotnet add package coverlet.msbuild
Coverlet integrates with the MSBuild system and that allows it to go through the following process:
- Locates the unit test assembly and selects all the referenced assemblies that have PDBs.
- Instruments the selected assemblies by inserting code to record sequence point hits to a temporary file.
- Restore the original non-instrumented assembly files.
- Read the recorded hits information from the temporary file.
- Generate the coverage result from the hits information and write it to a file.
Note: The assembly you'd like to get coverage for must be different from the assembly that contains the tests
Coverlet doesn't require any additional setup other than including the NuGet package. It integrates with the dotnet test
infrastructure built into the .NET Core CLI and when enabled, will automatically generate coverage results after tests are run.
Enabling code coverage is as simple as setting the CollectCoverage
property to true
dotnet test /p:CollectCoverage=true
After the above command is run, a coverage.json
file containing the results will be generated in the root directory of the test project. A summary of the results will also be displayed in the terminal.
Coverlet can generate coverage results in multiple formats, which is specified using the CoverletOutputFormat
property. For example, the following command emits coverage results in the opencover
format:
dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
Supported Formats:
- json (default)
- lcov
- opencover
- cobertura
The output folder of the coverage result file can also be specified using the CoverletOutputDirectory
property.
Coverlet allows you to specify a coverage threshold below which it fails the build. This allows you to enforce a minimum coverage percent on all changes to your project.
dotnet test /p:CollectCoverage=true /p:Threshold=80
The above command will automatically fail the build if the average code coverage of all instrumented modules falls below 80%.
You can ignore a method or an entire class from code coverage by creating and applying any of the following attributes:
- ExcludeFromCoverage
- ExcludeFromCoverageAttribute
Coverlet just uses the type name, so the attributes can be created under any namespace of your choosing.
- Filter modules to be instrumented
- Support for more output formats (e.g. JaCoCo)
- Console runner (removes the need for requiring a NuGet package)
If you find a bug or have a feature request, please report them at this repository's issues section. Contributions are highly welcome, however, except for very small changes, kindly file an issue and let's have a discussion before you open a pull request.
Clone this repo:
git clone https://github.com/tonerdo/coverlet
Change directory to repo root:
cd coverlet
Execute build script:
dotnet msbuild build.proj
This will result in the following:
- Restore all NuGet packages required for building
- Build and publish all projects. Final binaries are placed into
<repo_root>\build\<Configuration>
- Build and run tests
These steps must be followed before you attempt to open the solution in an IDE (e.g. Visual Studio, Rider) for all projects to be loaded successfully.
This project enforces a code of conduct in line with the contributor covenant. See CODE OF CONDUCT for details.
This project is licensed under the MIT license. See the LICENSE file for more info.