pytest-twisted
pytest-twisted copied to clipboard
@pytest.inlineCallbacks but for async def's
How about a decorator to allow directly writing async def
tests instead of having to wrap them with @pytest.inlineCallbacks
decorated tests.
https://github.com/altendky/altendpyqt5/pull/10/files#diff-7531f542e8da5192e38a81a441db749e
async def async_await_for_signal():
timer = altendpyqt5.tests.utils.singleshot_immediate_timer()
await altendpyqt5.twisted.signal_as_deferred(timer.timeout)
@pytest.inlineCallbacks
def test_await_for_signal():
yield twisted.internet.defer.ensureDeferred(async_await_for_signal())
Unless maybe I just haven't thought of an easy way to handle this with existing pieces.
I'm not sure what to call it or how @decorator.decorator
works but... maybe this is a start?
@decorator.decorator
def asyncCallbacks(f, *args, **kwargs):
@pytest.inlineCallbacks
def wrapper(*args, **kwargs):
yield twisted.internet.defer.ensureDeferred(f())
return wrapper
Drop the @decorator.decorator
stuff. That was a false positive working... Without it it actually runs the tests and passes/fails appropriately.
Then of course the option most like the existing inlineCallbacks
actually ends up working with fixtures as well.
@decorator.decorator
def asyncCallbacks(fun, *args, **kw):
return twisted.internet.defer.ensureDeferred(fun(*args, **kw))
(someday I'll make a PR with tests...)