import subprocess import sys import pytest def pytest_collect_file(parent, file_path): if file_path.suffix == ".test": return BtestFile.from_parent(parent, path=file_path) class BtestFile(pytest.File): def collect(self): yield BtestItem.from_parent(self, name=self.path.stem) class BtestItem(pytest.Item): def runtest(self): result = subprocess.run( [sys.executable, str(self.parent.path)], capture_output=True, text=True, ) if result.returncode != 0: raise BtestFailure(result) def repr_failure(self, excinfo): exc = excinfo.value return (exc.result.stdout + exc.result.stderr).strip() def reportinfo(self): return self.parent.path, None, f"btest: {self.name}" class BtestFailure(Exception): def __init__(self, result): self.result = result