File tree 3 files changed +35
-2
lines changed 3 files changed +35
-2
lines changed Original file line number Diff line number Diff line change @@ -733,10 +733,16 @@ def addfinalizer(self, finalizer):
733
733
self ._finalizer .append (finalizer )
734
734
735
735
def finish (self ):
736
+ exceptions = []
736
737
try :
737
738
while self ._finalizer :
738
- func = self ._finalizer .pop ()
739
- func ()
739
+ try :
740
+ func = self ._finalizer .pop ()
741
+ func ()
742
+ except :
743
+ exceptions .append (sys .exc_info ())
744
+ if exceptions :
745
+ py .builtin ._reraise (* exceptions [0 ])
740
746
finally :
741
747
ihook = self ._fixturemanager .session .ihook
742
748
ihook .pytest_fixture_post_finalizer (fixturedef = self )
Original file line number Diff line number Diff line change
1
+ Exceptions in a SubRequest's finish() block are suppressed until all finalizers are called, with the initial exception reraised.
Original file line number Diff line number Diff line change @@ -657,6 +657,32 @@ def test_second():
657
657
"*1 error*" # XXX the whole module collection fails
658
658
])
659
659
660
+ def test_request_subrequest_addfinalizer_exceptions (self , testdir ):
661
+ testdir .makepyfile ("""
662
+ import pytest
663
+ l = []
664
+ def _excepts():
665
+ raise Exception('Error')
666
+ @pytest.fixture
667
+ def subrequest(request):
668
+ return request
669
+ @pytest.fixture
670
+ def something(subrequest):
671
+ subrequest.addfinalizer(lambda: l.append(1))
672
+ subrequest.addfinalizer(lambda: l.append(2))
673
+ subrequest.addfinalizer(_excepts)
674
+ @pytest.fixture
675
+ def excepts(subrequest):
676
+ subrequest.addfinalizer(_excepts)
677
+ subrequest.addfinalizer(lambda: l.append(3))
678
+ def test_first(something, excepts):
679
+ pass
680
+ def test_second():
681
+ assert l == [3, 2, 1]
682
+ """ )
683
+ reprec = testdir .inline_run ()
684
+ reprec .assertoutcome (passed = 2 , failed = 1 )
685
+
660
686
def test_request_getmodulepath (self , testdir ):
661
687
modcol = testdir .getmodulecol ("def test_somefunc(): pass" )
662
688
item , = testdir .genitems ([modcol ])
You can’t perform that action at this time.
0 commit comments