Skip to main content

This plugin allows you to have async pytest fixtures and tests.

This plugin only supports python 3.6 and above.

The code here is influenced by pytest-asyncio but with some differences:

  • Error tracebacks from are from your tests, rather than asyncio internals

  • There is only one loop for all of the tests

  • You can manage the lifecycle of the loop yourself outside of pytest by using this plugin with your own loop

  • No need to explicitly mark your tests as async. (pytest-asyncio requires you mark your async tests because it also supports other event loops like curio and trio)

Like pytest-asyncio it supports async tests, coroutine fixtures and async generator fixtures.

Changelog

0.5.1 - 15 December 2019
  • Added an ini option default_alt_async_timeout for the default async timeout for fixtures and tests. The default is now 5 seconds. So say you wanted the default to be 3.5 seconds, you would set default_alt_async_timeout to be 3.5

0.5 - 16 August 2019
  • I made this functionality in a work project where I needed to run pytest.main from an existing event loop. I decided to make this it’s own module so I can have tests for this code.

Running from your own event loop

If you want to run pytest.main from with an existing event loop then you can do something like:

from alt_pytest_asyncio.plugin import AltPytestAsyncioPlugin, run_coro_as_main
import nest_asyncio
import asyncio
import pytest

async def my_tests():
   await do_some_setup_before_pytest()

   plugins = [AltPytestAsyncioPlugin(loop)]

   try:
       code = pytest.main([], plugins=plugins)
   finally:
       # Note that alt_pytest_asyncio will make sure all your async tests
       # have been finalized by this point, even if you KeyboardInterrupt
       # the pytest.main
       await do_any_teardown_after_pytest()

   if code != 0:
      raise Exception(repr(code))

if __name__ == '__main__':
   # Nest asyncio is required so that we can do run_until_complete in an
   # existing event loop - https://github.com/erdewit/nest_asyncio
   loop = asyncio.get_event_loop()
   nest_asyncio.apply(loop)

   run_coro_as_main(loop, my_tests())

Note that if you don’t need to run pytest from an existing event loop, you don’t need to do anything other than have alt_pytest_asyncio installed in your environment and you’ll be able to just use async keywords on your fixtures and tests.

Timeouts

alt_pytest_asyncio registers a pytest.mark.async_timeout(seconds) mark which you can use to set a timeout for your test.

For example:

import pytest

@pytest.mark.async_timeout(10)
async def test_something():
   await something_that_may_take_a_while()

This test will be cancelled after 10 seconds and raise an assertion error saying the test took too long and the file and line number where the test is.

You can also use the async_timeout mark on coroutine fixtures:

import pytest

@pytest.fixture()
@pytest.mark.async_timeout(0.5)
async def my_amazing_fixture():
   await asyncio.sleep(1)
   return 1

And you can have a timeout on generator fixtures:

import pytest

@pytest.fixture()
@pytest.mark.async_timeout(0.5)
async def my_amazing_fixture():
   try:
      await asyncio.sleep(1)
      yield 1
   finally:
      await asyncio.sleep(1)

Note that for generator fixtures, the timeout is applied in whole to both the setup and finalization of the fixture. As in the real timeout for the entire fixture is essentially double the single timeout specified.

The default timeout is 5 seconds. You can change this default by setting the default_alt_async_timeout option to the number of seconds you want.

Download files

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

Source Distribution

alt_pytest_asyncio-0.5.1.tar.gz (6.2 kB view details)

Uploaded Source

File details

Details for the file alt_pytest_asyncio-0.5.1.tar.gz.

File metadata

  • Download URL: alt_pytest_asyncio-0.5.1.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.8.0

File hashes

Hashes for alt_pytest_asyncio-0.5.1.tar.gz
Algorithm Hash digest
SHA256 45b8abf4059ed91f3f8035240df23f9994d78f9bf255d89cc9fb89cafff2038b
MD5 604a12bc34f4a66c148442778c1bc75b
BLAKE2b-256 d10ba8807f1640b2da3673d71ec457c89e7fb7634f09914f1e3464ecbad0f512

See more details on using hashes here.

Release history Release notifications | RSS feed

0.9.6

2 files

0.9.5

2 files

0.9.4

2 files

0.9.3

2 files

0.9.2

2 files

0.9.1

2 files

0.9.0

2 files

0.8.2

2 files

0.8.1

2 files

0.8.0

2 files

0.7.2

2 files

0.7.1

2 files

0.7.0

2 files

0.6.0

1 file

0.5.4

1 file

0.5.3

1 file

0.5.2

1 file

This release

0.5.1 This release

1 file

0.5

1 file

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