Collecting single code coverage report for multiple dotnet core projects

Hardik Goel
2 min readSep 13, 2018

--

The problem statement is — I have a dotnet core solution with more than 1 projects (.csproj files) and hence very likely more than 1 test projects. And I need a way to generate a single code coverage report for the whole solution. Also, I am using dotnet test to run my tests so I would prefer a solution that works well with that. Here is what I did.

Based on some research online, I found the easiest and best tool to use for this is coverlet. From the github page-

Coverlet is a cross platform code coverage library for .NET Core, with support for line, branch and method coverage.

And it works well with dotnet test! I installed the command line tool

dotnet tool install --global coverlet.console

Then it’s pretty straightforward to generate coverage report for 1 dotnet core project, based on the README on Coverlet github page.

coverlet /path/to/test-assembly.dll --target "dotnet" --targetargs "test /path/to/test-project --no-build"

But the above only takes in the path to one test dll and hence generates one output file. To be able to merge multiple coverages into one, we need to use the --merge-with option.

coverlet <ASSEMBLY> --target <TARGET> --targetargs <TARGETARGS> --merge-with "/path/to/existingCoverage.json"

The above command merges the current calculated coverage with an existing coverage file. This existing coverage file must be in coverlet’s json format. So I start with one project’s json coverage report and keep merging with generated coverages of other projects using the above command. In the end I have one coverage json file with all the coverage.

In my case, I wanted the final coverage in the cobertura format so that I could display it in VSTS. So I ran the above command once more for the last project with the extra option --format cobertura. This gave me one final xml file in covertura format that VSTS could use to display code coverage.

If we want to go further and generate a browsable html file that allows us to explore the coverage of the source code interactively, we can use the reportgenerator tool. This tool generates html files from our cobertura xml. Again, I installed the cli tool

dotnet tool install --global dotnet-reportgenerator-globaltool --version 4.0.0-rc4

And used the following command to generate the html files

reportgenerator -reports:/path/to/coverage.xml -targetdir:/path/to/output/directory -reportTypes:htmlInline

This generates a bunch of htmls in the specified output directory. index.htm can be used to browse the coverage or the directory itself can be passed to VSTS to allow it to display the code coverage in the build page.

--

--