Adding Coverage to Tests
I had to configure a bunch of different files to get coverage working both locally and in CI.
What changed
- Added
addopts = --cov=powercrud --cov-report=html --cov-report=xmltopytest.iniso every local or CI run gathers coverage and produces the reports I care about. - Trimmed
pyproject.tomldown to coverage-specific blocks:[tool.coverage.run] source = ["powercrud"]keeps the tracer focused on the app, and[tool.coverage.report]omitssampleandtestsso reports stay clean. - Updated
runteststo wipe old artifacts (rm -rf htmlcov coverage.xml) before running pytest and then regenerate HTML and XML output in one go. - Simplified
.github/workflows/run_tests.ymlto callpytest -m "not playwright" --cov=powercrud --cov-report=term-missing --cov-report=xml, ensuring the matrix createscoverage.xmlexactly once. - Generated a Codecov repo token and wired it into the workflow (
token: ${{ secrets.CODECOV_TOKEN }}) because uploads from protected branches now require authentication.
Why coverage broke midway
Switching the report filters to include = ["*/src/powercrud"] accidentally matched nothing when pytest ran from the repo root, so coverage emitted “No data to report.” Reverting to source = ["powercrud"] (and removing the overzealous include) fixed the path-matching bug and brought the reports back.
Current workflow
- Local runs:
./runtestserases old data, executes the non-Playwright suite undertests.settings, and leaves freshhtmlcov/pluscoverage.xml. - CI runs: the matrix executes the same pytest command, saves
coverage.xml, and the Codecov action uploads it on the Python 3.14 / Django 5.2 leg only. - Badge: once the authenticated upload landed, Codecov flipped the badge from “unknown” to the current percentage (57 % for
powercrud), giving me a baseline to improve from.