Skip to main content

Run all your asynchronous tests cooperatively.

Project description

https://img.shields.io/badge/code%20style-black-000000.svg

Use asyncio (cooperative multitasking) to run your I/O bound test suite efficiently and quickly.

import asyncio

import pytest

@pytest.mark.asyncio_cooperative
async def test_a():
    await asyncio.sleep(2)


@pytest.mark.asyncio_cooperative
async def test_b():
    await asyncio.sleep(2)
========== 2 passed in 2.05 seconds ==========

Quickstart

pip install pytest-asyncio-cooperative

Compatibility

pytest-asyncio is NOT compatible with this plugin. Please uninstall pytest-asyncio or pass this flag to pytest -p no:asyncio

Fixtures

It’s recommended that async tests use async fixtures.

import asyncio
import pytest


@pytest.fixture
async def my_fixture():
    await asyncio.sleep(2)
    yield "XXX"
    await asyncio.sleep(2)


@pytest.mark.asyncio_cooperative
async def test_a(my_fixture):
    await asyncio.sleep(2)
    assert my_fixture == "XXX"

Goals

  • Reduce the total run time of I/O bound test suites via cooperative multitasking

  • Reduce system resource usage via cooperative multitasking

Pros

  • An I/O bound test suite will run faster (ie. individual tests will take just as long. The total runtime of the entire test suite will be faster)

  • An I/O bound test suite will use less system resources (ie. only a single thread is used)

Cons

  • All tests MUST be coroutines (ie. have the async keyword)

  • Order of tests is not guaranteed (ie. some blocking operations might taken longer and affect the order of test results)

  • Tests MUST be isolated from each other (ie. NO shared resources, NO mock.patch)

  • There is NO parallelism, CPU bound tests will NOT get a performance benefit

Known Issues

Synchronous tests fail to run when async tests fail

Async tests are run BEFORE synchronous tests. If an async test fails then synchronous tests will NOT run. Therefore the synchronous tests will not report to have passed or failed.

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-asyncio-cooperative-0.13.0.tar.gz (6.7 kB view details)

Uploaded Source

File details

Details for the file pytest-asyncio-cooperative-0.13.0.tar.gz.

File metadata

File hashes

Hashes for pytest-asyncio-cooperative-0.13.0.tar.gz
Algorithm Hash digest
SHA256 079d841f8deef435d3e1adc50557bf9aa9b5f5fac2eee3c9e998102c5b51afe0
MD5 a702c8dc4fc8cc3ef89992c819e0433d
BLAKE2b-256 989afc327c8c34e7a388e2d7ef8223f29a45f48f0c8386ba894e6667fe76d328

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