Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate code coverage report for asp.net unit tests in Azure DevOps build

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.

like image 617
Shubho Avatar asked Jun 09 '19 15:06

Shubho


People also ask

How do I get code coverage report in Azure DevOps?

The code coverage summary can be viewed on the Summary tab on the pipeline run summary. The results can be viewed and downloaded on the Code coverage tab.

How do you find the code coverage of a unit test?

To calculate the code coverage percentage, simply use the following formula: Code Coverage Percentage = (Number of lines of code executed by a testing algorithm/Total number of lines of code in a system component) * 100.

How do I download a test report from Azure DevOps?

Go to the Tests tab from your build pipeline run. Select your Test Run from the bottom pane (top level row for your run). A dialog should open on your right with options like Debug, Attachments, etc. Click on the Attachments tab and you will find your trx file here.


1 Answers

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

like image 125
Shayki Abramczyk Avatar answered Sep 19 '22 17:09

Shayki Abramczyk