Skip to content

Commit 3fef2ef

Browse files
authored
Merge pull request #116 from altendky/more_async_yield_fixture_test_specificity
More async yield fixture test specificity
2 parents 47510b6 + 06e855c commit 3fef2ef

File tree

1 file changed

+68
-17
lines changed

1 file changed

+68
-17
lines changed

testing/test_basic.py

Lines changed: 68 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -450,40 +450,91 @@ def test_succeed(this, that):
450450

451451

452452
@skip_if_no_async_generators()
453-
def test_async_yield_fixture(testdir, cmd_opts):
453+
def test_async_yield_fixture_can_await(testdir, cmd_opts):
454454
test_file = """
455455
from twisted.internet import reactor, defer
456-
import pytest
457456
import pytest_twisted
458457
459-
@pytest_twisted.async_yield_fixture(
460-
scope="function",
461-
params=["fs", "imap", "web", "archie"],
462-
)
463-
async def foo(request):
458+
@pytest_twisted.async_yield_fixture()
459+
async def foo():
464460
d1, d2 = defer.Deferred(), defer.Deferred()
465461
reactor.callLater(0.01, d1.callback, 1)
466-
reactor.callLater(0.02, d2.callback, request.param)
462+
reactor.callLater(0.02, d2.callback, 2)
467463
await d1
468464
469465
# Twisted doesn't allow calling back with a Deferred as a value.
470466
# This deferred is being wrapped up in a tuple to sneak through.
471467
# https://github.com/twisted/twisted/blob/c0f1394c7bfb04d97c725a353a1f678fa6a1c602/src/twisted/internet/defer.py#L459
472468
yield d2,
473469
474-
if request.param == "archie":
475-
yield 42
470+
@pytest_twisted.ensureDeferred
471+
async def test(foo):
472+
x = await foo[0]
473+
assert x == 2
474+
"""
475+
testdir.makepyfile(test_file)
476+
rr = testdir.run(*cmd_opts, timeout=timeout)
477+
assert_outcomes(rr, {"passed": 1})
476478

477-
@pytest_twisted.inlineCallbacks
478-
def test_succeed(foo):
479-
x = yield foo[0]
480-
if x == "web":
481-
raise RuntimeError("baz")
479+
480+
@skip_if_no_async_generators()
481+
def test_async_yield_fixture_failed_test(testdir, cmd_opts):
482+
test_file = """
483+
import pytest_twisted
484+
485+
@pytest_twisted.async_yield_fixture()
486+
async def foo():
487+
yield 92
488+
489+
@pytest_twisted.ensureDeferred
490+
async def test(foo):
491+
assert False
492+
"""
493+
testdir.makepyfile(test_file)
494+
rr = testdir.run(*cmd_opts, timeout=timeout)
495+
rr.stdout.fnmatch_lines(lines2=["E*assert False"])
496+
assert_outcomes(rr, {"failed": 1})
497+
498+
499+
@skip_if_no_async_generators()
500+
def test_async_yield_fixture_test_exception(testdir, cmd_opts):
501+
test_file = """
502+
import pytest_twisted
503+
504+
class UniqueLocalException(Exception):
505+
pass
506+
507+
@pytest_twisted.async_yield_fixture()
508+
async def foo():
509+
yield 92
510+
511+
@pytest_twisted.ensureDeferred
512+
async def test(foo):
513+
raise UniqueLocalException("some message")
482514
"""
483515
testdir.makepyfile(test_file)
484516
rr = testdir.run(*cmd_opts, timeout=timeout)
485-
# TODO: this is getting super imprecise...
486-
assert_outcomes(rr, {"passed": 3, "failed": 1, "errors": 1})
517+
rr.stdout.fnmatch_lines(lines2=["E*.UniqueLocalException: some message*"])
518+
assert_outcomes(rr, {"failed": 1})
519+
520+
521+
@skip_if_no_async_generators()
522+
def test_async_yield_fixture_yields_twice(testdir, cmd_opts):
523+
test_file = """
524+
import pytest_twisted
525+
526+
@pytest_twisted.async_yield_fixture()
527+
async def foo():
528+
yield 92
529+
yield 36
530+
531+
@pytest_twisted.ensureDeferred
532+
async def test(foo):
533+
assert foo == 92
534+
"""
535+
testdir.makepyfile(test_file)
536+
rr = testdir.run(*cmd_opts, timeout=timeout)
537+
assert_outcomes(rr, {"passed": 1, "errors": 1})
487538

488539

489540
@skip_if_no_async_generators()

0 commit comments

Comments
 (0)