user989988
user989988

Reputation: 3736

Collect code coverage using GitHub Action

I have an Azure Pipeline build step as follows:

    - task: PublishCodeCoverageResults@1
      displayName: 'collect code coverage'
      inputs:
        codeCoverageTool: 'cobertura'
        summaryFileLocation: $(Build.SourcesDirectory)\Coverage\coverage.cobertura.xml
        failIfCoverageEmpty: false
      
    - task: mspremier.BuildQualityChecks.QualityChecks-task.BuildQualityChecks@6
      displayName: 'check build quality'
      inputs:
        checkCoverage: true
        coverageFailOption: fixed
        coverageType: lines
        coverageThreshold: 1

How do I convert these to GitHub actions?

Upvotes: 1

Views: 3290

Answers (2)

Jarno
Jarno

Reputation: 7212

While github actions for many bigger coverage platforms exists, you could use cobertura-action instead. The action will comment a summary of the coverage to the pull request and can fail your build if the coverage does not match a certain percentage. It is free and does not require external apps.

on:
  pull_request:

jobs:
  coverage:
    runs-on: ubuntu-latest
    steps:
      - name: Test and generate coverage
        ...
      - uses: 5monkeys/cobertura-action@master
        with:
          path: path/to/coverage.xml
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          minimum_coverage: 75

Upvotes: 0

vrdmr
vrdmr

Reputation: 396

You can use a code coverage dashboard such as codecov to collect the code-coverage. In Azure DevOps, the code coverage view is within the Azure Pipelines view, but GitHub does not provide such a view. CodeCov, Coveralls come in to help with that. Others can be found in the GitHub marketplace.

An example usage would be something like the below:

- name: Test with pytest
  run: |
    pytest  --instafail --cov=./src --cov-report xml --cov-branch tests/unittests
- name: Codecov
  uses: codecov/[email protected]
  with:
    token: ${{ secrets.CODECOV_TOKEN }} # not required for public repos
    file: ./coverage.xml # optional
    flags: unittests # optional
    name: codecov # optional
    fail_ci_if_error: false # optional (default = false)

Upvotes: 1

Related Questions