Skip to content

pdd test should generate tests that isolate local code under test #342

@jamesdlevine

Description

@jamesdlevine

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 SomeClass

This 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 SomeClass

Why 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

  1. Have a project with package foo installed in site-packages
  2. Run pdd test to generate tests for ./foo/module.py
  3. Generated test imports from foo.module import ...
  4. Run pytest - imports from site-packages, not local code
  5. Modify ./foo/module.py locally
  6. 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.py

Real-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 fix broken: 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 SomeClass

The 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

  1. 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
  2. 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
  3. Require editable installs (pip install -e .)

    • Adds setup complexity
    • May conflict with production installed versions
    • Not always feasible in CI/worktree environments
  4. 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 sync if it generates tests

Environment

  • PDD version: 0.0.120
  • Python: 3.12
  • OS: Linux

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions