-
Notifications
You must be signed in to change notification settings - Fork 159
Maintain contextvars between fixtures and tests #1008
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aec8eb4
Maintain contextvars.Context in fixtures and tests
bcmills d50effb
Simplify contextvars support
bcmills e6a8374
Improve contextvars test coverage
bcmills 344e62f
Copy context variables from non-generator fixtures
bcmills 0f76adf
Refactor tests to use Pytester
bcmills 7e2da55
Add release note for #1008
bcmills 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
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,73 @@ | ||
""" | ||
Regression test for https://github.com/pytest-dev/pytest-asyncio/issues/127: | ||
contextvars were not properly maintained among fixtures and tests. | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
import sys | ||
from contextlib import contextmanager | ||
from contextvars import ContextVar | ||
|
||
import pytest | ||
|
||
_context_var = ContextVar("context_var") | ||
|
||
|
||
@contextmanager | ||
def context_var_manager(value): | ||
token = _context_var.set(value) | ||
try: | ||
yield | ||
finally: | ||
_context_var.reset(token) | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
bcmills marked this conversation as resolved.
Show resolved
Hide resolved
|
||
async def no_var_fixture(): | ||
with pytest.raises(LookupError): | ||
_context_var.get() | ||
yield | ||
with pytest.raises(LookupError): | ||
_context_var.get() | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
async def var_fixture_1(no_var_fixture): | ||
with context_var_manager("value1"): | ||
yield | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
async def var_nop_fixture(var_fixture_1): | ||
with context_var_manager(_context_var.get()): | ||
yield | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def var_fixture_2(var_nop_fixture): | ||
assert _context_var.get() == "value1" | ||
with context_var_manager("value2"): | ||
yield | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
async def var_fixture_3(var_fixture_2): | ||
assert _context_var.get() == "value2" | ||
with context_var_manager("value3"): | ||
yield | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
async def var_fixture_4(var_fixture_3, request): | ||
assert _context_var.get() == "value3" | ||
_context_var.set("value4") | ||
# Rely on fixture teardown to reset the context var. | ||
|
||
|
||
@pytest.mark.asyncio | ||
@pytest.mark.xfail( | ||
sys.version_info < (3, 11), reason="requires asyncio Task context support" | ||
) | ||
async def test(var_fixture_4): | ||
assert _context_var.get() == "value4" |
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.
Uh oh!
There was an error while loading. Please reload this page.