Collect code coverage using cobertura report format by specifying configuration. Cobertura report format can be used to generate HTML report using report generator. This format can be also used with PublishCodeCoverageResults@2 in Azure DevOps pipelines.
git clone https://github.com/microsoft/codecoverage.git
cd codecoverage/samples/Calculator/tests/Calculator.Core.Tests/
dotnet test --settings ../../scenarios/scenario03/coverage.runsettings
NOTE: You don't have to use
--collect "Code Coverage"
when you specify runsettings with code coverage configuration.
You can also use run.ps1 to collect code coverage.
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --settings ../../scenarios/scenario03/coverage.runsettings --no-build --verbosity normal
- name: ReportGenerator
uses: danielpalme/[email protected]
with:
reports: './**/TestResults/**/*.cobertura.xml'
targetdir: '${{ github.workspace }}/coveragereport'
reporttypes: 'MarkdownSummaryGithub'
- name: Upload coverage into summary
run: cat $GITHUB_WORKSPACE/coveragereport/SummaryGithub.md >> $GITHUB_STEP_SUMMARY
- name: Archive code coverage results
uses: actions/upload-artifact@v4
with:
name: code-coverage-report
path: ./**/TestResults/**/*.cobertura.xml
overwrite: true
steps:
- task: DotNetCoreCLI@2
inputs:
command: 'restore'
projects: '$(projectPath)' # this is specific to example - in most cases not needed
displayName: 'dotnet restore'
- task: DotNetCoreCLI@2
inputs:
command: 'build'
arguments: '--no-restore --configuration $(buildConfiguration)'
projects: '$(projectPath)' # this is specific to example - in most cases not needed
displayName: 'dotnet build'
- task: DotNetCoreCLI@2
inputs:
command: 'test'
arguments: '--no-build --configuration $(buildConfiguration) --settings samples/Calculator/scenarios/scenario03/coverage.runsettings --logger trx --results-directory $(Agent.TempDirectory)'
publishTestResults: false
projects: '$(projectPath)' # this is specific to example - in most cases not needed
displayName: 'dotnet test'
- task: PublishTestResults@2
inputs:
testResultsFormat: 'VSTest'
testResultsFiles: '$(Agent.TempDirectory)/**/*.trx'
publishRunAttachments: false
- task: PublishCodeCoverageResults@2
inputs:
summaryFileLocation: $(Agent.TempDirectory)/**/*.cobertura.xml
NOTE: To make sure that Code Coverage tab will be visible in Azure DevOps you need to make sure that previous steps will not publish test attachments (
publishRunAttachments: false
andpublishTestResults: false
).