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=[
        'tests/unit/test_database.py',  # Simple file path
        '**/test_integration_*'          # Wildcard pattern
    ]
)
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 = [
    # Simple file paths (automatically matches all tests in the file)
    "app/tests/unit/test_database.py",
    "app/tests/unit/routes/test_organizations.py",
    
    # Wildcard patterns
    "**/test_database_*",
    "**/test_migration_*",
    "tests/e2e/**"
]

Pattern Matching:

Para-pytest automatically handles test node IDs (like test_file.py::test_name). You can use:

  1. Simple file paths - No wildcards needed!

    • "app/tests/unit/test_database.py" matches all tests in that file
    • Automatically expands to match test_database.py::test_function
  2. Wildcard patterns - For matching multiple files

    • "**/test_database_*" - Any test file starting with test_database_
    • "tests/e2e/**" - All tests in 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:
    • "tests/unit/test_database.py" (specific file)
    • "**/test_database_*" (any file starting with test_database_)
  • Integration tests:
    • "**/test_integration_*", "tests/integration/**"
  • E2E tests:
    • "tests/e2e/**", "tests/functional/**"
  • Any test modifying shared resources

Note: For specific files, you don't need wildcards - just use the file path!

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.3.tar.gz (10.9 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.3-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: para_pytest-0.1.3.tar.gz
  • Upload date:
  • Size: 10.9 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.3.tar.gz
Algorithm Hash digest
SHA256 6552cbedaa8665480826a93bdbbba6da5c83577ccabd195e2877a8c32e362cdf
MD5 7cac0c1c5808ba5a812bbabeb3bf2f26
BLAKE2b-256 716be3ff8e8bde31842da53acade328297b73e47eae36144088ac5fc799b8b57

See more details on using hashes here.

File details

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

File metadata

  • Download URL: para_pytest-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.5 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 81cdd191cdace964020232b668d16a9191b6d348e9b33d26e8a35a2ec0d87d7b
MD5 f1985175f73ed461c35aaa395d331447
BLAKE2b-256 71169a3b53b3b6117e3056f2c4df82e1db23b146694a09782214dd2503cc5c9c

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