Skip to content

K-Fujimura/vscode-pytest-path-resolver

Repository files navigation

Pytest Path Resolver

VS Code / Cursor extension that statically resolves pathlib.Path expressions in pytest test files to actual file paths.

Features

Right-click on a Path expression in a pytest test file and select "Open Resolved Test Path" to open the resolved file or directory.

Supported patterns

Pattern A: Fixture chain + class attribute + string literal

# conftest.py
@pytest.fixture(scope="session")
def data_root_path(request) -> Path:
    return request.config.rootpath / "tests" / "module_a" / "data"

# test_example.py
@pytest.fixture()
def data_path(data_root_path: Path) -> Path:
    return data_root_path / "sub" / "test_example"

class TestExample:
    _data_dir = "TestExample"

    def test_case(self, data_path: Path) -> None:
        d = data_path / self._data_dir / "case1"  # <- resolves here

Pattern B: Local variable reuse

d = data_path / self._data_dir / "test_case"
result = read_data(d / "input.csv")  # <- resolves here

Pattern C: request.config.rootpath resolution

@pytest.fixture(scope="session")
def data_root_path(request) -> Path:
    return request.config.rootpath / "tests" / "data"  # <- resolves to workspace root

Installation

From .vsix file

  1. Build the package (see Development below)
  2. In VS Code / Cursor: Extensions > ... > Install from VSIX...
  3. Select the generated .vsix file

Usage

  1. Open a pytest test file (file path must contain tests/)
  2. Place your cursor on a Path expression (e.g., d / "input.csv")
  3. Right-click and select "Open Resolved Test Path"
  4. The resolved file or directory will open

Configuration

Setting Default Description
pytestPathResolver.pythonPath python3 Path to Python interpreter
pytestPathResolver.timeout 5000 Timeout for Python subprocess (ms)

Development

Prerequisites

  • Node.js 22+
  • Python 3.11+
  • pnpm
  • uv

Setup

pnpm install
uv sync

Build

npx tsc -p ./

Lint & Format

uv run ruff check python/ python_tests/
uv run ruff format python/ python_tests/

Run tests

uv run pytest python_tests/ -v

Debug

  1. Open this project in VS Code / Cursor
  2. Press F5 to launch the Extension Development Host
  3. Open a project with pytest test files in the new window

Package

npx @vscode/vsce package --no-dependencies --allow-missing-repository

Architecture

src/                    # TypeScript (VS Code extension)
  extension.ts          # Extension entry point
  pythonBridge.ts       # Spawns Python subprocess
  pathResolver.ts       # Handles resolve results (open file/directory)
  types.ts              # Shared type definitions

python/                 # Python (static analysis)
  ast_parser.py         # AST parsing: extracts Path segments from source
  fixture_resolver.py   # Resolves pytest fixtures across conftest.py chain
  path_evaluator.py     # Orchestrator: combines parser + resolver
  resolve_path.py       # CLI entry point (JSON stdin/stdout)

License

MIT

About

VSCode/Cursor extension that statically analyzes pathlib.Path expressions in pytest test files and resolves them to actual file paths

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors