Skip to main content

Run pytest tests in parallel chunks for faster test execution

Project description

Parallel Pytest (para-pytest)

Run pytest tests in parallel chunks for significantly faster test execution.

Installation

pip install para-pytest

Quick Start

# Run with default settings (4 chunks)
para-pytest

# Specify number of chunks
para-pytest --chunks 8

# Specify test path
para-pytest --path tests/

CLI Options

para-pytest [OPTIONS]

Options:
  --chunks N      Number of parallel chunks (default: 4)
  --path PATH     Path to tests (default: current directory)
  --debug         Show detailed chunking and pattern matching info

Useful for debugging:

# See which tests match serial patterns and how tests are chunked
para-pytest --path tests/ --debug

Use Cases

Local Development

Speed up your test suite during development:

# Before (sequential execution)
pytest tests/

# After (parallel execution)
para-pytest --chunks 4

Continuous Integration

Add to your GitHub Actions workflow:

name: Tests

on: [push, pull_request]

jobs: 
	test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-python@v5
        with:
          python-version: '3.12.3'

      - name: Install dependencies
        run: |
          pip install -r requirements.txt
          pip install para-pytest

      - name: Run tests
        run: para-pytest --chunks 4 --path tests/

Or use the Github Action:

- uses: ALHelton/parallel-pytest@v1
  with:
    chunks: 8
    path: tests/

Python API

from para_pytest import ParaPytestRunner

# Serial patterns are automatically loaded from pyproject.toml
runner = ParaPytestRunner(chunks=4, pytest_args=['tests/'])
exit_code = runner.run()

# Or explicitly pass serial patterns (overrides pyproject.toml)
runner = ParaPytestRunner(
    chunks=4,
    pytest_args=['tests/'],
    serial_patterns=['**/test_database_*', '**/test_integration_*']
)
exit_code = runner.run()

Configuration (Optional)

Serial Test Patterns

Some tests can't run in parallel (e.g., database tests, integration tests with shared state). Configure these to run serially by adding to your existing pyproject.toml:

[tool.para-pytest]
serial_patterns = [
    "**/test_database_*",
    "**/test_migration_*",
    "**/test_integration_*",
    "tests/e2e/**"
]

Pattern examples:

  • **/test_database_* - Any test file starting with test_database_ anywhere in your project
  • tests/e2e/** - All tests in the tests/e2e/ directory and subdirectories
  • **/test_*_integration.py - Any test file ending with _integration.py

That's it! No external dependencies required - the configuration is parsed using built-in Python modules.

Performance

Real-world example with 2000+ tests:

Method Time Speedup
pytest tests/ 50s 1x
para-pytest --chunks 4 15s 3.3x
para-pytest --chunks 8 10s 5x

Performance varies based on test suite characteristics

How It Works

  1. Loads serial patterns from pyproject.toml (if configured)
  2. Collects all tests from pytest
  3. Separates tests into parallel and serial groups based on patterns
  4. Splits parallel tests into equal chunks
  5. Runs parallel chunks concurrently using asyncio
  6. Runs serial tests sequentially (if any)
  7. Aggregates and displays results

Troubleshooting

Tests Fail in Parallel But Pass Normally?

Some tests can't run in parallel due to shared state (databases, files, etc.). Configure them to run serially:

[tool.para-pytest]
serial_patterns = [
    "**/test_database_*",
    "**/test_integration_*",
]

Run with --debug to see which tests are being run serially:

para-pytest --path tests/ --debug

Common Patterns for Serial Tests

  • Database tests: **/test_database_*, **/test_migration_*
  • Integration tests: **/test_integration_*, tests/integration/**
  • E2E tests: tests/e2e/**, tests/functional/**
  • Any test modifying shared resources

Requirements

  • Python 3.8+
  • pytest 7.0+

License

MIT

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

para_pytest-0.1.2.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

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

para_pytest-0.1.2-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file para_pytest-0.1.2.tar.gz.

File metadata

  • Download URL: para_pytest-0.1.2.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for para_pytest-0.1.2.tar.gz
Algorithm Hash digest
SHA256 f5cdca75dc3250fd8d86ca2829bfc2bd886b3c26906f0bd044cadc4cd6cf01c1
MD5 ae1420446cf02670d6ca60f76817bb11
BLAKE2b-256 975e7fd85311e091cea867a5d50ded0c6f31835dca4444023b1d9c46b29e1cbd

See more details on using hashes here.

File details

Details for the file para_pytest-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: para_pytest-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for para_pytest-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 391914388f75fdaf156c26142a76c36790419c606a6c01775c98fa7bbaa354f1
MD5 0bab94b3d1a4896f24dacbc86f99ca80
BLAKE2b-256 20f8613bfcc8b95689549b0e93e41f57ca424526433dba16c6c7f7f45030ecad

See more details on using hashes here.

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