This example shows that dynamic code coverage by default is collecting code coverage also for all child processes. Calculator.Console.Tests
run tests by spawning Calculator.Console
as child process. Default format is binary (.coverage
extension) which can be opened in Visual Studio Enterprise.
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Configuration>
<CodeCoverage>
<CollectFromChildProcesses>True</CollectFromChildProcesses>
<UseVerifiableInstrumentation>False</UseVerifiableInstrumentation>
<EnableStaticNativeInstrumentation>False</EnableStaticNativeInstrumentation>
<EnableDynamicNativeInstrumentation>False</EnableDynamicNativeInstrumentation>
</CodeCoverage>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>
git clone https://github.com/microsoft/codecoverage.git
cd codecoverage/samples/Calculator/src/Calculator.Console/
dotnet build
cd ../../tests/Calculator.Console.Tests/
dotnet test --settings ../../scenarios/scenario11/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: 7.0.x
- name: Restore dependencies
run: dotnet restore
- name: Restore dependencies (Console project)
run: dotnet restore ../../src/Calculator.Console/Calculator.Console.csproj
- name: Build
run: dotnet build --no-restore
- name: Build (Console project)
run: dotnet build --no-restore ../../src/Calculator.Console/Calculator.Console.csproj
- name: Test
run: dotnet test --settings ../../scenarios/scenario11/coverage.runsettings --no-build --verbosity normal
- name: Install dotnet-coverage
run: dotnet tool install -g dotnet-coverage
- name: Convert .coverage report to cobertura
run: dotnet-coverage merge $GITHUB_WORKSPACE/samples/Calculator/tests/Calculator.Console.Tests/TestResults/**/*.coverage -f cobertura -o $GITHUB_WORKSPACE/report.cobertura.xml
- name: ReportGenerator
uses: danielpalme/[email protected]
with:
reports: '${{ github.workspace }}/report.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@v3
with:
name: code-coverage-report
path: ./**/TestResults/**/*.coverage
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: 'restore'
projects: '$(testProjectPath)' # this is specific to example - in most cases not needed
displayName: 'dotnet restore (tests)'
- 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: 'build'
arguments: '--no-restore --configuration $(buildConfiguration)'
projects: '$(testProjectPath)' # this is specific to example - in most cases not needed
displayName: 'dotnet build (tests)'
- task: DotNetCoreCLI@2
inputs:
command: 'test'
arguments: '--no-build --configuration $(buildConfiguration) --settings samples/Calculator/scenarios/scenario11/coverage.runsettings'
projects: '$(testProjectPath)' # this is specific to example - in most cases not needed
displayName: 'dotnet test'
NOTE: Azure DevOps pipelines automatically recognize binary coverage report format. Code coverage results are automatically processed and published to Azure DevOps. No additional steps needed.