MaxouMask
MaxouMask

Reputation: 1037

Displaying NUnit tests code coverage on Azure DevOps

I've setup a new pipeline on Azure DevOps which builds and run the tests of the projects. The tests are written with NUnit.

In the pipeline I'm using the VSTest@2 task to run the unit tests and I add the codeCoverageEnabled to true.

In the end the pipeline runs and when I go in the "Code Coverage" tab of the job, it allows me to download .codecoverage file but it does not display its content in the tab. My understanding was that this should happen.

How can I fix this ?

Thanks

Upvotes: 2

Views: 3650

Answers (1)

AimusSage
AimusSage

Reputation: 756

By default, the code coverage for the VSTest Task is output to a .codecoverage file, which Azure DevOps does not know how to interpret and only provides as a downloadable file. You'll need to use a few DotNetCoreCLI tasks and coverlet to be able to display code coverage results on the code coverage tab in azure pipelines.

So, if you are on .NET CORE, There is a way how you can do that.

Step 1 add Coverlet.collector nuget Package in your test project

Step 2 Change your azure-pipelines.yml to include the following for your code coverage: If you have any settings from a CodeCoverage.runsettings file, you can keep them too

- task: DotNetCoreCLI@2
  inputs:
    command: 'test'
    projects: '**/*.Tests/*.csproj'
    arguments: -c $(BuildConfiguration) --collect:"XPlat Code Coverage" -- RunConfiguration.DisableAppDomain=true
    testRunTitle: 'Run Test and collect Coverage' 
  displayName: 'Running tests'

- task: DotNetCoreCLI@2
  inputs:
    command: custom
    custom: tool
    arguments: install --tool-path . dotnet-reportgenerator-globaltool
  displayName: Install ReportGenerator tool

- script: reportgenerator -reports:$(Agent.TempDirectory)/**/coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)/coverlet/reports -reporttypes:"Cobertura"
  displayName: Create reports

- task: PublishCodeCoverageResults@1
  displayName: 'Publish code coverage'
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: $(Build.SourcesDirectory)/coverlet/reports/Cobertura.xml  

One other thing to note for the above code is the Report Generator. Depending on which version of .net core you are using it might be required to get a different version of the tool.

More information can also be found on the Microsoft Docs

Upvotes: 9

Related Questions