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).
Similar Action for Jest
After I see that action become popular, I made similar action (even better) for javascript/typescript that runs jest
jest-coverage-comment
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 |
issue-number |
'' | PR Number where you'd like your comments to be posted to. Required to post a comment when running the workflow on an event that isn't push or pull-request . |
|
pytest-coverage-path |
./pytest-coverage.txt |
The location of the txt output of pytest-coverage. Used to determine coverage percentage | |
pytest-xml-coverage-path |
'' | The location of coverage-xml from pytest-coverage (--cov-report "xml:coverage.xml) | |
coverage-path-prefix |
'' | Prefix for path when link to files in 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 | |
report-only-changed-files |
false | Show in report only changed files for this commit, and not all files | |
junitxml-path |
'' | The location of the junitxml. Used to determine test numbers (passed, failed, etc) | |
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 useful 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-junit.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. |
|
remove-link-from-badge |
false | When true, it will remove the link from badge to readme | |
unique-id-for-comment |
'' | When running in a matrix, pass the matrix value, so each comment will be updated its own comment unique-id-for-comment: ${{ matrix.python-version }} |
Output Variables
Name | Example | Description |
---|---|---|
coverage |
30% | Percentage of the coverage, get from pytest-cov |
color |
red | Color of the percentage. You can see the whole list of badge colors |
coverageHtml |
... | Html with links to files of missing lines. See the output-example |
summaryReport |
... | Markdown with summaryof: Tests/Skipped/Failures/Errors/Time |
warnings |
2441 | Number of warnings, get from pytest-cov |
tests |
109 | Total number of tests, get from junitxml |
skipped |
2 | Total number of skipped tests, get from junitxml |
failures |
1 | Total number of tests with failures, get from junitxml |
errors |
0 | Total number of tests with errors, get from junitxml |
time |
0.583 | Seconds the took to run all the tests, get from junitxml |
notSuccessTestInfo |
example | Info from testcase that has failures/errors/skipped, get from junitxml |
notSuccessTestInfo
the format will be JSON.stringify in current structure
{
"failures": [{ "classname": "...", "name": "..." }],
"errors": [{ "classname": "...", "name": "..." }],
"skipped": [{ "classname": "...", "name": "..." }],
"count": 3
}
Output example
Coverage Report
File | Stmts | Miss | Cover | Missing |
---|---|---|---|---|
functions/example_completed | ||||
Β Β example_completed.py | 64 | 19 | 70% | 33, 39β45, 48β51, 55β58, 65β70, 91β92 |
functions/example_manager | ||||
Β Β example_manager.py | 44 | 11 | 75% | 31β33, 49β55, 67β69 |
Β Β example_static.py | 40 | 2 | 95% | 60β61 |
functions/my_exampels | ||||
Β Β example.py | 20 | 20 | 0% | 1β31 |
functions/resources | ||||
Β Β resources.py | 26 | 26 | 0% | 1β37 |
TOTAL | 1055 | 739 | 30% | Β |
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@v3
- 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 }}"
echo "Summary Report - ${{ steps.coverageComment.outputs.summaryReport }}"
echo "Coverage Warnings - ${{ steps.coverageComment.outputs.warnings }}"
echo "Coverage Errors - ${{ steps.coverageComment.outputs.errors }}"
echo "Coverage Failures - ${{ steps.coverageComment.outputs.failures }}"
echo "Coverage Skipped - ${{ steps.coverageComment.outputs.skipped }}"
echo "Coverage Tests - ${{ steps.coverageComment.outputs.tests }}"
echo "Coverage Time - ${{ steps.coverageComment.outputs.time }}"
echo "Not Success Test Info - ${{ steps.coverageComment.outputs.notSuccessTestInfo }}"
Example GitHub Action workflow that get coverage report from coverage-xml instead of coverage.txt
pytest --cov-report "xml:coverage.xml" --cov=src tests/
- name: Pytest coverage comment
uses: MishaKav/pytest-coverage-comment@main
with:
pytest-xml-coverage-path: ./coverage.xml
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
pytest-xml-coverage-path: ./path-to-file/coverage.xml
title: My Coverage Report Title
badge-title: My Badge Coverage Title
hide-badge: false
hide-report: false
create-new-comment: false
hide-comment: false
report-only-changed-files: false
remove-link-from-badge: false
unique-id-for-comment: python3.8
junitxml-path: ./path-to-file/pytest.xml
junitxml-title: My JUnit Xml Summary Title
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_junit_1.xml
My Title 2, ./data/pytest-coverage_4.txt, ./data/pytest_junit_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@v3
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: 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: |
sed -i '/<!-- Pytest Coverage Comment:Begin -->/,/<!-- Pytest Coverage Comment:End -->/c\<!-- Pytest Coverage Comment:Begin -->\n\${{ steps.coverageComment.outputs.coverageHtml }}\n<!-- Pytest Coverage Comment:End -->' ./README.md
- name: Commit & Push changes to Readme
if: ${{ github.ref == 'refs/heads/main' }}
uses: actions-js/push@master
with:
message: Update coverage on Readme
github_token: ${{ secrets.GITHUB_TOKEN }}
Result example
Multiple Files Mode (can be useful on mono-repo projects)
Badge Colors
Badge | Range |
---|---|
0 - 40 | |
40 - 60 | |
60 - 80 | |
80 - 90 | |
90 - 100 |
Auto updating badge on README
If you want auto-update the coverage badge on your Readme, you can see the workflow
π€ Contributing
We welcome all contributions. You can submit any ideas as pull requests or as GitHub issues and have a good time! :)