Skip to main content

Alternative pytest plugin to pytest-asyncio

Project description

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.


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)]

       code = pytest.main([], plugins=plugins)
       # 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 -
   loop = asyncio.get_event_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.


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

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

async def my_amazing_fixture():
   await asyncio.sleep(1)
   return 1

And you can have a timeout on generator fixtures:

import pytest

async def my_amazing_fixture():
      await asyncio.sleep(1)
      yield 1
      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 value is 5 seconds any the closest async_timeout mark will be used. i.e. on the class your test is defined on, or the module, or the session.

Project details

Release history Release notifications

This version


Download files

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

Files for alt-pytest-asyncio, version 0.5
Filename, size File type Python version Upload date Hashes
Filename, size alt_pytest_asyncio-0.5.tar.gz (6.0 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page