A simple pytest plugin to run tests concurrently
Project description
A simple pytest plugin to run tests concurrently
This pytest plugin takes a set of tests that would be normally be run serially and execute them in parallel.
The main goal of pytest-run-parallel is to discover thread-safety issues that could exist when using C libraries, this is of vital importance after PEP703, which provides a path for a CPython implementation without depending on the Global Interpreter Lock (GIL), thus allowing for proper parallelism in programs that make use of the CPython interpreter.
For more information about C thread-safety issues, please visit the free-threaded community guide at https://py-free-threading.github.io/
Features
- Two global CLI flags:
--parallel-threads to run a test suite in parallel
--iterations to run multiple times in each thread
- Three corresponding markers:
pytest.mark.parallel_threads(n) to mark a single test to run in parallel in n threads
pytest.mark.thread_unsafe to mark a single test to run in a single thread. It is equivalent to using pytest.mark.parallel_threads(1)
pytest.mark.iterations(n) to mark a single test to run n times in each thread
- And the corresponding fixtures:
num_parallel_threads: The number of threads the test will run in
num_iterations: The number of iterations the test will run in each thread
Requirements
pytest-run-parallel depends exclusively on pytest.
Installation
You can install “pytest-run-parallel” via pip from PyPI:
$ pip install pytest-run-parallel
Usage
This plugin has two modes of operation, one via the --parallel-threads and --iterations pytest CLI flags, which allows a whole test suite to be run in parallel:
pytest --parallel-threads=10 --iterations=10 tests
By default, the value for both flags will be 1, thus not modifying the usual behaviour of pytest except when the flag is set.
Note that using pytest-xdist and setting iterations to a number greater than one might cause tests to run even more times than intended.
The other mode of operation occurs at the individual test level, via the pytest.mark.parallel_threads and pytest.mark.iterations markers:
# test_file.py
import pytest
@pytest.fixture
def my_fixture():
...
@pytest.mark.parallel_threads(2)
@pytest.mark.iterations(10)
def test_something_1():
# This test will be run in parallel using two concurrent threads
# and 10 times in each thread
...
@pytest.mark.parametrize('arg', [1, 2, 3])
@pytest.mark.parallel_threads(3)
def test_fixture(my_fixture, arg):
# pytest markers and fixtures are supported as well
...
Both modes of operations are supported simultaneously, i.e.,
# test_something_1 and test_fixture will be run using their set number of
# threads; other tests will be run using 5 threads.
pytest -x -v --parallel-threads=5 test_file.py
Additionally, pytest-run-parallel exposes the num_parallel_threads and num_iterations fixtures which enable a test to be aware of the number of threads that are being spawned and the number of iterations each test will run:
# test_file.py
import pytest
def test_skip_if_parallel(num_parallel_threads):
if num_parallel_threads > 1:
pytest.skip(reason='does not work in parallel')
...
Finally, the thread_comp fixture allows for parallel test debugging, by providing an instance of ThreadComparator, whose __call__ method allows to check if all the values produced by all threads during an specific execution step are the same:
# test_file.py
def test_same_execution_values(thread_comp):
a = 2
b = [3, 4, 5]
c = None
# Check that the values for a, b, c are the same across tests
thread_comp(a=a, b=b, c=c)
Contributing
Contributions are very welcome. Tests can be run with tox, please ensure the coverage at least stays the same before you submit a pull request.
License
Distributed under the terms of the MIT license, “pytest-run-parallel” is free and open source software
Issues
If you encounter any problems, please file an issue along with a detailed description.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pytest_run_parallel-0.2.0.tar.gz
.
File metadata
- Download URL: pytest_run_parallel-0.2.0.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | accc9cd7eb298acbdd7d864ce3a38daaa45ab6a92e12b9c5db81b65bb19f609f |
|
MD5 | b560bef197754e0941c68c54ecfa5a9c |
|
BLAKE2b-256 | c55a72a2a066034f807226d85970e7f79c9342329fb723cde9bb35b70680e038 |
File details
Details for the file pytest_run_parallel-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: pytest_run_parallel-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 562d312e4351b0975c9520fc8783dbc8513dacf61ff767cbc78f3782a34493b7 |
|
MD5 | 166379071ccc4295ee54f9a19126000e |
|
BLAKE2b-256 | fc299df63c86a73a10a2f1e15bd0ae24c19899ab9745514999751aec248c9afa |