Running Tests in Git Worktrees¶
Problem¶
When working in a git worktree (e.g., .claude/worktrees/<name>), Python imports resolve to the main repo instead of the worktree's modified source files.
Root Cause¶
UV workspace uses editable installs with .pth files. The tradai namespace package aggregates paths from both the main repo and the worktree, with the main repo paths appearing first on sys.path:
# sys.path order (main repo wins):
/Users/.../tradai-uv/libs/tradai-common/src ← main repo (first!)
/Users/.../tradai-uv/libs/tradai-data/src
...
/Users/.../.claude/worktrees/<name>/libs/tradai-common/src ← worktree (too late)
This means from tradai.common.aws.errors import handle_ecs_error loads the main repo version, ignoring worktree edits.
Symptoms¶
- Tests pass/fail based on main repo code, not your worktree changes
uv run python -c "import tradai.common; print(tradai.common.__file__)"shows the main repo path- Clearing
__pycache__doesn't help
Solution¶
Prepend worktree source paths to PYTHONPATH so they take priority over the main repo.
One-liner for test commands¶
WT="$(pwd)" PYTHONPATH="$WT/libs/tradai-common/src:$WT/libs/tradai-data/src:$WT/libs/tradai-strategy/src:$WT/cli/src:$WT/services/backend/src:$WT/services/strategy-service/src:$WT/services/data-collection/src:$WT/services/mlflow:$WT/infra/shared:$WT/infra/foundation:$WT/infra/compute:$WT/infra/edge" uv run pytest <test-path> --no-cov
Reusable shell function¶
Add to your shell profile or run at the start of a worktree session:
wt-test() {
local WT="$(pwd)"
PYTHONPATH="$WT/libs/tradai-common/src:$WT/libs/tradai-data/src:$WT/libs/tradai-strategy/src:$WT/cli/src:$WT/services/backend/src:$WT/services/strategy-service/src:$WT/services/data-collection/src:$WT/services/mlflow:$WT/infra/shared:$WT/infra/foundation:$WT/infra/compute:$WT/infra/edge" \
uv run pytest "$@"
}
# Usage:
wt-test libs/tradai-common/tests/unit/test_aws_errors.py -v --no-cov
wt-test -k "test_handle_ecs" --no-cov
Verify it works¶
# Should print the WORKTREE path, not the main repo path:
WT="$(pwd)" PYTHONPATH="$WT/libs/tradai-common/src" \
uv run python -c "import tradai.common; print(tradai.common.__file__)"
Notes¶
uv sync --all-packagesmust be run in the worktree first to create its.venv- The
--no-covflag is recommended for quick test runs (coverage slows things down and reports incorrect percentages in worktrees) just checkandjust testdo not setPYTHONPATH— they will run against main repo source. Use thewt-testhelper or the one-liner instead.