Files
mtusi2026lab5/student-tests/test_coverage.py
T
ricoromaandClaude Opus 5.5 04f859282d chore: port lab 4 solution into the lab 5 skeleton
Source: mtusi2026lab4-safronov, solution/bvt2301-safronov, 2546deb.
Contracts, public tests and evidence of lab 4 are not carried over.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 03:09:12 +03:00

126 lines
4.3 KiB
Python

"""У каждого сценария основных спеков есть тест или исключение."""
from pathlib import Path
import pytest
from spec_coverage import (
CAPABILITIES,
COLLECTED,
EXCLUDED,
Exclusion,
Scenario,
all_scenarios,
coverage_problems,
missing_evidence,
spec_scenarios,
)
TESTS_DIR = Path(__file__).parent
def complete_marks(scenarios: set[Scenario]) -> dict[Scenario, list[str]]:
"""Маркеры, при которых покрыто всё, кроме исключений."""
return {scenario: ["test_x"] for scenario in scenarios - EXCLUDED.keys()}
def test_every_scenario_is_covered(request: pytest.FixtureRequest) -> None:
collected = request.config.stash[COLLECTED]
modules = {path.name for path in TESTS_DIR.glob("test_*.py")}
if not modules <= collected.modules:
pytest.skip(
"coverage is checked only when the whole suite is collected: "
"make student_test",
)
scenarios = all_scenarios()
problems = coverage_problems(scenarios, collected.marks, EXCLUDED)
tested = len(scenarios & collected.marks.keys())
# тесты fault выполняет только make failure_checks
by_fault = len(scenarios & collected.fault)
print(
f"coverage: {len(scenarios)} scenarios in "
f"{len(CAPABILITIES)} specs, {tested} tested "
f"({by_fault} by make failure_checks), {len(EXCLUDED)} excluded",
)
assert not problems, "\n".join(problems)
def test_every_spec_has_scenarios() -> None:
for capability in CAPABILITIES:
assert spec_scenarios(capability), f"no scenarios in {capability}"
def test_exclusions_explain_reason_and_check() -> None:
for (capability, scenario), exclusion in EXCLUDED.items():
assert exclusion.reason.strip(), f"{capability} / {scenario}"
assert exclusion.check.strip(), f"{capability} / {scenario}"
def test_exclusion_evidence_exists() -> None:
"""Файл evidence, на который ссылается исключение, есть в репозитории
(спек student-tests, «Доказательства исключений сверки в
репозитории»)."""
problems = missing_evidence(EXCLUDED)
assert not problems, "\n".join(problems)
def test_missing_evidence_fails(tmp_path: Path) -> None:
excluded = {
("health", "Сценарий"): Exclusion(
reason="r",
check="команда — evidence/demo/file.md",
),
}
assert missing_evidence(excluded, tmp_path) == [
"exclusion refers to missing evidence/demo/file.md: health / Сценарий",
]
(tmp_path / "evidence" / "demo").mkdir(parents=True)
(tmp_path / "evidence" / "demo" / "file.md").write_text(
"x",
encoding="utf-8",
)
assert missing_evidence(excluded, tmp_path) == []
def test_scenario_parser(tmp_path: Path) -> None:
spec = tmp_path / "demo" / "spec.md"
spec.parent.mkdir()
spec.write_text(
"### Requirement: R\n\n#### Scenario: Первый\n\n- **WHEN** x\n\n"
"#### Scenario: Второй \n",
encoding="utf-8",
)
assert spec_scenarios("demo", tmp_path) == ["Первый", "Второй"]
def test_new_scenario_without_test_fails() -> None:
scenarios = all_scenarios()
marks = complete_marks(scenarios)
assert coverage_problems(scenarios, marks, EXCLUDED) == []
grown = scenarios | {("users", "Новый сценарий")}
assert coverage_problems(grown, marks, EXCLUDED) == [
"no test and no exclusion: users / Новый сценарий",
]
def test_marker_typo_fails() -> None:
scenarios = all_scenarios()
marks = complete_marks(scenarios)
marks[("users", "Повтор usernme")] = ["test_typo"]
assert coverage_problems(scenarios, marks, EXCLUDED) == [
"marker points to unknown scenario users / Повтор usernme: test_typo",
]
def test_stale_exclusion_fails() -> None:
scenarios = all_scenarios()
marks = complete_marks(scenarios)
excluded = {
**EXCLUDED,
("health", "Удалённый сценарий"): Exclusion(reason="r", check="c"),
}
assert coverage_problems(scenarios, marks, excluded) == [
"exclusion points to unknown scenario: health / Удалённый сценарий",
]