Files
anki/tools/coverage-py
Fernando Lins 2ea8e5731a feat: add Python test coverage (#4841)
## Linked issue

Closes #4838

## Summary/motivation

Adds `coverage.py`-based test coverage for both Python test suites
(`pylib` and `qt`). Introduces `just test-py --coverage` and `just
test-py --coverage --html`, plus the `just test --coverage`.

Coverage reports are written to `out/coverage/`.

## How to test
```sh
# Existing behavior unchanged
just test-py

# Terminal summary + enforces thresholds
just test-py --coverage

# Terminal summary + HTML reports under out/coverage/
just test-py --coverage --html

# Umbrella (Python only for now)
just test --coverage
just test --coverage --html
```
### Checklist (minimum)

- [x] I ran `./ninja check` or an equivalent relevant check locally.

### Details

- `coverage` dependency pinned to >=7.13.5 in pyproject.toml.
- The `coverage` umbrella recipe currently delegates to Python only for
now

## Before / after behavior

Before: no `just test-py`, no coverage support.
After: `just test-py` runs Python tests via ninja; `just test-py
--coverage`
runs them with `coverage.py` and enforces minimum line coverage.

---------

Co-authored-by: Abdo <abdo@abdnh.net>
2026-05-15 21:34:38 -03:00

42 lines
1.1 KiB
Bash
Executable File

#!/bin/bash
set -e
target="$1"
html="$2"
case "$target" in
pylib)
pythonpath="out/pylib"
source="pylib/anki"
outdir="out/coverage/python-pylib"
tests="pylib/tests"
threshold=65
;;
qt)
pythonpath="pylib:out/pylib:out/qt"
source="qt/aqt"
outdir="out/coverage/python-qt"
tests="qt/tests"
threshold=20
;;
*)
echo "Usage: $0 [pylib|qt] [--html]"
exit 1
;;
esac
mkdir -p "$outdir"
PYTHONPATH="$pythonpath" ANKI_TEST_MODE=1 out/pyenv/bin/python -m coverage run \
--source="$source" --data-file="$outdir/.coverage" \
-m pytest -p no:cacheprovider "$tests"
out/pyenv/bin/python -m coverage json \
--data-file="$outdir/.coverage" -o "$outdir/coverage-summary.json"
out/pyenv/bin/python -m coverage report \
--data-file="$outdir/.coverage" --fail-under="$threshold"
if [ "$html" = "--html" ]; then
out/pyenv/bin/python -m coverage html \
--data-file="$outdir/.coverage" -d "$outdir/html" --fail-under="$threshold"
echo "Python $target coverage report: $outdir/html/index.html"
fi