1- import sys
2- import pytest
3- import re
4-
5-
6- @pytest .fixture (params = ["Python>=36" , "async_generator" ])
7- def async_yield_implementation (request ):
8- if request .param == "Python>=36" :
9-
10- def patch_code (code ):
11- # Convert code to use Python>=3.6 builtin async generator
12- code = re .sub (r"(?m)^\s*@async_generator\n" , r"" , code )
13- code = re .sub (r"await yield_" , r"yield" , code )
14- return code
15-
16- return patch_code
17- else :
18- return lambda x : x
19-
20-
21- def test_single_async_yield_fixture (testdir , async_yield_implementation ):
1+ def test_single_async_yield_fixture (testdir ):
222 testdir .makepyfile (
23- async_yield_implementation (
24- """
3+ """
254 import pytest
265 import trio
27- from async_generator import async_generator, yield_
286
297 events = []
308
319 @pytest.fixture
32- @async_generator
3310 async def fix1():
3411 events.append('fix1 setup')
3512 await trio.sleep(0)
3613
37- await yield_( 'fix1')
14+ yield 'fix1'
3815
3916 await trio.sleep(0)
4017 events.append('fix1 teardown')
@@ -53,43 +30,38 @@ def test_after():
5330 'fix1 teardown',
5431 ]
5532 """
56- )
5733 )
5834
5935 result = testdir .runpytest ()
6036
6137 result .assert_outcomes (passed = 3 )
6238
6339
64- def test_nested_async_yield_fixture (testdir , async_yield_implementation ):
40+ def test_nested_async_yield_fixture (testdir ):
6541
6642 testdir .makepyfile (
67- async_yield_implementation (
68- """
43+ """
6944 import pytest
7045 import trio
71- from async_generator import async_generator, yield_
7246
7347 events = []
7448
7549 @pytest.fixture
76- @async_generator
7750 async def fix2():
7851 events.append('fix2 setup')
7952 await trio.sleep(0)
8053
81- await yield_( 'fix2')
54+ yield 'fix2'
8255
8356 await trio.sleep(0)
8457 events.append('fix2 teardown')
8558
8659 @pytest.fixture
87- @async_generator
8860 async def fix1(fix2):
8961 events.append('fix1 setup')
9062 await trio.sleep(0)
9163
92- await yield_( 'fix1')
64+ yield 'fix1'
9365
9466 await trio.sleep(0)
9567 events.append('fix1 teardown')
@@ -113,32 +85,28 @@ def test_after():
11385 'fix2 teardown',
11486 ]
11587 """
116- )
11788 )
11889
11990 result = testdir .runpytest ()
12091
12192 result .assert_outcomes (passed = 3 )
12293
12394
124- def test_async_yield_fixture_within_sync_fixture (testdir , async_yield_implementation ):
95+ def test_async_yield_fixture_within_sync_fixture (testdir ):
12596
12697 testdir .makepyfile (
127- async_yield_implementation (
128- """
98+ """
12999 import pytest
130100 import trio
131- from async_generator import async_generator, yield_
132101
133102 events = []
134103
135104 @pytest.fixture
136- @async_generator
137105 async def fix2():
138106 events.append('fix2 setup')
139107 await trio.sleep(0)
140108
141- await yield_( 'fix2')
109+ yield 'fix2'
142110
143111 await trio.sleep(0)
144112 events.append('fix2 teardown')
@@ -163,34 +131,28 @@ def test_after():
163131 'fix2 teardown',
164132 ]
165133 """
166- )
167134 )
168135
169136 result = testdir .runpytest ()
170137
171138 result .assert_outcomes (passed = 3 )
172139
173140
174- def test_async_yield_fixture_within_sync_yield_fixture (
175- testdir , async_yield_implementation
176- ):
141+ def test_async_yield_fixture_within_sync_yield_fixture (testdir ):
177142
178143 testdir .makepyfile (
179- async_yield_implementation (
180- """
144+ """
181145 import pytest
182146 import trio
183- from async_generator import async_generator, yield_
184147
185148 events = []
186149
187150 @pytest.fixture
188- @async_generator
189151 async def fix2():
190152 events.append('fix2 setup')
191153 await trio.sleep(0)
192154
193- await yield_( 'fix2')
155+ yield 'fix2'
194156
195157 await trio.sleep(0)
196158 events.append('fix2 teardown')
@@ -220,36 +182,31 @@ def test_after():
220182 'fix2 teardown',
221183 ]
222184 """
223- )
224185 )
225186
226187 result = testdir .runpytest ()
227188
228189 result .assert_outcomes (passed = 3 )
229190
230191
231- def test_async_yield_fixture_with_multiple_yields (testdir , async_yield_implementation ):
192+ def test_async_yield_fixture_with_multiple_yields (testdir ):
232193
233194 testdir .makepyfile (
234- async_yield_implementation (
235- """
195+ """
236196 import pytest
237197 import trio
238- from async_generator import async_generator, yield_
239198
240199 @pytest.fixture
241- @async_generator
242200 async def fix1():
243201 await trio.sleep(0)
244- await yield_( 'good')
202+ yield 'good'
245203 await trio.sleep(0)
246- await yield_( 'bad')
204+ yield 'bad'
247205
248206 @pytest.mark.trio
249207 async def test_actual_test(fix1):
250208 pass
251209 """
252- )
253210 )
254211
255212 result = testdir .runpytest ()
@@ -259,14 +216,12 @@ async def test_actual_test(fix1):
259216 result .assert_outcomes (failed = 1 )
260217
261218
262- def test_async_yield_fixture_with_nursery (testdir , async_yield_implementation ):
219+ def test_async_yield_fixture_with_nursery (testdir ):
263220
264221 testdir .makepyfile (
265- async_yield_implementation (
266- """
222+ """
267223 import pytest
268224 import trio
269- from async_generator import async_generator, yield_
270225
271226
272227 async def handle_client(stream):
@@ -276,11 +231,10 @@ async def handle_client(stream):
276231
277232
278233 @pytest.fixture
279- @async_generator
280234 async def server():
281235 async with trio.open_nursery() as nursery:
282236 listeners = await nursery.start(trio.serve_tcp, handle_client, 0)
283- await yield_( listeners[0])
237+ yield listeners[0]
284238 nursery.cancel_scope.cancel()
285239
286240
@@ -291,42 +245,35 @@ async def test_actual_test(server):
291245 rep = await stream.receive_some(4)
292246 assert rep == b'ping'
293247 """
294- )
295248 )
296249
297250 result = testdir .runpytest ()
298251
299252 result .assert_outcomes (passed = 1 )
300253
301254
302- def test_async_yield_fixture_crashed_teardown_allow_other_teardowns (
303- testdir , async_yield_implementation
304- ):
255+ def test_async_yield_fixture_crashed_teardown_allow_other_teardowns (testdir ):
305256
306257 testdir .makepyfile (
307- async_yield_implementation (
308- """
258+ """
309259 import pytest
310260 import trio
311- from async_generator import async_generator, yield_
312261
313262 setup_events = set()
314263 teardown_events = set()
315264
316265 @pytest.fixture
317- @async_generator
318266 async def good_fixture():
319267 async with trio.open_nursery() as nursery:
320268 setup_events.add('good_fixture setup')
321- await yield_( None)
269+ yield None
322270 teardown_events.add('good_fixture teardown')
323271
324272 @pytest.fixture
325- @async_generator
326273 async def bad_fixture():
327274 async with trio.open_nursery() as nursery:
328275 setup_events.add('bad_fixture setup')
329- await yield_( None)
276+ yield None
330277 teardown_events.add('bad_fixture teardown')
331278 raise RuntimeError('Crash during fixture teardown')
332279
@@ -348,7 +295,6 @@ def test_after():
348295 'good_fixture teardown',
349296 }
350297 """
351- )
352298 )
353299
354300 result = testdir .runpytest ()
0 commit comments