forked from pytest-dev/pytest-asyncio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_async_fixtures_contextvars.py
247 lines (213 loc) · 6.84 KB
/
test_async_fixtures_contextvars.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
"""
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 textwrap import dedent
import pytest
from pytest import Pytester
_prelude = dedent(
"""
import pytest
import pytest_asyncio
from contextlib import contextmanager
from contextvars import ContextVar
_context_var = ContextVar("context_var")
@contextmanager
def context_var_manager(value):
token = _context_var.set(value)
try:
yield
finally:
_context_var.reset(token)
"""
)
def test_var_from_sync_generator_propagates_to_async(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
_prelude
+ dedent(
"""
@pytest.fixture
def var_fixture():
with context_var_manager("value"):
yield
@pytest_asyncio.fixture
async def check_var_fixture(var_fixture):
assert _context_var.get() == "value"
@pytest.mark.asyncio
async def test(check_var_fixture):
assert _context_var.get() == "value"
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=1)
@pytest.mark.xfail(
sys.version_info < (3, 11),
reason="requires asyncio Task context support",
strict=True,
)
def test_var_from_async_generator_propagates_to_sync(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
_prelude
+ dedent(
"""
@pytest_asyncio.fixture
async def var_fixture():
with context_var_manager("value"):
yield
@pytest.fixture
def check_var_fixture(var_fixture):
assert _context_var.get() == "value"
@pytest.mark.asyncio
async def test(check_var_fixture):
assert _context_var.get() == "value"
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=1)
@pytest.mark.xfail(
sys.version_info < (3, 11),
reason="requires asyncio Task context support",
strict=True,
)
def test_var_from_async_fixture_propagates_to_sync(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
_prelude
+ dedent(
"""
@pytest_asyncio.fixture
async def var_fixture():
_context_var.set("value")
# Rely on async fixture teardown to reset the context var.
@pytest.fixture
def check_var_fixture(var_fixture):
assert _context_var.get() == "value"
def test(check_var_fixture):
assert _context_var.get() == "value"
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=1)
@pytest.mark.xfail(
sys.version_info < (3, 11),
reason="requires asyncio Task context support",
strict=True,
)
def test_var_from_generator_reset_before_previous_fixture_cleanup(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
_prelude
+ dedent(
"""
@pytest_asyncio.fixture
async def no_var_fixture():
with pytest.raises(LookupError):
_context_var.get()
yield
with pytest.raises(LookupError):
_context_var.get()
@pytest_asyncio.fixture
async def var_fixture(no_var_fixture):
with context_var_manager("value"):
yield
@pytest.mark.asyncio
async def test(var_fixture):
assert _context_var.get() == "value"
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=1)
@pytest.mark.xfail(
sys.version_info < (3, 11),
reason="requires asyncio Task context support",
strict=True,
)
def test_var_from_fixture_reset_before_previous_fixture_cleanup(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
_prelude
+ dedent(
"""
@pytest_asyncio.fixture
async def no_var_fixture():
with pytest.raises(LookupError):
_context_var.get()
yield
with pytest.raises(LookupError):
_context_var.get()
@pytest_asyncio.fixture
async def var_fixture(no_var_fixture):
_context_var.set("value")
# Rely on async fixture teardown to reset the context var.
@pytest.mark.asyncio
async def test(var_fixture):
assert _context_var.get() == "value"
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=1)
@pytest.mark.xfail(
sys.version_info < (3, 11),
reason="requires asyncio Task context support",
strict=True,
)
def test_var_previous_value_restored_after_fixture(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
_prelude
+ dedent(
"""
@pytest_asyncio.fixture
async def var_fixture_1():
with context_var_manager("value1"):
yield
assert _context_var.get() == "value1"
@pytest_asyncio.fixture
async def var_fixture_2(var_fixture_1):
with context_var_manager("value2"):
yield
assert _context_var.get() == "value2"
@pytest.mark.asyncio
async def test(var_fixture_2):
assert _context_var.get() == "value2"
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=1)
@pytest.mark.xfail(
sys.version_info < (3, 11),
reason="requires asyncio Task context support",
strict=True,
)
def test_var_set_to_existing_value_ok(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
_prelude
+ dedent(
"""
@pytest_asyncio.fixture
async def var_fixture():
with context_var_manager("value"):
yield
@pytest_asyncio.fixture
async def same_var_fixture(var_fixture):
with context_var_manager(_context_var.get()):
yield
@pytest.mark.asyncio
async def test(same_var_fixture):
assert _context_var.get() == "value"
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=1)