-
Notifications
You must be signed in to change notification settings - Fork 48
Description
Summary
PDD-generated test files inconsistently use sys.modules for pre-import mocking, often using incorrect module paths that don't match the actual import paths in the source code. This causes tests to pass in isolation but fail when run as part of a full test suite.
Problem
When generating tests, PDD frequently uses sys.modules pre-import mocking patterns like:
# What PDD generates (INCORRECT):
sys.modules["config"] = mock_config
sys.modules["models"] = mock_models_module
# But the actual source code uses:
from src.config import get_settings
from src.models import Installation, JobThe mock paths don't match the actual import paths (config vs src.config), so the mocks are never applied to the correct modules.
Evidence
In a generated github_app_2 project, I found inconsistent patterns across test files:
Incorrect (short paths that don't match source imports):
# tests/test_firestore_client.py
sys.modules["config"] = mock_config # Wrong: source uses "src.config"
sys.modules["models"] = mock_models_module # Wrong: source uses "src.models"
# tests/test_credits_service.py
mock_config = sys.modules["config"] # Wrong
mock_firestore = sys.modules["firestore_client"] # WrongCorrect (full paths matching source imports):
# tests/test_installation_service.py
sys.modules["src.config"] = mock_config # Correct
sys.modules["src.models"] = mock_models # Correct
# tests/test_pubsub_client.py
sys.modules["src.config"] = mock_config # Correct
sys.modules["src.models"] = mock_models # CorrectImpact
-
Tests pass in isolation but fail in suite: When PDD runs
pytest tests/test_firestore_client.pyalone, import order may accidentally make mocks work. Butpytest testsfails during collection. -
False positives in sync: PDD sync reports "All required files synchronized" with tests passing, but the tests are actually broken.
-
Silent validation failures: The
_run.jsonshows"tests_passed": 13, "tests_failed": 0but running the full suite reveals collection errors.
Reproduction
- Generate a Python project with multiple modules that import from each other (e.g.,
src.config,src.models,src.clients) - Run
pdd syncon each module - Run
pytest tests(full suite) - observe collection failures - Run
pytest tests/test_<module>.py(individual) - may pass due to import order
Suggested Fix
When generating sys.modules mocks, PDD should:
- Analyze actual import statements in the source file to determine the correct module path
- Use fully-qualified paths that match the source imports (e.g.,
sys.modules["src.config"]notsys.modules["config"]) - Prefer
pytestfixtures withmonkeypatchorunittest.mock.patchover rawsys.modulesmanipulation when possible - Consider using
conftest.pyfor shared test configuration instead of per-filesys.moduleshacks
Environment
- PDD version: latest (as of 2026-01-27)
- Python: 3.12
- Project structure:
src/package with submodules