Skip to content

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=xml to pytest.ini so every local or CI run gathers coverage and produces the reports I care about.
  • Trimmed pyproject.toml down to coverage-specific blocks: [tool.coverage.run] source = ["powercrud"] keeps the tracer focused on the app, and [tool.coverage.report] omits sample and tests so reports stay clean.
  • Updated runtests to 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.yml to call pytest -m "not playwright" --cov=powercrud --cov-report=term-missing --cov-report=xml, ensuring the matrix creates coverage.xml exactly 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: ./runtests erases old data, executes the non-Playwright suite under tests.settings, and leaves fresh htmlcov/ plus coverage.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.