Skip to content
You're viewing an older version of this GitHub Action. Do you want to see the latest version instead?
message-circle

GitHub Action

Pytest Coverage Comment

v1.1.7

Pytest Coverage Comment

message-circle

Pytest Coverage Comment

Comments a pull request with the pytest code coverage badge and full report

Installation

Copy and paste the following snippet into your .yml file.

              

- name: Pytest Coverage Comment

uses: MishaKav/pytest-coverage-comment@v1.1.7

Learn more about this action in MishaKav/pytest-coverage-comment

Choose a version

Pytest Coverage Comment

This action comments a pull request or commit with a HTML test coverage report. The report is based on the coverage report generated by your test runner. Note that this action does not run any tests, but expects the tests to have been run by another action already (support pytest only).

You can add this action to your GitHub workflow for Ubuntu runners (e.g. runs-on: ubuntu-latest) as follows:

- name: Pytest coverage comment
  uses: MishaKav/pytest-coverage-comment@main
  with:
    pytest-coverage-path: ./pytest-coverage.txt
    junitxml-path: ./pytest.xml

Inputs

Name Required Default Description
github-token ${{github.token}} An alternative GitHub token, other than the default provided by GitHub Actions runner
pytest-coverage-path ./pytest-coverage.txt The location of the txt output of pytest-coverage. Used to generate the comment
title Coverage Report Title for the coverage report. Useful for monorepo projects
badge-title Coverage Title for the badge icon
hide-badge false Hide badge with percentage
hide-report false Hide coverage report
junitxml-path '' The location of the junitxml path
junitxml-title '' Title for summary for junitxml
create-new-comment false When false, will update the same comment, otherwise will publish new comment on each run.
hide-comment false Hide the whole comment (use when you need only the output). Useful for auto-update bagdes in readme. See the workflow for example
default-branch main This branch name is usefull when generate "coverageHtml", it points direct links to files on this branch (instead of commit).
Usually "main" or "master".
multiple-files '' You can pass array of titles and files to generate single comment with table of results.
Single line should look like Title, ./path/to/pytest-coverage.txt, ./path/to/pytest.xml
example:
My Title 1, ./data/pytest-coverage_3.txt, ./data/pytest_1.xml
Note: In that mode the output for coverage and color will be for the first file only.

Output example

Coverage

Coverage Report
FileStmtsMissCoverMissing
functions/example_completed
   example_completed.py641970%33, 39–45, 48–51, 55–58, 65–70, 91–92
functions/example_manager
   example_manager.py441175%31–33, 49–55, 67–69
   example_static.py40295%60–61
functions/my_exampels
   example.py20200%1–31
functions/resources
   resources.py26260%1–37
TOTAL105573930% 

Tests Skipped Failures Errors Time
109 2 💤 1 ❌ 0 🔥 0.583s ⏱️

Example usage

The following is an example GitHub Action workflow that uses the Pytest Coverage Comment to extract the coverage report to comment at pull request:

# This workflow will install dependencies, create coverage tests and run Pytest Coverage Comment
# For more information see: https://github.com/MishaKav/pytest-coverage-comment/
name: pytest-coverage-comment
on:
  pull_request:
    branches:
      - '*'
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Set up Python 3.8
        uses: actions/setup-python@v2
        with:
          python-version: 3.8

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install flake8 pytest pytest-cov
          if [ -f requirements.txt ]; then pip install -r requirements.txt; fi

      - name: Build coverage file
        run: |
          pytest --junitxml=pytest.xml --cov-report=term-missing:skip-covered --cov=app tests/ | tee pytest-coverage.txt

      - name: Pytest coverage comment
        uses: MishaKav/pytest-coverage-comment@main
        with:
          pytest-coverage-path: ./pytest-coverage.txt
          junitxml-path: ./pytest.xml

Example GitHub Action workflow that uses coverage percentage as output (see the live workflow)

- name: Pytest coverage comment
  id: coverageComment
  uses: MishaKav/pytest-coverage-comment@main
  with:
    pytest-coverage-path: ./pytest-coverage.txt
    junitxml-path: ./pytest.xml

- name: Check the output coverage
  run: |
    echo "Coverage Percantage - ${{ steps.coverageComment.outputs.coverage }}"
    echo "Coverage Color - ${{ steps.coverageComment.outputs.color }}"
    echo "Coverage Html - ${{ steps.coverageComment.outputs.coverageHtml }}"

Example GitHub Action workflow that passes all params to Pytest Coverage Comment

- name: Pytest coverage comment
  uses: MishaKav/pytest-coverage-comment@main
  with:
    pytest-coverage-path: ./path-to-file/pytest-coverage.txt
    title: My Coverage Report Title
    badge-title: My Badge Coverage Title
    hide-badge: false
    hide-report: false
    create-new-comment: false
    hide-comment: false
    junitxml-path: ./path-to-file/pytest.xml
    junitxml-title: My JUnit Xml Summary Title

image

Example GitHub Action workflow that runs pytest inside docker It will generate pytest-coverage.txt and pytest.xml in /tmp directory inside docker and share /tmp directory with GitHub workspace.

- name: Run unit tests (pytest)
  run: |
    docker run -v /tmp:/tmp $IMAGE_TAG python3 -m pytest --cov-report=term-missing:skip-covered --junitxml=/tmp/pytest.xml --cov=src tests/ | tee /tmp/pytest-coverage.txt

- name: Pytest coverage comment
  uses: MishaKav/pytest-coverage-comment@main
  with:
    pytest-coverage-path: /tmp/pytest-coverage.txt
    junitxml-path: /tmp/pytest.xml

Example GitHub Action workflow that uses multiple files mode (see the live workflow)

- name: Pytest coverage comment
  uses: MishaKav/pytest-coverage-comment@main
  with:
    multiple-files: |
      My Title 1, ./data/pytest-coverage_3.txt, ./data/pytest_1.xml
      My Title 2, ./data/pytest-coverage_4.txt, ./data/pytest_2.xml

Example GitHub Action workflow that will update your README.md with coverage report, only on merge to main branch (see the update-coverage-on-readme workflow) All you need is to add in your README.md the following lines wherever you want. If your coverage html report will not change, it wouldn't push any changes to readme file.

<!-- Pytest Coverage Comment:Begin -->
<!-- Pytest Coverage Comment:End -->
name: Update Coverage on Readme
on:
  push:
jobs:
  update-coverage-on-readme:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          persist-credentials: false # otherwise, the token used is the GITHUB_TOKEN, instead of your personal token
          fetch-depth: 0 # otherwise, you will failed to push refs to dest repo

      - name: Extract branch name
        run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
        id: extract_branch

      - name: Pytest coverage comment
        if: ${{ github.ref == 'refs/heads/main' }}
        id: coverageComment
        uses: MishaKav/pytest-coverage-comment@main
        with:
          hide-comment: true
          pytest-coverage-path: ./data/pytest-coverage_4.txt

      - name: Update Readme with Coverage Html
        if: ${{ github.ref == 'refs/heads/main' }}
        run: |