Skip to content

Commit f57de3a

Browse files
committed
Split up the async yield fixture tests for more specificity
1 parent 55bfbd8 commit f57de3a

File tree

1 file changed

+72
-14
lines changed

1 file changed

+72
-14
lines changed

testing/test_basic.py

Lines changed: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -450,40 +450,98 @@ 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
456456
import pytest
457457
import pytest_twisted
458458
459-
@pytest_twisted.async_yield_fixture(
460-
scope="function",
461-
params=["fs", "imap", "web", "archie"],
462-
)
463-
async def foo(request):
459+
@pytest_twisted.async_yield_fixture()
460+
async def foo():
464461
d1, d2 = defer.Deferred(), defer.Deferred()
465462
reactor.callLater(0.01, d1.callback, 1)
466-
reactor.callLater(0.02, d2.callback, request.param)
463+
reactor.callLater(0.02, d2.callback, 2)
467464
await d1
468465
469466
# Twisted doesn't allow calling back with a Deferred as a value.
470467
# This deferred is being wrapped up in a tuple to sneak through.
471468
# https://github.com/twisted/twisted/blob/c0f1394c7bfb04d97c725a353a1f678fa6a1c602/src/twisted/internet/defer.py#L459
472469
yield d2,
473470
474-
if request.param == "archie":
475-
yield 42
476-
477471
@pytest_twisted.inlineCallbacks
478472
def test_succeed(foo):
479473
x = yield foo[0]
480-
if x == "web":
481-
raise RuntimeError("baz")
474+
assert x == 2
475+
"""
476+
testdir.makepyfile(test_file)
477+
rr = testdir.run(*cmd_opts, timeout=timeout)
478+
assert_outcomes(rr, {"passed": 1})
479+
480+
481+
@skip_if_no_async_generators()
482+
def test_async_yield_fixture_failed_test(testdir, cmd_opts):
483+
test_file = """
484+
from twisted.internet import reactor, defer
485+
import pytest
486+
import pytest_twisted
487+
488+
@pytest_twisted.async_yield_fixture()
489+
async def foo(request):
490+
yield 92
491+
492+
@pytest_twisted.inlineCallbacks
493+
def test_succeed(foo):
494+
assert False
495+
"""
496+
testdir.makepyfile(test_file)
497+
rr = testdir.run(*cmd_opts, timeout=timeout)
498+
rr.stdout.fnmatch_lines(lines2=["E*assert False"])
499+
assert_outcomes(rr, {"failed": 1})
500+
501+
502+
@skip_if_no_async_generators()
503+
def test_async_yield_fixture_test_exception(testdir, cmd_opts):
504+
test_file = """
505+
from twisted.internet import reactor, defer
506+
import pytest
507+
import pytest_twisted
508+
509+
class UniqueLocalException(Exception):
510+
pass
511+
512+
@pytest_twisted.async_yield_fixture()
513+
async def foo(request):
514+
yield 92
515+
516+
@pytest_twisted.inlineCallbacks
517+
def test_succeed(foo):
518+
raise UniqueLocalException("some message")
519+
"""
520+
testdir.makepyfile(test_file)
521+
rr = testdir.run(*cmd_opts, timeout=timeout)
522+
rr.stdout.fnmatch_lines(lines2=["E*.UniqueLocalException: some message*"])
523+
assert_outcomes(rr, {"failed": 1})
524+
525+
526+
@skip_if_no_async_generators()
527+
def test_async_yield_fixture_yields_twice(testdir, cmd_opts):
528+
test_file = """
529+
from twisted.internet import reactor, defer
530+
import pytest
531+
import pytest_twisted
532+
533+
@pytest_twisted.async_yield_fixture()
534+
async def foo():
535+
yield 92
536+
yield 36
537+
538+
@pytest_twisted.inlineCallbacks
539+
def test_succeed(foo):
540+
assert foo == 92
482541
"""
483542
testdir.makepyfile(test_file)
484543
rr = testdir.run(*cmd_opts, timeout=timeout)
485-
# TODO: this is getting super imprecise...
486-
assert_outcomes(rr, {"passed": 3, "failed": 1, "errors": 1})
544+
assert_outcomes(rr, {"errors": 1, "failed": 1})
487545

488546

489547
@skip_if_no_async_generators()

0 commit comments

Comments
 (0)