Framework Mode - pytest-cov for coverage
Folder Structure
project-root/ ├── src/ │ └── app_module.py ├── tests/ │ ├── test_app_module.py │ └── __init__.py ├── .coveragerc ├── pytest.ini ├── requirements.txt └── README.md
project-root/ ├── src/ │ └── app_module.py ├── tests/ │ ├── test_app_module.py │ └── __init__.py ├── .coveragerc ├── pytest.ini ├── requirements.txt └── README.md
src/ contains the application code to be tested.tests/ holds test files using pytest functions and classes.pytest.ini and .coveragerc configure pytest and coverage behavior.tests/ or a tests/utils/ folder.Use pytest.ini to set pytest options and enable coverage plugin:
[pytest] addopts = --cov=src --cov-report=term-missing # Optional: test paths testpaths = tests
Use .coveragerc to customize coverage behavior:
[run] branch = True source = src [report] show_missing = True skip_covered = True
Manage environments and credentials separately (e.g., environment variables or .env files) to keep tests clean and secure.
pytest with coverage to get a terminal report showing which lines are covered and which are missing.--cov-report=html for detailed visual feedback.
name: Python Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.12'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run tests with coverage
run: |
pytest --cov=src --cov-report=html --cov-fail-under=80
- name: Upload coverage report
uses: actions/upload-artifact@v3
with:
name: coverage-report
path: htmlcov/
src/) to avoid noise from tests or external libraries.--cov-fail-under to enforce minimum coverage thresholds and maintain quality.branch = True) to check all decision paths.Where in this folder structure would you add a new fixture to provide a reusable database connection for tests?