-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
test_async_yield_fixture.py
299 lines (226 loc) · 6.8 KB
/
test_async_yield_fixture.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
def test_single_async_yield_fixture(testdir):
testdir.makepyfile(
"""
import pytest
import trio
events = []
@pytest.fixture
async def fix1():
events.append('fix1 setup')
await trio.sleep(0)
yield 'fix1'
await trio.sleep(0)
events.append('fix1 teardown')
def test_before():
assert not events
@pytest.mark.trio
async def test_actual_test(fix1):
assert events == ['fix1 setup']
assert fix1 == 'fix1'
def test_after():
assert events == [
'fix1 setup',
'fix1 teardown',
]
"""
)
result = testdir.runpytest()
result.assert_outcomes(passed=3)
def test_nested_async_yield_fixture(testdir):
testdir.makepyfile(
"""
import pytest
import trio
events = []
@pytest.fixture
async def fix2():
events.append('fix2 setup')
await trio.sleep(0)
yield 'fix2'
await trio.sleep(0)
events.append('fix2 teardown')
@pytest.fixture
async def fix1(fix2):
events.append('fix1 setup')
await trio.sleep(0)
yield 'fix1'
await trio.sleep(0)
events.append('fix1 teardown')
def test_before():
assert not events
@pytest.mark.trio
async def test_actual_test(fix1):
assert events == [
'fix2 setup',
'fix1 setup',
]
assert fix1 == 'fix1'
def test_after():
assert events == [
'fix2 setup',
'fix1 setup',
'fix1 teardown',
'fix2 teardown',
]
"""
)
result = testdir.runpytest()
result.assert_outcomes(passed=3)
def test_async_yield_fixture_within_sync_fixture(testdir):
testdir.makepyfile(
"""
import pytest
import trio
events = []
@pytest.fixture
async def fix2():
events.append('fix2 setup')
await trio.sleep(0)
yield 'fix2'
await trio.sleep(0)
events.append('fix2 teardown')
@pytest.fixture
def fix1(fix2):
return 'fix1'
def test_before():
assert not events
@pytest.mark.trio
async def test_actual_test(fix1):
assert events == [
'fix2 setup',
]
assert fix1 == 'fix1'
def test_after():
assert events == [
'fix2 setup',
'fix2 teardown',
]
"""
)
result = testdir.runpytest()
result.assert_outcomes(passed=3)
def test_async_yield_fixture_within_sync_yield_fixture(testdir):
testdir.makepyfile(
"""
import pytest
import trio
events = []
@pytest.fixture
async def fix2():
events.append('fix2 setup')
await trio.sleep(0)
yield 'fix2'
await trio.sleep(0)
events.append('fix2 teardown')
@pytest.fixture
def fix1(fix2):
events.append('fix1 setup')
yield 'fix1'
events.append('fix1 teardown')
def test_before():
assert not events
@pytest.mark.trio
async def test_actual_test(fix1):
assert events == [
'fix2 setup',
'fix1 setup',
]
assert fix1 == 'fix1'
def test_after():
assert events == [
'fix2 setup',
'fix1 setup',
'fix1 teardown',
'fix2 teardown',
]
"""
)
result = testdir.runpytest()
result.assert_outcomes(passed=3)
def test_async_yield_fixture_with_multiple_yields(testdir):
testdir.makepyfile(
"""
import pytest
import trio
@pytest.fixture
async def fix1():
await trio.sleep(0)
yield 'good'
await trio.sleep(0)
yield 'bad'
@pytest.mark.trio
async def test_actual_test(fix1):
pass
"""
)
result = testdir.runpytest()
# TODO: should trigger error instead of failure
# result.assert_outcomes(errors=1)
result.assert_outcomes(failed=1)
def test_async_yield_fixture_with_nursery(testdir):
testdir.makepyfile(
"""
import pytest
import trio
async def handle_client(stream):
while True:
buff = await stream.receive_some(4)
await stream.send_all(buff)
@pytest.fixture
async def server():
async with trio.open_nursery() as nursery:
listeners = await nursery.start(trio.serve_tcp, handle_client, 0)
yield listeners[0]
nursery.cancel_scope.cancel()
@pytest.mark.trio
async def test_actual_test(server):
stream = await trio.testing.open_stream_to_socket_listener(server)
await stream.send_all(b'ping')
rep = await stream.receive_some(4)
assert rep == b'ping'
"""
)
result = testdir.runpytest()
result.assert_outcomes(passed=1)
def test_async_yield_fixture_crashed_teardown_allow_other_teardowns(testdir):
testdir.makepyfile(
"""
import pytest
import trio
setup_events = set()
teardown_events = set()
@pytest.fixture
async def good_fixture():
async with trio.open_nursery() as nursery:
setup_events.add('good_fixture setup')
yield None
teardown_events.add('good_fixture teardown')
@pytest.fixture
async def bad_fixture():
async with trio.open_nursery() as nursery:
setup_events.add('bad_fixture setup')
yield None
teardown_events.add('bad_fixture teardown')
raise RuntimeError('Crash during fixture teardown')
def test_before():
assert not setup_events
assert not teardown_events
@pytest.mark.trio
async def test_actual_test(bad_fixture, good_fixture):
pass
def test_after():
assert setup_events == {
'good_fixture setup',
'bad_fixture setup',
}
assert teardown_events == {
'bad_fixture teardown',
'good_fixture teardown',
}
"""
)
result = testdir.runpytest()
result.assert_outcomes(failed=1, passed=2)
result.stdout.re_match_lines(
[r"(E\W+| +\| )RuntimeError: Crash during fixture teardown"]
)