Skip to main content
PyPI version Python versions Testing Coverage See Build Status on GitHub Actions

System/Integration tests can take a really long time.

And pytest-asyncio-concurrent A pytest plugin aiming to solve this by running asynchronous tests in true parallel, enabling faster execution for high I/O or network-bound test suites.

Unlike pytest-asyncio, which runs async tests sequentially, pytest-asyncio-concurrent takes advantage of Python’s asyncio capabilities to execute tests concurrently by specifying async group.

Note: This plugin would more or less Break Test Isolation Principle (for none function scoped fixture). Please make sure your tests is ok to run concurrently before you use this plugin.

Key Features

  • Giving the capability to run pytest async functions.

  • Providing granular control over Concurrency – See Async Group for details

    • Specifying Async Group to control tests that can run together.

    • Limitation: Only test functions defined under same direct parent can be put into same group.

  • asyncio_concurrent mark accept a timeout parameter, which would throw error when test reach the given time.

  • Compatible with pytest-asyncio.

Key Concept: Async Group

The plugin control tests concurrency by putting them into different groups.

  • Limitation: Each group can only contain tests under same direct parent. For example, class methods within same class, or functions within same file.

  • Explicitly specify group by providing group parameter to asyncio_concurrent mark.

    • All tests marked with same group name will be executed together.

    • All tests will be skipped if the Same Parent rule got violated.

  • Use default grouping strategy. The plugin accept --default-group-strategy cli parameter, or default_group_strategy in ini or toml file. Possible value: self, parent.

    • self(default): Each test will be executed by itself if no group provided.

    • parent: Tests will grouped by their parent node. For example, all method within same class will be grouped together.

Installation

You can install “pytest-asyncio-concurrent” via pip from PyPI:

$ pip install pytest-asyncio-concurrent

How this work?

This plugin extend pytest_runtestloop hook to handle async tests seperately.

In the ‘async loop’, tests are grouped by the group provided in the mark. and executed one group at a time.

In each group, instead of sequentially calling setup, call, teardown for individual test, these hooks are called for all tests in the group in batch.

The fixture lifecycle within group is handled by this plugin instead of pytest core, to work around pytest sequential test execution assumption.

Usage

Run test Sequentially

@pytest.mark.asyncio_concurrent
async def async_test_A():
    res = await wait_for_something_async()
    assert result.is_valid()

@pytest.mark.asyncio_concurrent
async def async_test_B():
    res = await wait_for_something_async()
    assert result.is_valid()

Run tests Concurrently

# the test below will run by itself
@pytest.mark.asyncio_concurrent
async def test_by_itself():
    res = await wait_for_something_async()
    assert result.is_valid()

# the two tests below will run concurrently
@pytest.mark.asyncio_concurrent(group="my_group")
async def test_groupA():
    res = await wait_for_something_async()
    assert result.is_valid()

# this one will have a 10s timeout
@pytest.mark.asyncio_concurrent(group="my_group", timeout=10)
async def test_groupB():
    res = await wait_for_something_async()
    assert result.is_valid()

Parametrized Tests

# the parametrized tests below will run sequential
@pytest.mark.asyncio_concurrent
@pytest.parametrize("p", [0, 1, 2])
async def test_parametrize_sequential(p):
    res = await wait_for_something_async()
    assert result.is_valid()

# the parametrized tests below will run concurrently
@pytest.mark.asyncio_concurrent(group="my_group")
@pytest.parametrize("p", [0, 1, 2])
async def test_parametrize_concurrent():
    res = await wait_for_something_async()
    assert result.is_valid()

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-asyncio-concurrent” is free and open source software

Release files for pytest-asyncio-concurrent 0.5.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-asyncio-concurrent 0.5.2
File Size Uploaded
pytest_asyncio_concurrent-0.5.2.tar.gz 18.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-asyncio-concurrent 0.5.2
File Interpreter ABI Platform
pytest_asyncio_concurrent-0.5.2-py3-none-any.whl Python 3 none any Details

Total release size: 32.9 kB

Release files / pytest_asyncio_concurrent-0.5.2.tar.gz

Download URL pytest_asyncio_concurrent-0.5.2.tar.gz
Size 18.5 kB
Tags Source
SHA-256 checksum
How to use checksums
35d5aab732746deb0f3d806a9261b47f0567410f5fa4ad8785e992d0df09f424
BLAKE2b-256 checksum
How to use checksums
0be5a79e3ffb4bbb13da16038695edff8ab9cbc2cc6b708916a94c10c28cd50e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.8

Release files / pytest_asyncio_concurrent-0.5.2-py3-none-any.whl

Download URL pytest_asyncio_concurrent-0.5.2-py3-none-any.whl
Size 14.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a32826103e2626dccb9d82e677fc2a031a506d3a677319d7478c74f173838b94
BLAKE2b-256 checksum
How to use checksums
045f36bacece56a2b9a62371a49728537f0d9330aa640797761628589362efe4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.8

Release history Release notifications | RSS feed

This release

0.5.2 This release

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page