-
Notifications
You must be signed in to change notification settings - Fork 181
Document selecting loop factories per test #1432
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tjkuson
wants to merge
3
commits into
pytest-dev:main
Choose a base branch
from
tjkuson:document-factories-per-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| ======================================================== | ||
| How to configure event loop factories from the test item | ||
| ======================================================== | ||
|
|
||
| ``pytest_asyncio_loop_factories`` is called with the current pytest ``item``. | ||
| Use that item to decide which named event loop factories are available for the test being collected. | ||
|
|
||
| For example, a hook can inspect the test's fixtures and return a different factory mapping for tests that request a particular fixture. | ||
| In ``conftest.py``, check the current item's fixture names and build the factory mapping for that item: | ||
|
|
||
| .. include:: configure_loop_factories_per_test/conftest.py | ||
| :code: python | ||
|
|
||
| Then request the fixture from tests that should use the custom factory: | ||
|
|
||
| .. include:: configure_loop_factories_per_test/test_extra_loop_factories.py | ||
| :code: python | ||
|
|
||
| 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``. | ||
|
|
||
| The same pattern works with any information available from the current pytest item, such as fixture names, markers, node IDs, or file paths. | ||
|
|
||
| Because this is a standard pytest hook, its placement also matters. | ||
| An implementation in a nested ``conftest.py`` applies to tests collected under that directory. | ||
| Use this when a whole package or directory should share the same factory set. | ||
|
|
||
| For declaring factories without item-specific logic, see :doc:`custom_loop_factory`. | ||
|
|
||
| For selecting a subset of available factories from a test, see :doc:`run_test_with_specific_loop_factories`. |
18 changes: 18 additions & 0 deletions
18
docs/how-to-guides/configure_loop_factories_per_test/conftest.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import asyncio | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| class CustomEventLoop(asyncio.SelectorEventLoop): | ||
| pass | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def requires_custom_loop(): | ||
| pass | ||
|
|
||
|
|
||
| def pytest_asyncio_loop_factories(config, item): | ||
| if "requires_custom_loop" in item.fixturenames: | ||
| return {"custom": CustomEventLoop} | ||
| return {"default": asyncio.new_event_loop} |
11 changes: 11 additions & 0 deletions
11
docs/how-to-guides/configure_loop_factories_per_test/test_extra_loop_factories.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import pytest | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_runs_with_default_factory_only(): | ||
| pass | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_runs_with_custom_factory_only(requires_custom_loop): | ||
| pass |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
docs/how-to-guides/run_test_with_specific_loop_factories/conftest.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import asyncio | ||
|
|
||
|
|
||
| class CustomEventLoop(asyncio.SelectorEventLoop): | ||
| pass | ||
|
|
||
|
|
||
| def pytest_asyncio_loop_factories(config, item): | ||
| return { | ||
| "default": asyncio.new_event_loop, | ||
| "custom": CustomEventLoop, | ||
| } |
11 changes: 11 additions & 0 deletions
11
docs/how-to-guides/run_test_with_specific_loop_factories/test_loop_factories_subset.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import pytest | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_runs_with_every_configured_factory(): | ||
| pass | ||
|
|
||
|
|
||
| @pytest.mark.asyncio(loop_factories=["custom"]) | ||
| async def test_runs_with_only_custom_factory(): | ||
| pass |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The first part up to line 18 explains that all tests are parametrized with whatever entries are returned by the hook and that users can limit the parametrization via the loop_factories argument in the asyncio marker. The added code examples show an "inverted" control flow: there's no parametrization by default, unless the user explicitly lists the factory identifier in loop_factories.
I understand that we want to show both and each content makes sense, but it could be confusing that two approaches are mixed.
I'm not sure how to best solve this. We could either highlight that these are two very different approaches, e.g. "Variant 1, Variant 2" headlines or keep both variants in separate how-tos. I'm leaning towards the latter, but I'm failing to come up with distinguishable how-to titles.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I see what you mean.
I revisited how Diátaxis works and I think there's a reasonable argument in favour of this being three separate how-to guides: how to declare the event loop factories; how to declare factories based on item-specific logic; and how to select a subset of available factories from a test. I am curious what you think: 0a1803d f1a4d0d