Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ jobs:
python -m pytest -q
--cov=acid2reaper --cov-report=term-missing
--cov-fail-under=75
--junitxml=pytest-junit.xml

# Floor: passed tests only, skips excluded. Lowest leg (Windows) runs 467 passed
# (459 on main plus the 8 floor-script tests); 443 is about 5 percent under.
- name: Assert executed-test floor
if: ${{ !cancelled() }}
run: python scripts/assert_tests_executed.py pytest-junit.xml --min 443

- name: Check golden projects are up to date
run: python scripts/update_goldens.py --check
Expand Down
7 changes: 6 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ jobs:
run: python scripts/verify_changelog.py

- name: Run tests
run: python -m pytest -q
run: python -m pytest -q --junitxml=pytest-junit.xml

# Same floor as CI's lowest leg. Linux runs 468 passed here.
- name: Assert executed-test floor
if: ${{ !cancelled() }}
run: python scripts/assert_tests_executed.py pytest-junit.xml --min 443

- name: Build sdist and wheel
run: python -m build
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ id_ed25519.pub
.coverage.*
coverage.xml
htmlcov/
pytest-junit.xml
58 changes: 58 additions & 0 deletions scripts/assert_tests_executed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python3
"""
Fail unless pytest's junit report shows at least N tests that really ran.

A skipped test is not an executed test: env-gated skips (see tests/conftest.py) and a
mis-selected run both exit 0 with fewer tests than intended. Passed testcases only;
skipped, failed and errored ones don't count. A missing, unreadable or empty report
fails too.

Usage: python scripts/assert_tests_executed.py REPORT.xml --min N
"""
from __future__ import annotations

import argparse
import sys
import xml.etree.ElementTree as ET
from pathlib import Path

NOT_PASSED = ("skipped", "failure", "error")


def count_passed(report: Path) -> int:
"""Count testcases in a junit xml file that carry no skipped/failure/error child."""
try:
root = ET.parse(report).getroot()
except (OSError, ET.ParseError) as exc:
raise ValueError(f"cannot read junit report {report}: {exc}") from exc
cases = list(root.iter("testcase"))
if not cases:
raise ValueError(f"junit report {report} has no testcases")
return sum(1 for case in cases if not any(case.find(tag) is not None for tag in NOT_PASSED))


def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("report", type=Path)
parser.add_argument("--min", type=int, required=True, dest="minimum")
args = parser.parse_args(argv)
if args.minimum < 1:
print("assert_tests_executed: --min must be at least 1", file=sys.stderr)
return 1
try:
passed = count_passed(args.report)
except ValueError as exc:
print(f"assert_tests_executed: {exc}", file=sys.stderr)
return 1
if passed < args.minimum:
print(
f"assert_tests_executed: {passed} tests passed, floor is {args.minimum}",
file=sys.stderr,
)
return 1
print(f"assert_tests_executed: {passed} tests passed, floor {args.minimum}")
return 0


if __name__ == "__main__":
sys.exit(main())
64 changes: 64 additions & 0 deletions tests/test_assert_tests_executed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
"""Tests for scripts/assert_tests_executed.py, the CI executed-test floor."""
from __future__ import annotations

import importlib.util
from pathlib import Path

import pytest

_SCRIPT = Path(__file__).resolve().parent.parent / "scripts" / "assert_tests_executed.py"
_spec = importlib.util.spec_from_file_location("assert_tests_executed", _SCRIPT)
floor = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(floor)


def _report(tmp_path: Path, body: str) -> Path:
path = tmp_path / "junit.xml"
path.write_text(f'<?xml version="1.0"?><testsuites><testsuite>{body}</testsuite></testsuites>')
return path


def test_counts_only_passed_cases(tmp_path):
report = _report(
tmp_path,
'<testcase name="a"/><testcase name="b"/>'
'<testcase name="c"><skipped/></testcase>'
'<testcase name="d"><failure/></testcase>'
'<testcase name="e"><error/></testcase>',
)
assert floor.count_passed(report) == 2


def test_meets_floor_exactly(tmp_path):
report = _report(tmp_path, '<testcase name="a"/><testcase name="b"/>')
assert floor.main([str(report), "--min", "2"]) == 0


def test_below_floor_fails(tmp_path, capsys):
report = _report(tmp_path, '<testcase name="a"/><testcase name="b"><skipped/></testcase>')
assert floor.main([str(report), "--min", "2"]) == 1
assert "1 tests passed, floor is 2" in capsys.readouterr().err


def test_missing_report_fails(tmp_path):
assert floor.main([str(tmp_path / "nope.xml"), "--min", "1"]) == 1


def test_empty_report_fails(tmp_path):
assert floor.main([str(_report(tmp_path, "")), "--min", "1"]) == 1


def test_malformed_report_fails(tmp_path):
path = tmp_path / "junit.xml"
path.write_text("<testsuites><testsuite>")
assert floor.main([str(path), "--min", "1"]) == 1


def test_zero_floor_rejected(tmp_path):
report = _report(tmp_path, '<testcase name="a"/>')
assert floor.main([str(report), "--min", "0"]) == 1


def test_count_raises_on_empty(tmp_path):
with pytest.raises(ValueError):
floor.count_passed(_report(tmp_path, ""))
Loading