-
Notifications
You must be signed in to change notification settings - Fork 42
Description
pdd test should generate tests that isolate local code under test
Summary
Tests generated by pdd test don't set up the Python path to prioritize the local repository code. If the package is also installed in site-packages, tests import and run against the installed version rather than the local code under test. This breaks pdd fix, CI workflows, and any other situation where tests are run against local modifications.
Problem
Currently, generated tests simply import the module:
from foo.module import SomeClassThis relies on Python's default import resolution, which finds site-packages before the local directory. The generated tests should ensure the repository root is first in sys.path:
import sys
from pathlib import Path
# Ensure local repo code takes precedence over installed packages
_repo_root = Path(__file__).resolve().parent.parent # adjust depth as needed
sys.path.insert(0, str(_repo_root))
from foo.module import SomeClassWhy This Matters
By fixing test generation, the isolation is baked into the test file itself. This means:
- Tests work correctly regardless of how they're invoked (pytest, make, CI, IDE,
pdd fix) - No need to modify every PDD command that runs tests
- No reliance on external PYTHONPATH configuration
- Self-documenting: the test file explicitly shows what code it's testing
Reproduction
- Have a project with package
fooinstalled in site-packages - Run
pdd testto generate tests for./foo/module.py - Generated test imports
from foo.module import ... - Run pytest - imports from site-packages, not local code
- Modify
./foo/module.pylocally - Run pytest again - still tests the old site-packages version
Verification:
# Add to generated test to see the problem
from foo.module import SomeClass
print(f"Testing code from: {SomeClass.__module__}")
# Prints: foo.module (site-packages)
# Should print path to local ./foo/module.pyReal-World Example
From a pdd fix run on issue #333:
The local file was correctly modified:
./pdd/agentic_bug_orchestrator.py- contains the fix
But tests imported from site-packages:
/home/james/miniconda3/.../site-packages/pdd/agentic_bug_orchestrator.py- no fix
Result: 5 fix iterations + 2 agentic fallback attempts all "failed" despite generating correct fixes. The tests were validating the wrong code.
Impact
pdd fixbroken: Correct fixes fail validation because tests run against installed code- Silent failure mode: Users see "fix failed" but the fix was actually correct
- Wasted resources: LLM API costs for repeated attempts that can never succeed
- CI fragility: Tests may pass/fail depending on what's installed in the environment
- Inconsistent behavior: Tests behave differently based on environment setup
Proposed Fix
pdd test should generate a path setup preamble in test files:
import sys
from pathlib import Path
# Isolate local repository code - ensure we test the local version,
# not any installed package with the same name
_repo_root = Path(__file__).resolve().parent.parent
if str(_repo_root) not in sys.path:
sys.path.insert(0, str(_repo_root))
# Now imports will find local code first
from foo.module import SomeClassThe parent.parent depth should be calculated based on the test file's location relative to the repository root (e.g., tests/test_foo.py → 2 levels up).
Alternatives Considered
-
Set PYTHONPATH in every PDD command that runs tests
- Fragile: must update every command
- Doesn't help when tests are run outside PDD (CI, IDE, manual pytest)
- Doesn't help existing generated tests
-
Document that users should add
pythonpath = .to pytest.ini- Puts burden on users
- Easy to forget
- Doesn't help existing projects
- Not all test runners respect pytest.ini
-
Require editable installs (
pip install -e .)- Adds setup complexity
- May conflict with production installed versions
- Not always feasible in CI/worktree environments
-
Generate tests with explicit path isolation (recommended)
- Self-contained: test file handles its own imports
- Works with any test runner or invocation method
- Transparent and debuggable
- Matches the principle that tests should be deterministic
Affected Components
pdd test- primary fix location- Test generation prompts/templates
- Possibly
pdd syncif it generates tests
Environment
- PDD version: 0.0.120
- Python: 3.12
- OS: Linux