Skip to main content

A simple pytest plugin to run tests concurrently

Project description

PyPI version Python versions See Build Status on GitHub Actions

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

  • A global CLI flag --parallel-threads to run a test suite in parallel

  • A marker pytest.mark.parallel_threads to mark a single test to run in parallel

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 pytest CLI flag, which allows a whole test suite to be run in parallel:

pytest --parallel-threads=10 tests

By default, the value of parallel-threads will be 1, thus not modifying the usual behaviour of pytest except when the flag is set.

The other mode of operation occurs at the individual test level, via the pytest.mark.parallel_threads marker:

# test_file.py
import pytest

@pytest.fixture
def my_fixture():
    ...

@pytest.mark.parallel_threads(2)
def test_something_1():
    # This test will be run in parallel using two concurrent threads
    ...

@pytest.mark.parametrize('arg', [1, 2, 3])
@pytest.mark.parallel_threads(3)
def test_fixutre(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_fixutre 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 fixture which enable a test to be aware of the number of threads that are being spawned:

# 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


Download files

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

Source Distribution

pytest_run_parallel-0.1.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

pytest_run_parallel-0.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file pytest_run_parallel-0.1.0.tar.gz.

File metadata

  • Download URL: pytest_run_parallel-0.1.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for pytest_run_parallel-0.1.0.tar.gz
Algorithm Hash digest
SHA256 271854a2919aaff4e2a39bc2094bd2f96aa32fba9e51a995405ead35b74cc062
MD5 6a465494ec92dbe08234b1d99b741cea
BLAKE2b-256 54193ecc23763625d62866db55098c59b05285d8c82019cbb376ca7b97b9d68c

See more details on using hashes here.

File details

Details for the file pytest_run_parallel-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_run_parallel-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 13d8579d39d60d5d77695e6bc292daa3352a5974eb446819f52fba4e20bb0d0f
MD5 626ac8d5eef1b68eb685ba79e3babc53
BLAKE2b-256 8e5b635b5977f6f74a82f3c5bb539ea3a2fcf381efe9bcbd776d06e63248ca5b

See more details on using hashes here.

Supported by

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