If you see the message “[warning]No code coverage results were found to publish” when using Azure DevOps Pipelines to publish code coverage, one of the reasons could be due to a missing NuGet package in your solution. The package I was missing was coverlet.msbuild. Below shows the coverlet packages that need to be included in your csproj:
<PackageReference Include="coverlet.msbuild" Version="2.9.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> <PackageReference Include="coverlet.collector" Version="1.3.0"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference>
To test this is working, open a command prompt from your unit test project and type “dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=cobertura” – if you look in the directory of the test project you should now see a file called “coverage.cobertura.xml”.
To produce code coverage reports in Azure DevOps that are visible from the Code Coverage tab in the build output, please ensure you include the following test and publish steps in your pipeline:
- task: DotNetCoreCLI@2 displayName: dotnet test inputs: command: 'test' projects: '**/*Tests.csproj' # Might need to change this to suit your naming convention publishTestResults: true arguments: '/p:CollectCoverage=true /p:CoverletOutputFormat=cobertura' - script: | dotnet tool install -g dotnet-reportgenerator-globaltool reportgenerator -reports:$(Build.SourcesDirectory)/**/coverage.cobertura.xml -targetdir:$(Build.SourcesDirectory)/coverlet/reports -reporttypes:"Cobertura" displayName: Create Code coverage report - task: PublishCodeCoverageResults@1 displayName: 'Publish code coverage report' inputs: codeCoverageTool: 'Cobertura' summaryFileLocation: '$(Build.SourcesDirectory)/coverlet/reports/Cobertura.xml'