| 1 |
""" |
| 2 |
Regression test for https://github.com/pytest-dev/pytest-asyncio/issues/127: |
| 3 |
contextvars were not properly maintained among fixtures and tests. |
| 4 |
""" |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
from textwrap import dedent |
| 9 |
from typing import Literal |
| 10 |
|
| 11 |
import pytest |
| 12 |
from pytest import Pytester |
| 13 |
|
| 14 |
_prelude = dedent(""" |
| 15 |
import pytest |
| 16 |
import pytest_asyncio |
| 17 |
from contextlib import contextmanager |
| 18 |
from contextvars import ContextVar |
| 19 |
|
| 20 |
_context_var = ContextVar("context_var") |
| 21 |
|
| 22 |
@contextmanager |
| 23 |
def context_var_manager(value): |
| 24 |
token = _context_var.set(value) |
| 25 |
try: |
| 26 |
yield |
| 27 |
finally: |
| 28 |
_context_var.reset(token) |
| 29 |
""") |
| 30 |
|
| 31 |
|
| 32 |
def test_var_from_sync_generator_propagates_to_async(pytester: Pytester): |
| 33 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 34 |
pytester.makepyfile(_prelude + dedent(""" |
| 35 |
@pytest.fixture |
| 36 |
def var_fixture(): |
| 37 |
with context_var_manager("value"): |
| 38 |
yield |
| 39 |
|
| 40 |
@pytest_asyncio.fixture |
| 41 |
async def check_var_fixture(var_fixture): |
| 42 |
assert _context_var.get() == "value" |
| 43 |
|
| 44 |
@pytest.mark.asyncio |
| 45 |
async def test(check_var_fixture): |
| 46 |
assert _context_var.get() == "value" |
| 47 |
""")) |
| 48 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 49 |
result.assert_outcomes(passed=1) |
| 50 |
|
| 51 |
|
| 52 |
def test_var_from_async_generator_propagates_to_sync(pytester: Pytester): |
| 53 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 54 |
pytester.makepyfile(_prelude + dedent(""" |
| 55 |
@pytest_asyncio.fixture |
| 56 |
async def var_fixture(): |
| 57 |
with context_var_manager("value"): |
| 58 |
yield |
| 59 |
|
| 60 |
@pytest.fixture |
| 61 |
def check_var_fixture(var_fixture): |
| 62 |
assert _context_var.get() == "value" |
| 63 |
|
| 64 |
@pytest.mark.asyncio |
| 65 |
async def test(check_var_fixture): |
| 66 |
assert _context_var.get() == "value" |
| 67 |
""")) |
| 68 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 69 |
result.assert_outcomes(passed=1) |
| 70 |
|
| 71 |
|
| 72 |
def test_var_from_async_fixture_propagates_to_sync(pytester: Pytester): |
| 73 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 74 |
pytester.makepyfile(_prelude + dedent(""" |
| 75 |
@pytest_asyncio.fixture |
| 76 |
async def var_fixture(): |
| 77 |
_context_var.set("value") |
| 78 |
# Rely on async fixture teardown to reset the context var. |
| 79 |
|
| 80 |
@pytest.fixture |
| 81 |
def check_var_fixture(var_fixture): |
| 82 |
assert _context_var.get() == "value" |
| 83 |
|
| 84 |
def test(check_var_fixture): |
| 85 |
assert _context_var.get() == "value" |
| 86 |
""")) |
| 87 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 88 |
result.assert_outcomes(passed=1) |
| 89 |
|
| 90 |
|
| 91 |
def test_var_from_generator_reset_before_previous_fixture_cleanup(pytester: Pytester): |
| 92 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 93 |
pytester.makepyfile(_prelude + dedent(""" |
| 94 |
@pytest_asyncio.fixture |
| 95 |
async def no_var_fixture(): |
| 96 |
with pytest.raises(LookupError): |
| 97 |
_context_var.get() |
| 98 |
yield |
| 99 |
with pytest.raises(LookupError): |
| 100 |
_context_var.get() |
| 101 |
|
| 102 |
@pytest_asyncio.fixture |
| 103 |
async def var_fixture(no_var_fixture): |
| 104 |
with context_var_manager("value"): |
| 105 |
yield |
| 106 |
|
| 107 |
@pytest.mark.asyncio |
| 108 |
async def test(var_fixture): |
| 109 |
assert _context_var.get() == "value" |
| 110 |
""")) |
| 111 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 112 |
result.assert_outcomes(passed=1) |
| 113 |
|
| 114 |
|
| 115 |
def test_var_from_fixture_reset_before_previous_fixture_cleanup(pytester: Pytester): |
| 116 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 117 |
pytester.makepyfile(_prelude + dedent(""" |
| 118 |
@pytest_asyncio.fixture |
| 119 |
async def no_var_fixture(): |
| 120 |
with pytest.raises(LookupError): |
| 121 |
_context_var.get() |
| 122 |
yield |
| 123 |
with pytest.raises(LookupError): |
| 124 |
_context_var.get() |
| 125 |
|
| 126 |
@pytest_asyncio.fixture |
| 127 |
async def var_fixture(no_var_fixture): |
| 128 |
_context_var.set("value") |
| 129 |
# Rely on async fixture teardown to reset the context var. |
| 130 |
|
| 131 |
@pytest.mark.asyncio |
| 132 |
async def test(var_fixture): |
| 133 |
assert _context_var.get() == "value" |
| 134 |
""")) |
| 135 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 136 |
result.assert_outcomes(passed=1) |
| 137 |
|
| 138 |
|
| 139 |
def test_var_previous_value_restored_after_fixture(pytester: Pytester): |
| 140 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 141 |
pytester.makepyfile(_prelude + dedent(""" |
| 142 |
@pytest_asyncio.fixture |
| 143 |
async def var_fixture_1(): |
| 144 |
with context_var_manager("value1"): |
| 145 |
yield |
| 146 |
assert _context_var.get() == "value1" |
| 147 |
|
| 148 |
@pytest_asyncio.fixture |
| 149 |
async def var_fixture_2(var_fixture_1): |
| 150 |
with context_var_manager("value2"): |
| 151 |
yield |
| 152 |
assert _context_var.get() == "value2" |
| 153 |
|
| 154 |
@pytest.mark.asyncio |
| 155 |
async def test(var_fixture_2): |
| 156 |
assert _context_var.get() == "value2" |
| 157 |
""")) |
| 158 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 159 |
result.assert_outcomes(passed=1) |
| 160 |
|
| 161 |
|
| 162 |
def test_var_set_to_existing_value_ok(pytester: Pytester): |
| 163 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 164 |
pytester.makepyfile(_prelude + dedent(""" |
| 165 |
@pytest_asyncio.fixture |
| 166 |
async def var_fixture(): |
| 167 |
with context_var_manager("value"): |
| 168 |
yield |
| 169 |
|
| 170 |
@pytest_asyncio.fixture |
| 171 |
async def same_var_fixture(var_fixture): |
| 172 |
with context_var_manager(_context_var.get()): |
| 173 |
yield |
| 174 |
|
| 175 |
@pytest.mark.asyncio |
| 176 |
async def test(same_var_fixture): |
| 177 |
assert _context_var.get() == "value" |
| 178 |
""")) |
| 179 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 180 |
result.assert_outcomes(passed=1) |
| 181 |
|
| 182 |
|
| 183 |
def test_no_isolation_against_context_changes_in_sync_tests(pytester: Pytester): |
| 184 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 185 |
pytester.makepyfile(dedent(""" |
| 186 |
import pytest |
| 187 |
import pytest_asyncio |
| 188 |
from contextvars import ContextVar |
| 189 |
|
| 190 |
_context_var = ContextVar("my_var") |
| 191 |
|
| 192 |
def test_sync(): |
| 193 |
_context_var.set("new_value") |
| 194 |
|
| 195 |
@pytest.mark.asyncio |
| 196 |
async def test_async(): |
| 197 |
assert _context_var.get() == "new_value" |
| 198 |
""")) |
| 199 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 200 |
result.assert_outcomes(passed=2) |
| 201 |
|
| 202 |
|
| 203 |
@pytest.mark.parametrize("loop_scope", ("function", "module")) |
| 204 |
def test_isolation_against_context_changes_in_async_tests( |
| 205 |
pytester: Pytester, loop_scope: Literal["function", "module"] |
| 206 |
): |
| 207 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 208 |
pytester.makepyfile(dedent(f""" |
| 209 |
import pytest |
| 210 |
import pytest_asyncio |
| 211 |
from contextvars import ContextVar |
| 212 |
|
| 213 |
_context_var = ContextVar("my_var") |
| 214 |
|
| 215 |
@pytest.mark.asyncio(loop_scope="{loop_scope}") |
| 216 |
async def test_async_first(): |
| 217 |
_context_var.set("new_value") |
| 218 |
|
| 219 |
@pytest.mark.asyncio(loop_scope="{loop_scope}") |
| 220 |
async def test_async_second(): |
| 221 |
with pytest.raises(LookupError): |
| 222 |
_context_var.get() |
| 223 |
""")) |
| 224 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 225 |
result.assert_outcomes(passed=2) |