Skip to content

Commit e6a8374

Browse files
committed
Improve contextvars test coverage
1 parent d50effb commit e6a8374

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

pytest_asyncio/plugin.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -381,12 +381,10 @@ def _create_task_in_context(loop, coro, context):
381381
the API added for https://github.com/python/cpython/issues/91150.
382382
On earlier versions, the returned task will use the default context instead.
383383
"""
384-
if context is not None:
385-
try:
386-
return loop.create_task(coro, context=context)
387-
except TypeError:
388-
pass
389-
return loop.create_task(coro)
384+
try:
385+
return loop.create_task(coro, context=context)
386+
except TypeError:
387+
return loop.create_task(coro)
390388

391389

392390
def _wrap_async_fixture(fixturedef: FixtureDef) -> None:

tests/async_fixtures/test_async_fixtures_contextvars.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,34 @@ async def no_var_fixture():
3333

3434

3535
@pytest.fixture(scope="function")
36-
async def var_fixture(no_var_fixture):
37-
with context_var_manager("value"):
36+
async def var_fixture_1(no_var_fixture):
37+
with context_var_manager("value1"):
3838
yield
3939

4040

4141
@pytest.fixture(scope="function")
42-
async def var_nop_fixture(var_fixture):
42+
async def var_nop_fixture(var_fixture_1):
4343
with context_var_manager(_context_var.get()):
4444
yield
4545

4646

4747
@pytest.fixture(scope="function")
48-
def inner_var_fixture(var_nop_fixture):
49-
assert _context_var.get() == "value"
48+
def var_fixture_2(var_nop_fixture):
49+
assert _context_var.get() == "value1"
5050
with context_var_manager("value2"):
5151
yield
5252

5353

54+
@pytest.fixture(scope="function")
55+
async def var_fixture_3(var_fixture_2):
56+
assert _context_var.get() == "value2"
57+
with context_var_manager("value3"):
58+
yield
59+
60+
5461
@pytest.mark.asyncio
5562
@pytest.mark.xfail(
5663
sys.version_info < (3, 11), reason="requires asyncio Task context support"
5764
)
58-
async def test(inner_var_fixture):
59-
assert _context_var.get() == "value2"
65+
async def test(var_fixture_3):
66+
assert _context_var.get() == "value3"

0 commit comments

Comments
 (0)