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.6

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/[email protected]

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/[email protected]
  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
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=app tests/ | tee pytest-coverage.txt

      - name: Pytest coverage comment
        uses: MishaKav/[email protected]
        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/[email protected]
  with:
    pytest-coverage-path: ./pytest-coverage.txt
    junitxml-path: ./pytest.xml

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

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

- name: Pytest coverage comment
  uses: MishaKav/[email protected]
  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/[email protected]
  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/[email protected]
  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

Result example

Collapsed comment Result Collapse Example

Expanded comment Result Expand Example

Multiple Files Mode (can be useful on mono-repo projects) Result Multiple Files Mode Example

Badges colors

Coverage 0-40 [0, 40]

Coverage 40-60 [40, 60]

Coverage 60-80 [60, 80]

Coverage 80-90 [80, 90]

Coverage 90-100 [90, 100]

Auto updating badge on README

If you want auto-update the coverage badge on your Readme, you can see the workflow Auto Updating Bagde