@@ -557,73 +557,71 @@ def _callCleanup(self, function, /, *args, **kwargs):
557
557
function (* args , ** kwargs )
558
558
559
559
def run (self , result = None ):
560
- orig_result = result
561
560
if result is None :
562
561
result = self .defaultTestResult ()
563
562
startTestRun = getattr (result , 'startTestRun' , None )
563
+ stopTestRun = getattr (result , 'stopTestRun' , None )
564
564
if startTestRun is not None :
565
565
startTestRun ()
566
+ else :
567
+ stopTestRun = None
566
568
567
569
result .startTest (self )
568
-
569
- testMethod = getattr (self , self ._testMethodName )
570
- if (getattr (self .__class__ , "__unittest_skip__" , False ) or
571
- getattr (testMethod , "__unittest_skip__" , False )):
572
- # If the class or method was skipped.
573
- try :
570
+ try :
571
+ testMethod = getattr (self , self ._testMethodName )
572
+ if (getattr (self .__class__ , "__unittest_skip__" , False ) or
573
+ getattr (testMethod , "__unittest_skip__" , False )):
574
+ # If the class or method was skipped.
574
575
skip_why = (getattr (self .__class__ , '__unittest_skip_why__' , '' )
575
576
or getattr (testMethod , '__unittest_skip_why__' , '' ))
576
577
self ._addSkip (result , self , skip_why )
577
- finally :
578
- result .stopTest (self )
579
- return
580
- expecting_failure_method = getattr (testMethod ,
581
- "__unittest_expecting_failure__" , False )
582
- expecting_failure_class = getattr (self ,
583
- "__unittest_expecting_failure__" , False )
584
- expecting_failure = expecting_failure_class or expecting_failure_method
585
- outcome = _Outcome (result )
586
- try :
587
- self ._outcome = outcome
578
+ return
579
+
580
+ expecting_failure = (
581
+ getattr (self , "__unittest_expecting_failure__" , False ) or
582
+ getattr (testMethod , "__unittest_expecting_failure__" , False )
583
+ )
584
+ outcome = _Outcome (result )
585
+ try :
586
+ self ._outcome = outcome
588
587
589
- with outcome .testPartExecutor (self ):
590
- self ._callSetUp ()
591
- if outcome .success :
592
- outcome .expecting_failure = expecting_failure
593
- with outcome .testPartExecutor (self , isTest = True ):
594
- self ._callTestMethod (testMethod )
595
- outcome .expecting_failure = False
596
588
with outcome .testPartExecutor (self ):
597
- self ._callTearDown ()
598
-
599
- self .doCleanups ()
600
- for test , reason in outcome .skipped :
601
- self ._addSkip (result , test , reason )
602
- self ._feedErrorsToResult (result , outcome .errors )
603
- if outcome .success :
604
- if expecting_failure :
605
- if outcome .expectedFailure :
606
- self ._addExpectedFailure (result , outcome .expectedFailure )
589
+ self ._callSetUp ()
590
+ if outcome .success :
591
+ outcome .expecting_failure = expecting_failure
592
+ with outcome .testPartExecutor (self , isTest = True ):
593
+ self ._callTestMethod (testMethod )
594
+ outcome .expecting_failure = False
595
+ with outcome .testPartExecutor (self ):
596
+ self ._callTearDown ()
597
+
598
+ self .doCleanups ()
599
+ for test , reason in outcome .skipped :
600
+ self ._addSkip (result , test , reason )
601
+ self ._feedErrorsToResult (result , outcome .errors )
602
+ if outcome .success :
603
+ if expecting_failure :
604
+ if outcome .expectedFailure :
605
+ self ._addExpectedFailure (result , outcome .expectedFailure )
606
+ else :
607
+ self ._addUnexpectedSuccess (result )
607
608
else :
608
- self ._addUnexpectedSuccess (result )
609
- else :
610
- result .addSuccess (self )
611
- return result
609
+ result .addSuccess (self )
610
+ return result
611
+ finally :
612
+ # explicitly break reference cycles:
613
+ # outcome.errors -> frame -> outcome -> outcome.errors
614
+ # outcome.expectedFailure -> frame -> outcome -> outcome.expectedFailure
615
+ outcome .errors .clear ()
616
+ outcome .expectedFailure = None
617
+
618
+ # clear the outcome, no more needed
619
+ self ._outcome = None
620
+
612
621
finally :
613
622
result .stopTest (self )
614
- if orig_result is None :
615
- stopTestRun = getattr (result , 'stopTestRun' , None )
616
- if stopTestRun is not None :
617
- stopTestRun ()
618
-
619
- # explicitly break reference cycles:
620
- # outcome.errors -> frame -> outcome -> outcome.errors
621
- # outcome.expectedFailure -> frame -> outcome -> outcome.expectedFailure
622
- outcome .errors .clear ()
623
- outcome .expectedFailure = None
624
-
625
- # clear the outcome, no more needed
626
- self ._outcome = None
623
+ if stopTestRun is not None :
624
+ stopTestRun ()
627
625
628
626
def doCleanups (self ):
629
627
"""Execute all cleanup functions. Normally called for you after
0 commit comments