Debian Codegraph @

tests/markers/test_package_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_package_scoped_loop_strict_mode(pytester: Pytester):
10 package_name = pytester.path.name
11 subpackage_name = "subpkg"
12 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
13 pytester.makepyfile(
14 __init__="",
15 shared_module=dedent("""\
16 import asyncio
17
18 loop: asyncio.AbstractEventLoop = None
19 """),
20 test_module_one=dedent(f"""\
21 import asyncio
22 import pytest
23
24 from {package_name} import shared_module
25
26 @pytest.mark.asyncio(loop_scope="package")
27 async def test_remember_loop():
28 shared_module.loop = asyncio.get_running_loop()
29 """),
30 test_module_two=dedent(f"""\
31 import asyncio
32 import pytest
33
34 from {package_name} import shared_module
35
36 pytestmark = pytest.mark.asyncio(loop_scope="package")
37
38 async def test_this_runs_in_same_loop():
39 assert asyncio.get_running_loop() is shared_module.loop
40
41 class TestClassA:
42 async def test_this_runs_in_same_loop(self):
43 assert asyncio.get_running_loop() is shared_module.loop
44 """),
45 )
46 subpkg = pytester.mkpydir(subpackage_name)
47 subpkg.joinpath("__init__.py").touch()
48 subpkg.joinpath("test_subpkg.py").write_text(dedent(f"""\
49 import asyncio
50 import pytest
51
52 from {package_name} import shared_module
53
54 pytestmark = pytest.mark.asyncio(loop_scope="package")
55
56 async def test_subpackage_runs_in_different_loop():
57 assert asyncio.get_running_loop() is not shared_module.loop
58 """))
59 result = pytester.runpytest("--asyncio-mode=strict")
60 result.assert_outcomes(passed=4)
61
62
63 def test_asyncio_mark_respects_the_loop_policy(
64 pytester: Pytester,
65 ):
66 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
67 pytester.makepyfile(
68 __init__="",
69 conftest=dedent("""\
70 import pytest
71
72 from .custom_policy import CustomEventLoopPolicy
73
74 @pytest.fixture(scope="package")
75 def event_loop_policy():
76 return CustomEventLoopPolicy()
77 """),
78 custom_policy=dedent("""\
79 import asyncio
80
81 class CustomEventLoopPolicy(asyncio.DefaultEventLoopPolicy):
82 pass
83 """),
84 test_uses_custom_policy=dedent("""\
85 import asyncio
86 import pytest
87
88 from .custom_policy import CustomEventLoopPolicy
89
90 pytestmark = pytest.mark.asyncio(loop_scope="package")
91
92 async def test_uses_custom_event_loop_policy():
93 assert isinstance(
94 asyncio.get_event_loop_policy(),
95 CustomEventLoopPolicy,
96 )
97 """),
98 test_also_uses_custom_policy=dedent("""\
99 import asyncio
100 import pytest
101
102 from .custom_policy import CustomEventLoopPolicy
103
104 pytestmark = pytest.mark.asyncio(loop_scope="package")
105
106 async def test_also_uses_custom_event_loop_policy():
107 assert isinstance(
108 asyncio.get_event_loop_policy(),
109 CustomEventLoopPolicy,
110 )
111 """),
112 )
113 pytest_args = ["--asyncio-mode=strict"]
114 if sys.version_info >= (3, 14):
115 pytest_args.extend(["-W", "default"])
116 result = pytester.runpytest(*pytest_args)
117 if sys.version_info >= (3, 14):
118 result.assert_outcomes(passed=2, warnings=4)
119 result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*")
120 else:
121 result.assert_outcomes(passed=2)
122
123
124 def test_asyncio_mark_respects_parametrized_loop_policies(
125 pytester: Pytester,
126 ):
127 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
128 pytester.makepyfile(
129 __init__="",
130 test_parametrization=dedent("""\
131 import asyncio
132
133 import pytest
134
135 pytestmark = pytest.mark.asyncio(loop_scope="package")
136
137 @pytest.fixture(
138 scope="package",
139 params=[
140 asyncio.DefaultEventLoopPolicy(),
141 asyncio.DefaultEventLoopPolicy(),
142 ],
143 )
144 def event_loop_policy(request):
145 return request.param
146
147 async def test_parametrized_loop():
148 pass
149 """),
150 )
151 pytest_args = ["--asyncio-mode=strict"]
152 if sys.version_info >= (3, 14):
153 pytest_args.extend(["-W", "default"])
154 result = pytester.runpytest(*pytest_args)
155 if sys.version_info >= (3, 14):
156 result.assert_outcomes(passed=2, warnings=4)
157 result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*")
158 else:
159 result.assert_outcomes(passed=2)
160
161
162 def test_asyncio_mark_provides_package_scoped_loop_to_fixtures(
163 pytester: Pytester,
164 ):
165 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
166 package_name = pytester.path.name
167 pytester.makepyfile(
168 __init__="",
169 conftest=dedent(f"""\
170 import asyncio
171
172 import pytest_asyncio
173
174 from {package_name} import shared_module
175
176 @pytest_asyncio.fixture(loop_scope="package", scope="package")
177 async def my_fixture():
178 shared_module.loop = asyncio.get_running_loop()
179 """),
180 shared_module=dedent("""\
181 import asyncio
182
183 loop: asyncio.AbstractEventLoop = None
184 """),
185 test_fixture_runs_in_scoped_loop=dedent(f"""\
186 import asyncio
187
188 import pytest
189 import pytest_asyncio
190
191 from {package_name} import shared_module
192
193 pytestmark = pytest.mark.asyncio(loop_scope="package")
194
195 async def test_runs_in_same_loop_as_fixture(my_fixture):
196 assert asyncio.get_running_loop() is shared_module.loop
197 """),
198 )
199 result = pytester.runpytest("--asyncio-mode=strict")
200 result.assert_outcomes(passed=1)
201
202
203 def test_asyncio_mark_allows_combining_package_scoped_fixture_with_module_scoped_test(
204 pytester: Pytester,
205 ):
206 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
207 pytester.makepyfile(
208 __init__="",
209 test_mixed_scopes=dedent("""\
210 import asyncio
211
212 import pytest
213 import pytest_asyncio
214
215 loop: asyncio.AbstractEventLoop
216
217 @pytest_asyncio.fixture(loop_scope="package", scope="package")
218 async def async_fixture():
219 global loop
220 loop = asyncio.get_running_loop()
221
222 @pytest.mark.asyncio(loop_scope="module")
223 async def test_runs_in_different_loop_as_fixture(async_fixture):
224 global loop
225 assert asyncio.get_running_loop() is not loop
226 """),
227 )
228 result = pytester.runpytest("--asyncio-mode=strict")
229 result.assert_outcomes(passed=1)
230
231
232 def test_asyncio_mark_allows_combining_package_scoped_fixture_with_class_scoped_test(
233 pytester: Pytester,
234 ):
235 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
236 pytester.makepyfile(
237 __init__="",
238 test_mixed_scopes=dedent("""\
239 import asyncio
240
241 import pytest
242 import pytest_asyncio
243
244 loop: asyncio.AbstractEventLoop
245
246 @pytest_asyncio.fixture(loop_scope="package", scope="package")
247 async def async_fixture():
248 global loop
249 loop = asyncio.get_running_loop()
250
251 @pytest.mark.asyncio(loop_scope="class")
252 class TestMixedScopes:
253 async def test_runs_in_different_loop_as_fixture(self, async_fixture):
254 global loop
255 assert asyncio.get_running_loop() is not loop
256 """),
257 )
258 result = pytester.runpytest("--asyncio-mode=strict")
259 result.assert_outcomes(passed=1)
260
261
262 def test_asyncio_mark_allows_combining_package_scoped_fixture_with_function_scoped_test(
263 pytester: Pytester,
264 ):
265 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
266 pytester.makepyfile(
267 __init__="",
268 test_mixed_scopes=dedent("""\
269 import asyncio
270
271 import pytest
272 import pytest_asyncio
273
274 loop: asyncio.AbstractEventLoop
275
276 @pytest_asyncio.fixture(loop_scope="package", scope="package")
277 async def async_fixture():
278 global loop
279 loop = asyncio.get_running_loop()
280
281 @pytest.mark.asyncio
282 async def test_runs_in_different_loop_as_fixture(async_fixture):
283 global loop
284 assert asyncio.get_running_loop() is not loop
285 """),
286 )
287 result = pytester.runpytest("--asyncio-mode=strict")
288 result.assert_outcomes(passed=1)
289
290
291 def test_asyncio_mark_handles_missing_event_loop_triggered_by_fixture(
292 pytester: Pytester,
293 ):
294 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
295 pytester.makepyfile(
296 __init__="",
297 test_loop_is_none=dedent("""\
298 import pytest
299 import asyncio
300
301 @pytest.fixture(scope="package")
302 def sets_event_loop_to_none():
303 # asyncio.run() creates a new event loop without closing the existing
304 # one. For any test, but the first one, this leads to a ResourceWarning
305 # when the discarded loop is destroyed by the garbage collector.
306 # We close the current loop to avoid this
307 try:
308 asyncio.get_event_loop().close()
309 except RuntimeError:
310 pass
311 return asyncio.run(asyncio.sleep(0))
312 # asyncio.run() sets the current event loop to None when finished
313
314 @pytest.mark.asyncio(loop_scope="package")
315 # parametrization may impact fixture ordering
316 @pytest.mark.parametrize("n", (0, 1))
317 async def test_does_not_fail(sets_event_loop_to_none, n):
318 pass
319 """),
320 )
321 result = pytester.runpytest("--asyncio-mode=strict")
322 result.assert_outcomes(passed=2)