Skip to content

Commit

Permalink
Improve contextvars test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
bcmills authored and seifertm committed Dec 12, 2024
1 parent 3004bb7 commit 62ab185
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
10 changes: 4 additions & 6 deletions pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,12 +381,10 @@ def _create_task_in_context(loop, coro, context):
the API added for https://github.com/python/cpython/issues/91150.
On earlier versions, the returned task will use the default context instead.
"""
if context is not None:
try:
return loop.create_task(coro, context=context)
except TypeError:
pass
return loop.create_task(coro)
try:
return loop.create_task(coro, context=context)
except TypeError:
return loop.create_task(coro)


def _wrap_async_fixture(fixturedef: FixtureDef) -> None:
Expand Down
21 changes: 14 additions & 7 deletions tests/async_fixtures/test_async_fixtures_contextvars.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,34 @@ async def no_var_fixture():


@pytest.fixture(scope="function")
async def var_fixture(no_var_fixture):
with context_var_manager("value"):
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):
async def var_nop_fixture(var_fixture_1):
with context_var_manager(_context_var.get()):
yield


@pytest.fixture(scope="function")
def inner_var_fixture(var_nop_fixture):
assert _context_var.get() == "value"
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.mark.asyncio
@pytest.mark.xfail(
sys.version_info < (3, 11), reason="requires asyncio Task context support"
)
async def test(inner_var_fixture):
assert _context_var.get() == "value2"
async def test(var_fixture_3):
assert _context_var.get() == "value3"

0 comments on commit 62ab185

Please sign in to comment.