def leave_no_artifacts_behind():
    """Checks that no artifacts are left behind after the test."""
    tests_root = Path(__file__).parent.parent
    # Ignore the __pycache__ directories
    files_before = {p for p in tests_root.rglob("*") if "__pycache__" not in p.parts}
    yield
    files_after = {p for p in tests_root.rglob("*") if "__pycache__" not in p.parts}
    difference = files_after - files_before
    difference = {str(f.relative_to(tests_root)) for f in difference}
    # ignore the .coverage files
    difference = {f for f in difference if not f.endswith(".coverage")}
    assert not difference, f"Test left artifacts behind: {difference}"
Checks that no artifacts are left behind after the test.
tests/tests_pytorch/conftest.py