Skip to content

Latest commit

 

History

History
100 lines (78 loc) · 3.71 KB

File metadata and controls

100 lines (78 loc) · 3.71 KB

Scenario Description

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.

Collect code coverage using command line

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.

Collect code coverage inside github workflow

    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

Full source example

Run example

Collect code coverage inside Azure DevOps Pipelines

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 and publishTestResults: false).

Full source example

alt text

alt text

Report example

alt text

Link