Metadata-Version: 2.5
Name: testpick
Version: 0.2.0
Summary: Pick the tests affected by your git diff before you run them
Project-URL: Homepage, https://github.com/krauzX/testpick
Project-URL: Repository, https://github.com/krauzX/testpick
Project-URL: Issues, https://github.com/krauzX/testpick/issues
License: MIT
License-File: LICENSE
Keywords: ci,git,pytest,static-analysis,testing
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Software Development :: Testing
Requires-Python: >=3.10
Requires-Dist: click>=8.0
Requires-Dist: gitpython>=3.1
Requires-Dist: readchar>=4.0
Requires-Dist: rich>=13.0
Provides-Extra: dev
Requires-Dist: mypy>=1.10; extra == 'dev'
Requires-Dist: pytest-cov>=5.0; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: ruff>=0.5; extra == 'dev'
Provides-Extra: tree-sitter
Requires-Dist: tree-sitter-javascript>=0.23; extra == 'tree-sitter'
Requires-Dist: tree-sitter-python>=0.23; extra == 'tree-sitter'
Requires-Dist: tree-sitter-typescript>=0.23; extra == 'tree-sitter'
Requires-Dist: tree-sitter>=0.25; extra == 'tree-sitter'
Description-Content-Type: text/markdown

# testpick

[![CI](https://github.com/krauzX/testpick/actions/workflows/ci.yml/badge.svg)](https://github.com/krauzX/testpick/actions/workflows/ci.yml)

A Python CLI that looks at your git diff and tells you which tests are going to break. Before you run them.

## Why this exists

You change a function in `utils.py`. Now what? You could run the full test suite and wait, or you could guess
which tests matter and hope you're right. `pytest --lf` reruns failures only after they've already failed.
Jest's `--changedSince` follows direct file changes.

testpick parses your git diff, builds an import graph of your source files, and walks it backwards from what
you changed to find every test that depends on it — transitively. Each selected test comes with the chain that
explains why, so you can sanity-check the answer instead of trusting it.

## Getting started

```bash
git clone https://github.com/krauzX/testpick.git
cd testpick
pip install -e .

# optional but recommended: real AST parsing instead of the regex fallback
pip install -e ".[tree-sitter]"

# for development (tests, linter, type checker)
pip install -e ".[dev]"
```

Python 3.10+, runs on Linux, macOS and Windows.

## Usage

```bash
# what changed since your last commit, including uncommitted work?
testpick

# committed changes on your branch since it diverged from main
testpick --base main

# a single commit
testpick --commit abc123

# output for scripts and CI
testpick --format json
testpick --format github      # GitHub Actions annotations

# only analyze Python and TypeScript files
testpick --languages python,typescript
```

Everything resolves from the repository root, so it works from any directory inside the repo.

### Example output

```
┌─────────────────────────────────────────────────────────────────────────────┐
│ testpick - Test Selection Results                                           │
└─────────────────────────────────────────────────────────────────────────────┘

                     Changed Files
┌────────────┬────────────────────────────────────────┐
│ Status     │ File                                   │
├────────────┼────────────────────────────────────────┤
│ Modified   │ src/testpick/graph/builder.py          │
│ Modified   │ src/testpick/reporter/github.py        │
│ Modified   │ src/testpick/reporter/json_reporter.py │
│ Added      │ tests/test_selection_pipeline.py       │
└────────────┴────────────────────────────────────────┘

                                Selected Tests
┌───────┬──────────────────────────────────┬──────────────────────────────────┐
│ #     │ Test File                        │ Dependency Path                  │
├───────┼──────────────────────────────────┼──────────────────────────────────┤
│ 1     │ tests/test_cli.py                │ tests/test_cli.py ->             │
│       │                                  │ src/testpick/cli.py ->           │
│       │                                  │ src/testpick/graph/__init__.py   │
│       │                                  │ -> src/testpick/graph/builder.py │
│ 2     │ tests/test_graph/test_builder.py │ tests/test_graph/test_builder.py │
│       │                                  │ ->                               │
│       │                                  │ src/testpick/graph/__init__.py   │
│       │                                  │ -> src/testpick/graph/builder.py │
│ 3     │ tests/test_selection_pipeline.py │ direct                           │
│ 4     │ tests/test_selector/test_matche… │ tests/test_selector/test_matche… │
│       │                                  │ ->                               │
│       │                                  │ src/testpick/graph/__init__.py   │
│       │                                  │ -> src/testpick/graph/builder.py │
└───────┴──────────────────────────────────┴──────────────────────────────────┘

       Statistics
┌────────────────┬─────┐
│ Files Changed  │ 4   │
│ Tests Selected │ 4   │
│ Insertions     │ 258 │
│ Deletions      │ 25  │
└────────────────┴─────┘
```

`direct` means the test file itself changed. The JSON format carries the same
information, with chains as arrays:

```json
{
  "changed_files": [
    { "status": "M", "path": "src/utils.ts" }
  ],
  "test_files": [
    {
      "path": "tests/test_page.ts",
      "dependency_chain": ["tests/test_page.ts", "src/page.ts", "src/utils.ts"]
    }
  ],
  "stats": {
    "files_changed": 1,
    "insertions": 6,
    "deletions": 2,
    "tests_selected": 1
  }
}
```

## How it works

1. Read the diff with `git diff --name-status` and resolve every path against the repository root.
2. Parse each source file for imports. tree-sitter grammars are used when installed, otherwise a regex pass
   handles Python, TypeScript and JavaScript.
3. Resolve each import to a file: relative specifiers against the importing file's directory, absolute ones
   against `src/`, the project root, then the file's own directory — preferring the importing file's language
   when both a `.py` and a `.ts` file would match.
4. Walk reverse edges from the changed files with a breadth-first search, recording the parent of every hit so
   the chain can be explained afterwards.
5. Report the files that are test files, direct changes first.

## How it compares

Other tools pick tests from a diff too; they differ in how far they can see:

- **pytest-testmon** records which code each test actually executed, so it catches runtime coupling a static
  graph misses — but it needs a baseline run and is pytest-only.
- **Jest `--changedSince`** maps changed files to tests, but inside a Jest project and mostly direct
  relationships.
- **Nx and Turborepo affected tasks** pick packages to rebuild, which is coarser than picking individual tests.
- **snob, pytest-impacted, pytest-git-selector** do static graph selection like this one, but for Python only.

testpick's corner: one CLI covering Python and TypeScript/JavaScript, no baseline run required, and every
selection arrives with the chain that produced it.

## Limitations

Static analysis only sees imports it can read:

- Dynamic imports, dependency injection, plugin registries and other runtime wiring are invisible to it.
- A file deleted from the working tree is gone from the parse tree, so testpick reports the deletion but
  cannot trace what used to import it.
- `--languages` scopes which files are analyzed for dependencies; changed files are always reported.

Treat the output as a starting point. A full suite run still belongs in CI before a release.

## Running tests

```bash
pytest tests/ -v
pytest tests/ --cov=testpick
```

77 tests covering diff parsing, import resolution, graph building, the selection pipeline and all three
reporters, with 82% branch coverage. CI runs ruff, mypy and the suite on Python 3.10-3.13 across Linux, macOS
and Windows.

## License

MIT
