1
1
import sys
2
2
import pytest
3
+ import re
3
4
4
5
5
- @pytest .mark .skipif (sys .version_info < (3 , 6 ), reason = "requires python3.6" )
6
- def test_single_async_yield_fixture (testdir ):
6
+ @pytest .fixture (params = ['Python>=36' , 'async_generator' ])
7
+ def async_yield_implementation (request ):
8
+ if request .param == 'Python>=36' :
9
+ if sys .version_info < (3 , 6 ):
10
+ pytest .skip ("requires python3.6" )
11
+ else :
7
12
13
+ def patch_code (code ):
14
+ # Convert code to use Python>=3.6 builtin async generator
15
+ code = re .sub (r'(?m)^\s*@async_generator\n' , r'' , code )
16
+ code = re .sub (r'await yield_' , r'yield' , code )
17
+ return code
18
+
19
+ return patch_code
20
+ else :
21
+ return lambda x : x
22
+
23
+
24
+ def test_single_async_yield_fixture (testdir , async_yield_implementation ):
8
25
testdir .makepyfile (
9
- """
26
+ async_yield_implementation (
27
+ """
10
28
import pytest
11
29
import trio
30
+ from async_generator import async_generator, yield_
12
31
13
32
events = []
14
33
15
34
@pytest.fixture
35
+ @async_generator
16
36
async def fix1():
17
37
events.append('fix1 setup')
18
38
await trio.sleep(0)
19
39
20
- yield 'fix1'
40
+ await yield_( 'fix1')
21
41
22
42
await trio.sleep(0)
23
43
events.append('fix1 teardown')
@@ -36,39 +56,43 @@ def test_after():
36
56
'fix1 teardown',
37
57
]
38
58
"""
59
+ )
39
60
)
40
61
41
62
result = testdir .runpytest ()
42
63
43
64
result .assert_outcomes (passed = 3 )
44
65
45
66
46
- @pytest .mark .skipif (sys .version_info < (3 , 6 ), reason = "requires python3.6" )
47
- def test_nested_async_yield_fixture (testdir ):
67
+ def test_nested_async_yield_fixture (testdir , async_yield_implementation ):
48
68
49
69
testdir .makepyfile (
50
- """
70
+ async_yield_implementation (
71
+ """
51
72
import pytest
52
73
import trio
74
+ from async_generator import async_generator, yield_
53
75
54
76
events = []
55
77
56
78
@pytest.fixture
79
+ @async_generator
57
80
async def fix2():
58
81
events.append('fix2 setup')
59
82
await trio.sleep(0)
60
83
61
- yield 'fix2'
84
+ await yield_( 'fix2')
62
85
63
86
await trio.sleep(0)
64
87
events.append('fix2 teardown')
65
88
66
89
@pytest.fixture
90
+ @async_generator
67
91
async def fix1(fix2):
68
92
events.append('fix1 setup')
69
93
await trio.sleep(0)
70
94
71
- yield 'fix1'
95
+ await yield_( 'fix1')
72
96
73
97
await trio.sleep(0)
74
98
events.append('fix1 teardown')
@@ -92,29 +116,34 @@ def test_after():
92
116
'fix2 teardown',
93
117
]
94
118
"""
119
+ )
95
120
)
96
121
97
122
result = testdir .runpytest ()
98
123
99
124
result .assert_outcomes (passed = 3 )
100
125
101
126
102
- @pytest .mark .skipif (sys .version_info < (3 , 6 ), reason = "requires python3.6" )
103
- def test_async_yield_fixture_within_sync_fixture (testdir ):
127
+ def test_async_yield_fixture_within_sync_fixture (
128
+ testdir , async_yield_implementation
129
+ ):
104
130
105
131
testdir .makepyfile (
106
- """
132
+ async_yield_implementation (
133
+ """
107
134
import pytest
108
135
import trio
136
+ from async_generator import async_generator, yield_
109
137
110
138
events = []
111
139
112
140
@pytest.fixture
141
+ @async_generator
113
142
async def fix2():
114
143
events.append('fix2 setup')
115
144
await trio.sleep(0)
116
145
117
- yield 'fix2'
146
+ await yield_( 'fix2')
118
147
119
148
await trio.sleep(0)
120
149
events.append('fix2 teardown')
@@ -139,29 +168,34 @@ def test_after():
139
168
'fix2 teardown',
140
169
]
141
170
"""
171
+ )
142
172
)
143
173
144
174
result = testdir .runpytest ()
145
175
146
176
result .assert_outcomes (passed = 3 )
147
177
148
178
149
- @pytest .mark .skipif (sys .version_info < (3 , 6 ), reason = "requires python3.6" )
150
- def test_async_yield_fixture_within_sync_yield_fixture (testdir ):
179
+ def test_async_yield_fixture_within_sync_yield_fixture (
180
+ testdir , async_yield_implementation
181
+ ):
151
182
152
183
testdir .makepyfile (
153
- """
184
+ async_yield_implementation (
185
+ """
154
186
import pytest
155
187
import trio
188
+ from async_generator import async_generator, yield_
156
189
157
190
events = []
158
191
159
192
@pytest.fixture
193
+ @async_generator
160
194
async def fix2():
161
195
events.append('fix2 setup')
162
196
await trio.sleep(0)
163
197
164
- yield 'fix2'
198
+ await yield_( 'fix2')
165
199
166
200
await trio.sleep(0)
167
201
events.append('fix2 teardown')
@@ -191,32 +225,38 @@ def test_after():
191
225
'fix2 teardown',
192
226
]
193
227
"""
228
+ )
194
229
)
195
230
196
231
result = testdir .runpytest ()
197
232
198
233
result .assert_outcomes (passed = 3 )
199
234
200
235
201
- @pytest .mark .skipif (sys .version_info < (3 , 6 ), reason = "requires python3.6" )
202
- def test_async_yield_fixture_with_multiple_yields (testdir ):
236
+ def test_async_yield_fixture_with_multiple_yields (
237
+ testdir , async_yield_implementation
238
+ ):
203
239
204
240
testdir .makepyfile (
205
- """
241
+ async_yield_implementation (
242
+ """
206
243
import pytest
207
244
import trio
245
+ from async_generator import async_generator, yield_
208
246
209
247
@pytest.fixture
248
+ @async_generator
210
249
async def fix1():
211
250
await trio.sleep(0)
212
- yield 'good'
251
+ await yield_( 'good')
213
252
await trio.sleep(0)
214
- yield 'bad'
253
+ await yield_( 'bad')
215
254
216
255
@pytest.mark.trio
217
256
async def test_actual_test(fix1):
218
257
pass
219
258
"""
259
+ )
220
260
)
221
261
222
262
result = testdir .runpytest ()
@@ -226,13 +266,14 @@ async def test_actual_test(fix1):
226
266
result .assert_outcomes (failed = 1 )
227
267
228
268
229
- @pytest .mark .skipif (sys .version_info < (3 , 6 ), reason = "requires python3.6" )
230
- def test_async_yield_fixture_with_nursery (testdir ):
269
+ def test_async_yield_fixture_with_nursery (testdir , async_yield_implementation ):
231
270
232
271
testdir .makepyfile (
233
- """
272
+ async_yield_implementation (
273
+ """
234
274
import pytest
235
275
import trio
276
+ from async_generator import async_generator, yield_
236
277
237
278
238
279
async def handle_client(stream):
@@ -242,10 +283,11 @@ async def handle_client(stream):
242
283
243
284
244
285
@pytest.fixture
286
+ @async_generator
245
287
async def server():
246
288
async with trio.open_nursery() as nursery:
247
289
listeners = await nursery.start(trio.serve_tcp, handle_client, 0)
248
- yield listeners[0]
290
+ await yield_( listeners[0])
249
291
nursery.cancel_scope.cancel()
250
292
251
293
@@ -256,35 +298,41 @@ async def test_actual_test(server):
256
298
rep = await stream.receive_some(4)
257
299
assert rep == b'ping'
258
300
"""
301
+ )
259
302
)
260
303
261
304
result = testdir .runpytest ()
262
305
263
306
result .assert_outcomes (passed = 1 )
264
307
265
308
266
- @pytest .mark .skipif (sys .version_info < (3 , 6 ), reason = "requires python3.6" )
267
- def test_async_yield_fixture_crashed_teardown_allow_other_teardowns (testdir ):
309
+ def test_async_yield_fixture_crashed_teardown_allow_other_teardowns (
310
+ testdir , async_yield_implementation
311
+ ):
268
312
269
313
testdir .makepyfile (
270
- """
314
+ async_yield_implementation (
315
+ """
271
316
import pytest
272
317
import trio
318
+ from async_generator import async_generator, yield_
273
319
274
320
events = []
275
321
276
322
@pytest.fixture
323
+ @async_generator
277
324
async def good_fixture():
278
325
async with trio.open_nursery() as nursery:
279
326
events.append('good_fixture setup')
280
- yield
327
+ await yield_(None)
281
328
events.append('good_fixture teardown')
282
329
283
330
@pytest.fixture
331
+ @async_generator
284
332
async def bad_fixture():
285
333
async with trio.open_nursery() as nursery:
286
334
events.append('bad_fixture setup')
287
- yield
335
+ await yield_(None)
288
336
events.append('bad_fixture teardown')
289
337
raise RuntimeError('Crash during fixture teardown')
290
338
@@ -303,6 +351,7 @@ def test_after():
303
351
'good_fixture teardown',
304
352
]
305
353
"""
354
+ )
306
355
)
307
356
308
357
result = testdir .runpytest ()
0 commit comments