@@ -120,13 +120,18 @@ def decorator_apply(dec, func):
120
120
121
121
122
122
def inlineCallbacks (f ):
123
+ """
124
+ Mark as inline callbacks test for pytest-twisted processing and apply
125
+ @inlineCallbacks.
126
+ """
123
127
decorated = decorator_apply (defer .inlineCallbacks , f )
124
128
_set_mark (o = decorated , mark = 'inline_callbacks_test' )
125
129
126
130
return decorated
127
131
128
132
129
133
def ensureDeferred (f ):
134
+ """Mark as async test for pytest-twisted processing."""
130
135
_set_mark (o = f , mark = 'async_test' )
131
136
132
137
return f
@@ -151,10 +156,12 @@ def stop_twisted_greenlet():
151
156
152
157
153
158
def _get_mark (o , default = None ):
159
+ """Get the pytest-twisted test or fixture mark."""
154
160
return getattr (o , _mark_attribute_name , default )
155
161
156
162
157
163
def _set_mark (o , mark ):
164
+ """Set the pytest-twisted test or fixture mark."""
158
165
setattr (o , _mark_attribute_name , mark )
159
166
160
167
@@ -167,8 +174,15 @@ def fixture(*args, **kwargs):
167
174
scope = kwargs .get ('scope' , 'function' )
168
175
169
176
if scope not in ['function' , 'module' ]:
170
- # TODO: add test for session scope (and that's it, right?)
171
- # then remove this and update docs
177
+ # TODO: handle...
178
+ # - class
179
+ # - package
180
+ # - session
181
+ # - dynamic
182
+ #
183
+ # https://docs.pytest.org/en/latest/reference.html#pytest-fixture-api
184
+ # then remove this and update docs, or maybe keep it around
185
+ # in case new options come in without support?
172
186
raise AsyncFixtureUnsupportedScopeError .from_scope (scope = scope )
173
187
174
188
def decorator (f ):
@@ -188,22 +202,22 @@ def decorator(f):
188
202
189
203
190
204
def pytest_fixture_setup (fixturedef , request ):
191
- """Interface pytest to async setup for async and async yield fixtures."""
205
+ """Interface pytest to async for async and async yield fixtures."""
192
206
# TODO: what about _adding_ inlineCallbacks fixture support?
193
207
maybe_mark = _get_mark (fixturedef .func )
194
208
if maybe_mark is None :
195
209
return None
196
210
197
211
mark = maybe_mark
198
212
199
- run_inline_callbacks (_async_pytest_fixture_setup , fixturedef , request , mark )
213
+ _run_inline_callbacks (_async_pytest_fixture_setup , fixturedef , request , mark )
200
214
201
215
return not None
202
216
203
217
204
218
@defer .inlineCallbacks
205
219
def _async_pytest_fixture_setup (fixturedef , request , mark ):
206
- """Setup async and async yield fixtures ."""
220
+ """Setup an async or async yield fixture ."""
207
221
fixture_function = fixturedef .func
208
222
209
223
kwargs = {
@@ -266,12 +280,8 @@ def tear_it_down(deferred):
266
280
)
267
281
268
282
269
- # TODO: https://docs.pytest.org/en/latest/reference.html#_pytest.hookspec.pytest_runtest_protocol
270
- # claims it should also take a nextItem but that triggers a direct error
271
-
272
-
273
- def run_inline_callbacks (f , * args ):
274
- """Interface into Twisted greenlet to run and wait for deferred."""
283
+ def _run_inline_callbacks (f , * args ):
284
+ """Interface into Twisted greenlet to run and wait for a deferred."""
275
285
if _instances .gr_twisted is not None :
276
286
if _instances .gr_twisted .dead :
277
287
raise RuntimeError ("twisted reactor has stopped" )
@@ -295,13 +305,15 @@ def pytest_runtest_teardown(item):
295
305
296
306
while len (_tracking .to_be_torn_down ) > 0 :
297
307
deferred = _tracking .to_be_torn_down .pop (0 )
298
- run_inline_callbacks (tear_it_down , deferred )
308
+ _run_inline_callbacks (tear_it_down , deferred )
299
309
300
310
301
311
def pytest_pyfunc_call (pyfuncitem ):
302
312
"""Interface to async test call handler."""
303
313
# TODO: only handle 'our' tests? what is the point of handling others?
304
- run_inline_callbacks (_async_pytest_pyfunc_call , pyfuncitem )
314
+ # well, because our interface allowed people to return deferreds
315
+ # from arbitrary tests so we kinda have to keep this up for now
316
+ _run_inline_callbacks (_async_pytest_pyfunc_call , pyfuncitem )
305
317
return not None
306
318
307
319
@@ -328,6 +340,7 @@ def _async_pytest_pyfunc_call(pyfuncitem):
328
340
329
341
@pytest .fixture (scope = "session" , autouse = True )
330
342
def twisted_greenlet ():
343
+ """Provide the twisted greenlet in fixture form."""
331
344
return _instances .gr_twisted
332
345
333
346
0 commit comments