7
votes

I need guidance in generating code coverage report of Asp.net unit tests in azure build pipeline. My project is based on .Net Framework 4.6.

I am able to run all the unit tests using "visual studio test" task.

I tried the "report generator" task, but it require cobertura or jacoco etc xml files, which am unable to generate in the build pipeline.

Expectation - I want to get code coverage report for the runned unit tests which will show complete information like the lines coverage, branch coverage, function coverage etc. same as what "report generator" generates.

Note: I am able to generate the reports using opencover and reportgenerator on my local system but am unable to find a way to do the same in azure build pipeline.

2
Thank you for the reply, @meziantou. My project is based on .net framework 4.6 and not .net core. I have updated the same in the description. I apologize for not mentioning the same before. Just want to confirm can I use coverlet in .net framework 4.6. If yes, then which azure build task I need to use.Shubho

2 Answers

15
votes

To get the Code Coverage results in .Net framework you just need to enable it in the "Visual Studio Test" task:

enter image description here

If you are use .yml builds the syntax is:

- task: VSTest@2
  inputs:
    codeCoverageEnabled: true

Results:

enter image description here

Note: if you use Microsoft Hosted Agent you will see the results, if you use Self Hosted Agent you must Visual Studio Enterprise version to see the Code Coverage results.

If you want more detailed code coverage report you can use coverlet in .Net framework by install the tool during the pipeline and then generate the report. you can do with a PowerShell script:

dotnet tool install dotnet-reportgenerator --tool-path . --version 4.0.12
dotnet tool install coverlet.console --tool-path . --version 1.4.1
mkdir .\reports
$unitTestFile = gci -Recurse | ?{ $_.FullName -like "*bin\*test*.dll" }
$coverlet = "$pwd\coverlet.exe"
& $coverlet $unitTestFile.FullName --target "dotnet" --targetargs "vstest $($unitTestFile.FullName) --logger:trx" --format "cobertura"
gci -Recurse |
?{ $_.Name -eq "coverage.cobertura.xml"} |
%{ &"$pwd\reportgenerator.exe" "-reports:$($_.FullName)" "-targetdir:reports" "-reportstypes:HTMLInline;HTMLChart" }

enter image description here

Then add "Publish code coverage" task with these parameters:

enter image description here

Results:

enter image description here

1
votes

For anyone looking for code coverage in Azure Devops (using classic editor, without Yaml), in current .NET (core) 5, with xUnit tests:

  1. In your xUnit test project, add following (it generally comes by default in .NET 5, xUnit template now):

    \<PackageReference Include="coverlet.collector" Version="3.0.3" /\>

    Keep checking for new version.

  2. Head to Azure devops, create pipeline using classic editor. Do the restore, build steps. (Or you can choose dotnet core template as below): enter image description here

  3. In the test command of dotnet core task, add argument - --collect:"XPlat Code Coverage". Remember "XPlat Code Coverage" is friendly name and case sensitive. Your test command would look like: enter image description here Check this checkbox if you want to publish your test results: Publish test results and code coverage, but it won't publish code coverage. The functionality is not yet working (at least not in non-windows).

  4. Next add - Publish code coverage results task. Choose "Code coverage tool" as "Cobertura" and in "Summary file" field, add $(Agent.TempDirectory)/**/coverage.cobertura.xml. Looks like this: enter image description here

  5. Save and Queue (in any agent, I use Ubuntu) and see the result once pipeline run completes: enter image description here