Skip to content

Generated tests use incorrect sys.modules paths for mocking #412

@jiaminc-cmu

Description

@jiaminc-cmu

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, Job

The 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"]  # Wrong

Correct (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    # Correct

Impact

  1. Tests pass in isolation but fail in suite: When PDD runs pytest tests/test_firestore_client.py alone, import order may accidentally make mocks work. But pytest tests fails during collection.

  2. False positives in sync: PDD sync reports "All required files synchronized" with tests passing, but the tests are actually broken.

  3. Silent validation failures: The _run.json shows "tests_passed": 13, "tests_failed": 0 but running the full suite reveals collection errors.

Reproduction

  1. Generate a Python project with multiple modules that import from each other (e.g., src.config, src.models, src.clients)
  2. Run pdd sync on each module
  3. Run pytest tests (full suite) - observe collection failures
  4. Run pytest tests/test_<module>.py (individual) - may pass due to import order

Suggested Fix

When generating sys.modules mocks, PDD should:

  1. Analyze actual import statements in the source file to determine the correct module path
  2. Use fully-qualified paths that match the source imports (e.g., sys.modules["src.config"] not sys.modules["config"])
  3. Prefer pytest fixtures with monkeypatch or unittest.mock.patch over raw sys.modules manipulation when possible
  4. Consider using conftest.py for shared test configuration instead of per-file sys.modules hacks

Environment

  • PDD version: latest (as of 2026-01-27)
  • Python: 3.12
  • Project structure: src/ package with submodules

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions