pytest-twisted icon indicating copy to clipboard operation
pytest-twisted copied to clipboard

@pytest.inlineCallbacks but for async def's

Open altendky opened this issue 6 years ago • 3 comments

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.

altendky avatar Sep 11 '18 21:09 altendky

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

altendky avatar Sep 12 '18 03:09 altendky

Drop the @decorator.decorator stuff. That was a false positive working... Without it it actually runs the tests and passes/fails appropriately.

altendky avatar Sep 12 '18 17:09 altendky

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...)

altendky avatar Sep 19 '18 03:09 altendky