pytest-parallel
pytest-parallel is a pytest plugin that enables parallel and concurrent testing by utilizing both multiprocessing (workers) and multithreading (tests per worker). It is designed for specific use cases, such as Selenium tests, where tests can be thread-safe, benefit from non-blocking I/O, and manage minimal state in the Python environment. It is not intended as a direct replacement for pytest-xdist, which focuses solely on process-based parallelism for general test distribution. The current version is 0.1.1. The project appears to be unmaintained.
Warnings
- breaking The pytest-parallel project (version 0.1.1) is explicitly marked as unmaintained on its GitHub repository. It relies on patching pytest internals, which can lead to instability or breakage with newer versions of pytest as the internal structure changes.
- gotcha Starting with Python 3.8 on macOS, forking behavior for multiprocessing is forced at the expense of safety, and `spawn` is the default start method. Using `pytest-parallel` (which leverages multiprocessing) on macOS with Python 3.8+ might expose subprocess stability issues due to this change.
- gotcha Parallel and concurrent testing with `pytest-parallel` requires careful test isolation. Tests that modify shared global state, rely on specific test ordering, or use non-thread-safe fixtures (e.g., `capsys`, `monkeypatch` as warned by similar plugins) will likely lead to flaky tests, unpredictable failures, or incorrect results.
- gotcha While `pytest-parallel` enables concurrent execution, it's not a universal speedup solution. For test suites consisting of many very fast unit tests that don't involve I/O waits, the overhead of managing processes and threads can sometimes make parallel execution slower than sequential execution.
Install
-
pip install pytest-parallel
Quickstart
import time
import pytest
# test_example.py
def test_fast_operation_1():
time.sleep(0.01)
assert True
def test_fast_operation_2():
time.sleep(0.01)
assert True
def test_fast_operation_3():
time.sleep(0.01)
assert True
def test_slow_operation_1():
time.sleep(0.1)
assert True
def test_slow_operation_2():
time.sleep(0.1)
assert True
# Run with: pytest --workers 2 --tests-per-worker 2
# This will execute tests using 2 processes, with each process running up to 2 tests concurrently.