1. Detecting Session-Level Patch Leaks and Scope Mismatches
Python Backend DeveloperBackground
A test suite suffers from intermittent failures when tests execute in random order across subdirectories.
Problem
A root conftest fixture started a mock patch without stopping it, and a module fixture injected function-scoped monkeypatch.
How to use
Paste the root conftest.py and tests/unit/conftest.py into the source input separated by '# --- file: <path> ---' headers.
# --- file: conftest.py ---
import pytest
from unittest import mock
@pytest.fixture(scope="session")
def patched_auth():
patcher = mock.patch("auth.verify_token", return_value=True)
patcher.start()
return None
# --- file: tests/unit/conftest.py ---
import pytest
@pytest.fixture(scope="module")
def fake_auth(monkeypatch):
monkeypatch.setattr("auth.verify_token", lambda t: True)
return TrueOutcome
Flags the unstopped patcher.start() session leak, highlights the monkeypatch scope mismatch, and maps fixture overrides.