| 1 |
======================================================== |
| 2 |
How to configure event loop factories from the test item |
| 3 |
======================================================== |
| 4 |
|
| 5 |
``pytest_asyncio_loop_factories`` is called with the current pytest ``item``. |
| 6 |
Use that item to decide which named event loop factories are available for the test being collected. |
| 7 |
|
| 8 |
For example, a hook can inspect the test's fixtures and return a different factory mapping for tests that request a particular fixture. |
| 9 |
In ``conftest.py``, check the current item's fixture names and build the factory mapping for that item: |
| 10 |
|
| 11 |
.. include:: configure_loop_factories_per_test/conftest.py |
| 12 |
:code: python |
| 13 |
|
| 14 |
Then request the fixture from tests that should use the custom factory: |
| 15 |
|
| 16 |
.. include:: configure_loop_factories_per_test/test_extra_loop_factories.py |
| 17 |
:code: python |
| 18 |
|
| 19 |
In this example, ``test_runs_with_default_factory_only`` is parametrized only over ``default``, while ``test_runs_with_custom_factory_only`` is parametrized only over ``custom``. |
| 20 |
|
| 21 |
The same pattern works with any information available from the current pytest item, such as fixture names, markers, node IDs, or file paths. |
| 22 |
|
| 23 |
Because this is a standard pytest hook, its placement also matters. |
| 24 |
An implementation in a nested ``conftest.py`` applies to tests collected under that directory. |
| 25 |
Use this when a whole package or directory should share the same factory set. |
| 26 |
|
| 27 |
For declaring factories without item-specific logic, see :doc:`custom_loop_factory`. |
| 28 |
|
| 29 |
For selecting a subset of available factories from a test, see :doc:`run_test_with_specific_loop_factories`. |