Skip to main content

A project-agnostic Python CLI tool that reads any source file, analyzes its dependency graph via AST parsing, and generates everything you need to start testing

Project description

TestSmith

A project-agnostic Python test scaffold generator

TestSmith analyzes your Python source code via AST parsing and automatically generates pytest test scaffolds, mock fixtures, and test infrastructure—so you can focus on writing assertions, not boilerplate.

CI License: MIT


How It Works

TestSmith follows a simple pipeline: Analyze → Classify → Generate

graph LR
    A[Source File] -->|AST Parse| B[Analyze Imports]
    B --> C{Classify Import}
    C -->|stdlib| D[Skip]
    C -->|internal| E[Add to conftest paths]
    C -->|external| F[Generate Mock Fixture]
    
    E --> G[Generate Test File]
    F --> G
    G --> H[Update conftest.py]
    
    style A fill:#4a90d9,color:#fff
    style B fill:#7b68ee,color:#fff
    style G fill:#50c878,color:#fff
    style H fill:#ffa07a,color:#fff

What happens:

  1. Analyze: Parse source file with Python's AST to extract imports, classes, and functions
  2. Classify: Categorize each import as stdlib (skip), internal (add path), or external (mock)
  3. Generate: Create test file with proper structure, mock fixtures for external dependencies
  4. Update: Modify conftest.py to ensure internal imports resolve correctly

Features

Core Functionality

  • Zero Configuration: Works on any Python project structure
  • Smart Import Classification: Automatically detects stdlib, internal, and external dependencies
  • Shared Mock Fixtures: Generates reusable *.fixture.py files for external dependencies
  • Idempotent: Safe to run multiple times—won't duplicate or break existing tests
  • Project-Aware: Auto-detects project root and package structure

Advanced Features

  • 🤖 LLM Test Body Generation: Use --generate-bodies to fill in test assertions with AI
  • 📊 Dependency Graph Visualization: Generate Mermaid diagrams with --graph
  • 🧹 Fixture Pruning: Remove unused fixtures with --prune
  • 📈 Coverage Gap Analysis: Identify untested code with --coverage-gaps
  • 👀 Watch Mode: Auto-regenerate tests on file changes with --watch

Distribution

  • 📦 Standalone Binaries: No Python installation required
  • 🐍 PyPI Package: Install with pipx or pip
  • 🖥️ Multi-Platform: Linux, macOS (Intel + Apple Silicon), Windows

Installation

Option 1: Binary (No Python Required)

Download the latest binary for your platform from GitHub Releases:

# Linux
curl -LO https://github.com/orieken/testsmith/releases/latest/download/testsmith-linux-amd64
chmod +x testsmith-linux-amd64
sudo mv testsmith-linux-amd64 /usr/local/bin/testsmith

# macOS (Intel)
curl -LO https://github.com/orieken/testsmith/releases/latest/download/testsmith-macos-amd64
chmod +x testsmith-macos-amd64
sudo mv testsmith-macos-amd64 /usr/local/bin/testsmith

# macOS (Apple Silicon)
curl -LO https://github.com/orieken/testsmith/releases/latest/download/testsmith-macos-arm64
chmod +x testsmith-macos-arm64
sudo mv testsmith-macos-arm64 /usr/local/bin/testsmith

# Windows (PowerShell)
Invoke-WebRequest -Uri https://github.com/orieken/testsmith/releases/latest/download/testsmith-windows-amd64.exe -OutFile testsmith.exe

Option 2: pipx (Recommended for Python Users)

pipx install testsmith

Option 3: pip

pip install testsmith

Quick Start

1. Initialize TestSmith in Your Project

cd your-project/
testsmith --init

This creates:

  • tests/ directory
  • tests/fixtures/ for shared mocks
  • tests/fixtures/conftest.py for fixture registration

2. Generate Tests for a File

testsmith src/services/payment.py

This creates:

  • tests/src/services/test_payment.py with test scaffolds
  • Mock fixtures in tests/fixtures/ for external dependencies
  • Updates conftest.py with necessary paths

3. Run Your Tests

pytest tests/

Usage Examples

Basic Test Generation

# Generate test for a single file
testsmith src/api/users.py

# Generate tests for all untested files
testsmith --all

# Generate tests for a directory
testsmith --path src/services/

Advanced Features

# Generate test bodies with AI (requires ANTHROPIC_API_KEY)
testsmith src/api/users.py --generate-bodies

# Visualize dependency graph
testsmith --graph --graph-output deps.md

# Find and remove unused fixtures
testsmith --prune --confirm

# Analyze coverage gaps and prioritize testing
testsmith --coverage-gaps

# Watch mode: auto-regenerate on file changes
testsmith --watch

Dry Run Mode

# Preview what would be generated without writing files
testsmith src/api/users.py --dry-run

Configuration

TestSmith works with zero configuration, but you can customize behavior in pyproject.toml:

[tool.testsmith]
test_root = "tests/"
fixture_root = "tests/fixtures/"
exclude_dirs = ["venv", ".venv", "node_modules", "__pycache__"]

Architecture

TestSmith is organized into distinct layers:

src/testsmith/
├── cli.py                  # CLI entry point
├── core/                   # Core analysis engine
│   ├── source_analyzer.py  # AST parsing & import extraction
│   ├── import_classifier.py # stdlib/internal/external classification
│   └── project_detector.py # Project structure detection
├── generation/             # Code generation
│   ├── test_generator.py   # Test file generation
│   ├── fixture_generator.py # Mock fixture generation
│   └── conftest_updater.py # conftest.py management
├── llm/                    # LLM integration
│   └── test_body_generator.py # AI-powered test bodies
├── visualization/          # Dependency graphs
│   ├── graph_builder.py    # Build dependency graphs
│   └── mermaid_renderer.py # Render Mermaid diagrams
├── maintenance/            # Maintenance tools
│   ├── fixture_pruner.py   # Remove unused fixtures
│   └── coverage_analyzer.py # Coverage gap analysis
└── support/                # Shared utilities
    ├── config.py           # Configuration management
    ├── models.py           # Data models
    └── templates.py        # Code templates

See docs/architecture.md for detailed design documentation.


Platform Support

  • Linux: Ubuntu 20.04+, Debian 10+, RHEL 7+, Fedora 30+, CentOS 7+
  • macOS: macOS 11+ (Big Sur and later), Intel and Apple Silicon
  • Windows: Windows 10, Windows 11, Windows Server 2019+

Development

Local Setup

# Clone repository
git clone https://github.com/orieken/testsmith.git
cd testsmith

# Install dependencies (including dev tools)
poetry install --with dev,build

Using Source Version Locally

To use the development version of TestSmith in other projects on your machine:

  1. Activate the environment:

    # Activate the virtual environment in your shell
    source $(poetry env info --path)/bin/activate
    
  2. Navigate to your target project:

    cd ../my-other-project
    
  3. Run TestSmith:

    # The 'testsmith' command is now available in your PATH
    testsmith --generate-bodies src/app.py
    

Development Tasks

TestSmith uses poethepoet for task automation. Run poe to see all available tasks:

# Testing
poe test              # Run all tests with coverage
poe test-unit         # Run only unit tests
poe test-integration  # Run only integration tests
poe test-fast         # Stop on first failure, run failed tests first

# Linting & Formatting
poe lint              # Check code with ruff
poe format            # Format code with black
poe format-check      # Check formatting without modifying
poe check             # Run all checks (lint + format + test)

# Building
poe build             # Build binary with PyInstaller
poe build-test        # Build and test binary

# Cleaning
poe clean             # Remove build artifacts and caches

# Development
poe install           # Install all dependencies
poe version           # Show current version

Manual Commands (if not using poe)

# Run tests
poetry run pytest tests/ --cov=src/testsmith --cov-report=term-missing

# Lint
poetry run ruff check src/ tests/
poetry run black --check src/ tests/

Build Binary Locally

# Using poe (recommended)
poe build

# Or using scripts directly
# Linux/macOS
./scripts/build-local.sh

# Windows
.\scripts\build-local.ps1

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.


License

MIT License - see LICENSE for details.

Copyright (c) 2026 Oscar Rieken oriekenjr@gmail.com

Portfolio: https://rieken-portfolio.netlify.app/


About

TestSmith was built as a learning project to explore static code analysis, test automation, and AI-assisted development. Read more about the story and philosophy behind the project in ABOUT.md.


Acknowledgments

Built with:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

testsmith-1.1.7.tar.gz (41.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

testsmith-1.1.7-py3-none-any.whl (49.7 kB view details)

Uploaded Python 3

File details

Details for the file testsmith-1.1.7.tar.gz.

File metadata

  • Download URL: testsmith-1.1.7.tar.gz
  • Upload date:
  • Size: 41.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for testsmith-1.1.7.tar.gz
Algorithm Hash digest
SHA256 f526ba92aac0ce8c1c6ef8a2f96efb85c34e24772adb2ed7c09e7388c3482362
MD5 8d44ba27e9912902bee902848621f1ed
BLAKE2b-256 85acbc059fa4b6625e78bd2fb6957ba88ee3ebdb0b5cf55a4a4d9675e3a60a42

See more details on using hashes here.

Provenance

The following attestation bundles were made for testsmith-1.1.7.tar.gz:

Publisher: release.yml on orieken/testsmith

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file testsmith-1.1.7-py3-none-any.whl.

File metadata

  • Download URL: testsmith-1.1.7-py3-none-any.whl
  • Upload date:
  • Size: 49.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for testsmith-1.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 4574b15ec0742620fb36a83be3372ff36b0047597cd3f5b696b5d63f06509626
MD5 1e7899de643a0c5d729563e30ca68943
BLAKE2b-256 f5d598fbc90342fceb43d319a96906f47ae8ff07a7445d58891707acc53f481f

See more details on using hashes here.

Provenance

The following attestation bundles were made for testsmith-1.1.7-py3-none-any.whl:

Publisher: release.yml on orieken/testsmith

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page