"""Pytest configuration: collect testcases/*.md as test items.""" import pytest from pathlib import Path from runtime_test import parse_testcase, CogTestRunner def pytest_collect_file(parent, file_path): if file_path.suffix == ".md" and file_path.parent.name == "testcases": return TestCaseFile.from_parent(parent, path=file_path) class TestCaseFile(pytest.File): def collect(self): tc = parse_testcase(self.path) yield TestCaseItem.from_parent(self, name=tc["name"], testcase=tc) class TestCaseItem(pytest.Item): def __init__(self, name, parent, testcase): super().__init__(name, parent) self.testcase = testcase def runtest(self): runner = CogTestRunner() results = runner.run(self.testcase) # Collect failures failures = [r for r in results if r["status"] == "FAIL"] if failures: msg = "\n".join(f"Step {r['step']}: {r['check']} — {r['detail']}" for r in failures) raise TestCaseFailure(msg, results) def repr_failure(self, excinfo, style=None): if isinstance(excinfo.value, TestCaseFailure): return f"\n{excinfo.value.args[0]}" return super().repr_failure(excinfo, style) def reportinfo(self): return self.path, 0, f"testcase: {self.name}" class TestCaseFailure(Exception): def __init__(self, msg, results): super().__init__(msg) self.results = results