Your dev’s say they want to see the code coverage reports in Azure Devops…and it must be easy because Visual Studio “just does it”…
Sounds simple…everything is an MS product - Azure Devops, Visual Studio, .Net Core, what could possibly cause issues…
So you want to be able to see the results when your pipeline runs, Note the two tabs next to Summary, Tests and Code Coverage:
Is there a built in task that will do it for you? Yes and no.
I found plenty of examples just adding /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura
to the build options. But, trying that out I found that there were no reports created to upload. A little digging around and its related to .Net Core. Your project uses .Net Core so you need to do things a little differently.
First off you need to install the coverlet collector yourself so you can collect the required data.
This allows you to run the dotnet test --collect:"XPlat Code Coverage"
command. Now you can collect the results in a similar way to adding /p:CollectCoverage=true
, but you still need to create a report that you can publish.
To do this you’ll need to install the Report Generator. This takes the collected data and can transform it into a variety of formats (see the link above for more information).
Now as we want to read them within Azure Devops, we what the report to be in cobertura format. Running reportgenerator to convert the report into format that Azure Devops likes, we can use the usual PublishCodeCoverageResults@1 task to get it into our pipeline run page.
The code coverage results in Azure Devops:
The test results in Azure Devops:
Here’s the YAML pipeline section for this: