Skip to content

Instantly share code, notes, and snippets.

@AppleBoiy
Last active October 16, 2023 14:37
Show Gist options
  • Save AppleBoiy/21f49fe0faae85b052652819cd1bd647 to your computer and use it in GitHub Desktop.
Save AppleBoiy/21f49fe0faae85b052652819cd1bd647 to your computer and use it in GitHub Desktop.
(gh actions) codecov with pytest-cov

Running Tests with pytest-cov

To run tests and generate code coverage reports for your Python project

Configure Codecov Workflow

  1. Move the codecov/pyproject.toml to the root directory of your project.

  2. Move the codecov/codecov.yml to the .github/workflows directory in your project.

This workflow file automates code coverage with GitHub Actions.

name: '🚀Unit Testing'
on:
pull_request:
branches:
- main
permissions:
contents: read
jobs:
code-coverage:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
pip install pytest pytest-cov
- name: Run code coverage with pytest-cov
run: |
pytest --cov-config=pyproject.toml
- name: Upload Coverage Report to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml
flags: unittests
name: codecov-umbrella
[tool.pytest]
addopts = "-v --cov=myapp --cov-fail-under=90"
python_files = "test_*.py"
python_functions = "test_*"
markers = [
"slow: mark tests as slow",
"smoke: mark tests as smoke tests",
"unit: mark tests as unit tests"
]
cov_report = "term-missing"
cov_report_term = "term-missing:skip-covered"
cov_report_xml = "coverage.xml"
norecursedirs = [".git", ".tox", "venv"]
filterwarnings = "ignore::DeprecationWarning"
log_cli = true
log_cli_level = "INFO"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment