# Pytest Asyncio > In order to understand how pytest-asyncio works, it helps to understand how pytest collectors work. --- # Source: pytest-asyncio GitHub Repository ======== # Concepts # asyncio event loops In order to understand how pytest-asyncio works, it helps to understand how pytest collectors work. If you already know about pytest collectors, please [skip ahead [pytest-asyncio-event-loops]]. Otherwise, continue reading. Let's assume we have a test suite with a file named *test_all_the_things.py* holding a single test, async or not: ```python [See: concepts_function_scope_example.py] ``` The file *test_all_the_things.py* is a Python module with a Python test function. When we run pytest, the test runner descends into Python packages, modules, and classes, in order to find all tests, regardless whether the tests will run or not. This process is referred to as *test collection* by pytest. In our particular example, pytest will find our test module and the test function. We can visualize the collection result by running `pytest --collect-only`:: [Module test_all_the_things.py] [Function test_runs_in_a_loop] The example illustrates that the code of our test suite is hierarchical. Pytest uses so called *collectors* for each level of the hierarchy. Our contrived example test suite uses the *Module* and *Function* collectors, but real world test code may contain additional hierarchy levels via the *Package* or *Class* collectors. There's also a special *Session* collector at the root of the hierarchy. You may notice that the individual levels resemble the possible [scopes of a pytest fixture.](https://docs.pytest.org/en/7.4.x/how-to/fixtures.html#scope-sharing-fixtures-across-classes-modules-packages-or-session)_ Pytest-asyncio provides one asyncio event loop for each pytest collector. By default, each test runs in the event loop provided by the *Function* collector, i.e. tests use the loop with the narrowest scope. This gives the highest level of isolation between tests. If two or more tests share a common ancestor collector, the tests can be configured to run in their ancestor's loop by passing the appropriate *loop_scope* keyword argument to the *asyncio* mark. For example, the following two tests use the asyncio event loop provided by the *Module* collector: ```python [See: concepts_module_scope_example.py] ``` It's highly recommended for neighboring tests to use the same event loop scope. For example, all tests in a class or module should use the same scope. Assigning neighboring tests to different event loop scopes is discouraged as it can make test code hard to follow. # Test discovery modes Pytest-asyncio provides two modes for test discovery, *strict* and *auto*. This can be set through Pytest's `--asyncio-mode` command line flag, or through the configuration file: ```toml [tool.pytest.ini_options] asyncio_mode = "auto" # or "strict" ## Strict mode In strict mode pytest-asyncio will only run tests that have the *asyncio* marker and will only evaluate async fixtures decorated with `@pytest_asyncio.fixture`. Test functions and fixtures without these markers and decorators will not be handled by pytest-asyncio. This mode is intended for projects that want so support multiple asynchronous programming libraries as it allows pytest-asyncio to coexist with other async testing plugins in the same codebase. Pytest automatically enables installed plugins. As a result pytest plugins need to coexist peacefully in their default configuration. This is why strict mode is the default mode. ## Auto mode In *auto* mode pytest-asyncio automatically adds the *asyncio* marker to all asynchronous test functions. It will also take ownership of all async fixtures, regardless of whether they are decorated with `@pytest.fixture` or `@pytest_asyncio.fixture`. This mode is intended for projects that use *asyncio* as their only asynchronous programming library. Auto mode makes for the simplest test and fixture configuration and is the recommended default. If you intend to support multiple asynchronous programming libraries, e.g. *asyncio* and *trio*, strict mode will be the preferred option. # Test execution and concurrency pytest-asyncio runs async tests sequentially, just like how pytest runs synchronous tests. Each asynchronous test runs within its assigned event loop. For example, consider the following two tests: ```python [See: concepts_concurrent_execution_example.py] ``` This sequential execution is intentional and important for maintaining test isolation. Running tests concurrently could introduce race conditions and side effects where one test could interfere with another, making test results unreliable and difficult to debug. --- # Source: pytest-asyncio GitHub Repository ========================== # Welcome to pytest-asyncio! concepts how-to-guides/index reference/index support pytest-asyncio is a [pytest](https://docs.pytest.org/en/latest/contents.html) plugin. It facilitates testing of code that uses the [asyncio](https://docs.python.org/3/library/asyncio.html) library. Specifically, pytest-asyncio provides support for coroutines as test functions. This allows users to *await* code inside their tests. For example, the following code is executed as a test item by pytest: ```python @pytest.mark.asyncio async def test_some_asyncio_code(): res = await library.do_something() assert b"expected result" == res Note that test classes subclassing the standard [unittest](https://docs.python.org/3/library/unittest.html)_ library are not supported. Users are advised to use [unittest.IsolatedAsyncioTestCase](https://docs.python.org/3/library/unittest.html#unittest.IsolatedAsyncioTestCase)_ or an async framework such as [asynctest](https://asynctest.readthedocs.io/en/latest)_. pytest-asyncio is available under the [Apache License 2.0](https://github.com/pytest-dev/pytest-asyncio/blob/main/LICENSE). --- # Source: pytest-asyncio GitHub Repository =============== # Getting support # Enterprise support [Tidelift](https://www.tidelift.com) works with maintainers of numerous open source projects to ensure enterprise-grade support for your software supply chain. The Tidelift subscription includes security updates, verified license compliance, continuous software maintenance, and more. As a result, you get the guarantees provided by commercial software for the open source packages you use. Consider [signing up for the Tidelift subscription](https://tidelift.com/subscription/pkg/pypi-pytest-asyncio?utm_source=pypi-pytest-asyncio&utm_medium=referral&utm_campaign=enterprise)_. # Direct maintainer support If you require commercial support outside of the Tidelift subscription, reach out to [Michael Seifert,](https://seifertm.de)_ one of the project's maintainers. # Community support The GitHub page of pytest-asyncio offers free community support on a best-effort basis. Please use the [issue tracker](https://github.com/pytest-dev/pytest-asyncio/issues)_ to report bugs and the Matrix chat room [#pytest-asyncio:matrix.org](https://matrix.to/#/#pytest-asyncio:matrix.org)_ or [GitHub discussions](https://github.com/pytest-dev/pytest-asyncio/discussions)_ to ask questions.