Skip to content

Commit b731183

Browse files
tjkusonseifertm
authored andcommitted
Document selecting loop factories per test
Split guide Tweak wording
1 parent a06249a commit b731183

8 files changed

Lines changed: 98 additions & 9 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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`.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import asyncio
2+
3+
import pytest
4+
5+
6+
class CustomEventLoop(asyncio.SelectorEventLoop):
7+
pass
8+
9+
10+
@pytest.fixture
11+
def requires_custom_loop():
12+
pass
13+
14+
15+
def pytest_asyncio_loop_factories(config, item):
16+
if "requires_custom_loop" in item.fixturenames:
17+
return {"custom": CustomEventLoop}
18+
return {"default": asyncio.new_event_loop}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pytest
2+
3+
4+
@pytest.mark.asyncio
5+
async def test_runs_with_default_factory_only():
6+
pass
7+
8+
9+
@pytest.mark.asyncio
10+
async def test_runs_with_custom_factory_only(requires_custom_loop):
11+
pass

docs/how-to-guides/custom_loop_factory.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
How to use custom event loop factories for tests
33
================================================
44

5-
pytest-asyncio can run asynchronous tests with custom event loop factories by implementing ``pytest_asyncio_loop_factories`` in ``conftest.py``. The hook returns a mapping from factory names to loop factory callables:
5+
pytest-asyncio can run asynchronous tests with custom event loop factories by implementing ``pytest_asyncio_loop_factories`` in ``conftest.py``. The hook provides the named event loop factories that are available for a test item by returning a mapping from factory names to loop factory callables:
66

77
.. code-block:: python
88
@@ -21,6 +21,6 @@ pytest-asyncio can run asynchronous tests with custom event loop factories by im
2121
"custom": CustomEventLoop,
2222
}
2323
24-
See :doc:`run_test_with_specific_loop_factories` for running tests with only a subset of configured factories.
24+
The hook receives the current pytest ``item``, so it can return different factory mappings for different tests. See :doc:`configure_loop_factories_per_test` for item-based factory configuration.
2525

26-
See :doc:`../reference/hooks` and :doc:`../reference/markers/index` for the hook and marker reference.
26+
To run a test with only some configured factories, see :doc:`run_test_with_specific_loop_factories`.

docs/how-to-guides/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ How-To Guides
1111
change_default_fixture_loop
1212
change_default_test_loop
1313
custom_loop_factory
14+
configure_loop_factories_per_test
1415
run_test_with_specific_loop_factories
1516
run_class_tests_in_same_loop
1617
run_module_tests_in_same_loop

docs/how-to-guides/run_test_with_specific_loop_factories.rst

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@
22
How to run a test with specific event loop factories only
33
=========================================================
44

5-
To run a test with only a subset of configured factories, use the ``loop_factories`` argument of ``pytest.mark.asyncio``:
5+
``pytest_asyncio_loop_factories`` determines which named event loop factories are available for each test item.
6+
By default, pytest-asyncio parametrizes a test with every factory returned for that item.
7+
Use ``loop_factories`` to select a subset of the factory names returned by the hook.
68

7-
.. code-block:: python
9+
Assume ``conftest.py`` provides two named factories:
810

9-
import pytest
11+
.. include:: run_test_with_specific_loop_factories/conftest.py
12+
:code: python
1013

14+
Then use ``loop_factories`` to select which available factory names a test should run with:
1115

12-
@pytest.mark.asyncio(loop_factories=["custom"])
13-
async def test_only_with_custom_event_loop():
14-
pass
16+
.. include:: run_test_with_specific_loop_factories/test_loop_factories_subset.py
17+
:code: python
1518

1619
If a requested factory name is not available from the hook, the test variant for that factory is skipped.
20+
21+
For declaring the factories themselves, see :doc:`custom_loop_factory`.
22+
23+
For choosing the available factories from the pytest item, see :doc:`configure_loop_factories_per_test`.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import asyncio
2+
3+
4+
class CustomEventLoop(asyncio.SelectorEventLoop):
5+
pass
6+
7+
8+
def pytest_asyncio_loop_factories(config, item):
9+
return {
10+
"default": asyncio.new_event_loop,
11+
"custom": CustomEventLoop,
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pytest
2+
3+
4+
@pytest.mark.asyncio
5+
async def test_runs_with_every_configured_factory():
6+
pass
7+
8+
9+
@pytest.mark.asyncio(loop_factories=["custom"])
10+
async def test_runs_with_only_custom_factory():
11+
pass

0 commit comments

Comments
 (0)