Debian Codegraph @

tests/markers/test_function_scope.py

raw
1 from __future__ import annotations
2
3 import sys
4 from textwrap import dedent
5
6 from pytest import Pytester
7
8
9 def test_asyncio_mark_provides_function_scoped_loop_strict_mode(pytester: Pytester):
10 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
11 pytester.makepyfile(dedent("""\
12 import asyncio
13 import pytest
14
15 pytestmark = pytest.mark.asyncio
16
17 loop: asyncio.AbstractEventLoop
18
19 async def test_remember_loop():
20 global loop
21 loop = asyncio.get_running_loop()
22
23 async def test_does_not_run_in_same_loop():
24 global loop
25 assert asyncio.get_running_loop() is not loop
26 """))
27 result = pytester.runpytest("--asyncio-mode=strict")
28 result.assert_outcomes(passed=2)
29
30
31 def test_loop_scope_function_provides_function_scoped_event_loop(pytester: Pytester):
32 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
33 pytester.makepyfile(dedent("""\
34 import asyncio
35 import pytest
36
37 pytestmark = pytest.mark.asyncio(loop_scope="function")
38
39 loop: asyncio.AbstractEventLoop
40
41 async def test_remember_loop():
42 global loop
43 loop = asyncio.get_running_loop()
44
45 async def test_does_not_run_in_same_loop():
46 global loop
47 assert asyncio.get_running_loop() is not loop
48 """))
49 result = pytester.runpytest("--asyncio-mode=strict")
50 result.assert_outcomes(passed=2)
51
52
53 def test_raises_when_scope_and_loop_scope_arguments_are_present(pytester: Pytester):
54 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
55 pytester.makepyfile(dedent("""\
56 import pytest
57
58 @pytest.mark.asyncio(scope="function", loop_scope="function")
59 async def test_raises():
60 ...
61 """))
62 result = pytester.runpytest("--asyncio-mode=strict")
63 result.assert_outcomes(errors=1)
64
65
66 def test_warns_when_scope_argument_is_present(pytester: Pytester):
67 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
68 pytester.makepyfile(dedent("""\
69 import pytest
70
71 @pytest.mark.asyncio(scope="function")
72 async def test_warns():
73 ...
74 """))
75 result = pytester.runpytest("--asyncio-mode=strict", "-W", "default")
76 result.assert_outcomes(passed=1, warnings=1)
77 result.stdout.fnmatch_lines("*DeprecationWarning*")
78
79
80 def test_asyncio_mark_respects_the_loop_policy(
81 pytester: Pytester,
82 ):
83 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
84 pytester.makepyfile(
85 dedent("""\
86 import asyncio
87 import pytest
88
89 pytestmark = pytest.mark.asyncio
90
91 class CustomEventLoopPolicy(asyncio.DefaultEventLoopPolicy):
92 pass
93
94 @pytest.fixture(scope="function")
95 def event_loop_policy():
96 return CustomEventLoopPolicy()
97
98 async def test_uses_custom_event_loop_policy():
99 assert isinstance(
100 asyncio.get_event_loop_policy(),
101 CustomEventLoopPolicy,
102 )
103 """),
104 )
105 pytest_args = ["--asyncio-mode=strict"]
106 if sys.version_info >= (3, 14):
107 pytest_args.extend(["-W", "default"])
108 result = pytester.runpytest(*pytest_args)
109 if sys.version_info >= (3, 14):
110 result.assert_outcomes(passed=1, warnings=3)
111 result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*")
112 else:
113 result.assert_outcomes(passed=1)
114
115
116 def test_asyncio_mark_respects_parametrized_loop_policies(
117 pytester: Pytester,
118 ):
119 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
120 pytester.makepyfile(dedent("""\
121 import asyncio
122
123 import pytest
124
125 pytestmark = pytest.mark.asyncio
126
127 class CustomEventLoopPolicy(asyncio.DefaultEventLoopPolicy):
128 pass
129
130 @pytest.fixture(
131 scope="module",
132 params=[
133 CustomEventLoopPolicy(),
134 CustomEventLoopPolicy(),
135 ],
136 )
137 def event_loop_policy(request):
138 return request.param
139
140 async def test_parametrized_loop():
141 assert isinstance(
142 asyncio.get_event_loop_policy(),
143 CustomEventLoopPolicy,
144 )
145 """))
146 pytest_args = ["--asyncio-mode=strict"]
147 if sys.version_info >= (3, 14):
148 pytest_args.extend(["-W", "default"])
149 result = pytester.runpytest(*pytest_args)
150 if sys.version_info >= (3, 14):
151 result.assert_outcomes(passed=2, warnings=5)
152 result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*")
153 else:
154 result.assert_outcomes(passed=2)
155
156
157 def test_event_loop_policy_fixture_override_emits_deprecation_warning(
158 pytester: Pytester,
159 ):
160 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
161 pytester.makepyfile(
162 dedent("""\
163 import asyncio
164 import pytest
165
166 pytestmark = pytest.mark.asyncio
167
168 @pytest.fixture
169 def event_loop_policy():
170 return asyncio.DefaultEventLoopPolicy()
171
172 async def test_anything():
173 pass
174 """),
175 )
176 result = pytester.runpytest("--asyncio-mode=strict", "-W", "default")
177 result.assert_outcomes(passed=1)
178 result.stdout.fnmatch_lines(
179 "*PytestDeprecationWarning*event_loop_policy*deprecated*"
180 )
181
182
183 def test_default_event_loop_policy_fixture_does_not_warn(
184 pytester: Pytester,
185 ):
186 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
187 pytester.makepyfile(
188 dedent("""\
189 import pytest
190
191 pytestmark = pytest.mark.asyncio
192
193 async def test_anything():
194 pass
195 """),
196 )
197 result = pytester.runpytest("--asyncio-mode=strict", "-W", "default")
198 result.assert_outcomes(passed=1)
199 result.stdout.no_fnmatch_line("*PytestDeprecationWarning*event_loop_policy*")
200
201
202 def test_asyncio_mark_provides_function_scoped_loop_to_fixtures(
203 pytester: Pytester,
204 ):
205 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
206 pytester.makepyfile(dedent("""\
207 import asyncio
208
209 import pytest
210 import pytest_asyncio
211
212 pytestmark = pytest.mark.asyncio
213
214 loop: asyncio.AbstractEventLoop
215
216 @pytest_asyncio.fixture
217 async def my_fixture():
218 global loop
219 loop = asyncio.get_running_loop()
220
221 async def test_runs_is_same_loop_as_fixture(my_fixture):
222 global loop
223 assert asyncio.get_running_loop() is loop
224 """))
225 result = pytester.runpytest("--asyncio-mode=strict")
226 result.assert_outcomes(passed=1)
227
228
229 def test_asyncio_mark_handles_missing_event_loop_triggered_by_fixture(
230 pytester: Pytester,
231 ):
232 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
233 pytester.makepyfile(dedent("""\
234 import pytest
235 import asyncio
236
237 @pytest.fixture
238 def sets_event_loop_to_none():
239 # asyncio.run() creates a new event loop without closing the existing
240 # one. For any test, but the first one, this leads to a ResourceWarning
241 # when the discarded loop is destroyed by the garbage collector.
242 # We close the current loop to avoid this
243 try:
244 asyncio.get_event_loop().close()
245 except RuntimeError:
246 pass
247 return asyncio.run(asyncio.sleep(0))
248 # asyncio.run() sets the current event loop to None when finished
249
250 @pytest.mark.asyncio
251 # parametrization may impact fixture ordering
252 @pytest.mark.parametrize("n", (0, 1))
253 async def test_does_not_fail(sets_event_loop_to_none, n):
254 pass
255 """))
256 result = pytester.runpytest("--asyncio-mode=strict")
257 result.assert_outcomes(passed=2)
258
259
260 def test_standalone_test_does_not_trigger_warning_about_no_current_event_loop_being_set(
261 pytester: Pytester,
262 ):
263 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
264 pytester.makepyfile(dedent("""\
265 import pytest
266
267 @pytest.mark.asyncio
268 async def test_anything():
269 pass
270 """))
271 result = pytester.runpytest("--asyncio-mode=strict", "--assert=plain")
272 result.assert_outcomes(warnings=0, passed=1)
273
274
275 def test_asyncio_mark_does_not_duplicate_other_marks_in_auto_mode(
276 pytester: Pytester,
277 ):
278 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
279 pytester.makeconftest(dedent("""\
280 def pytest_configure(config):
281 config.addinivalue_line(
282 "markers", "dummy_marker: mark used for testing purposes"
283 )
284 """))
285 pytester.makepyfile(dedent("""\
286 import pytest
287
288 @pytest.mark.dummy_marker
289 async def test_markers_not_duplicated(request):
290 markers = []
291 for node, marker in request.node.iter_markers_with_node():
292 markers.append(marker)
293 assert len(markers) == 2
294 """))
295 result = pytester.runpytest("--asyncio-mode=auto", "--assert=plain")
296 result.assert_outcomes(warnings=0, passed=1)