@@ -450,40 +450,98 @@ def test_succeed(this, that):
450
450
451
451
452
452
@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 ):
454
454
test_file = """
455
455
from twisted.internet import reactor, defer
456
456
import pytest
457
457
import pytest_twisted
458
458
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():
464
461
d1, d2 = defer.Deferred(), defer.Deferred()
465
462
reactor.callLater(0.01, d1.callback, 1)
466
- reactor.callLater(0.02, d2.callback, request.param )
463
+ reactor.callLater(0.02, d2.callback, 2 )
467
464
await d1
468
465
469
466
# Twisted doesn't allow calling back with a Deferred as a value.
470
467
# This deferred is being wrapped up in a tuple to sneak through.
471
468
# https://github.com/twisted/twisted/blob/c0f1394c7bfb04d97c725a353a1f678fa6a1c602/src/twisted/internet/defer.py#L459
472
469
yield d2,
473
470
474
- if request.param == "archie":
475
- yield 42
476
-
477
471
@pytest_twisted.inlineCallbacks
478
472
def test_succeed(foo):
479
473
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
482
541
"""
483
542
testdir .makepyfile (test_file )
484
543
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 })
487
545
488
546
489
547
@skip_if_no_async_generators ()
0 commit comments