Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

TestResult.stopTest called when test is skipped, despiteTestResult.startTest not being called #113267

Closed
Labels
3.12only security fixes3.13bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error
@yvan-the-barbarian

Description

@yvan-the-barbarian

Bug report

Bug description:

Following commit551aa6ab9419109a80ad53900ad930e9b7f2e40d a bug seems to have been introduced in theTestCase.run method.

Because theresult.startTest(self) call was moved inside the try/finally block after the check to see if the test is skipped, theresult.stopTest(self) call will be made even if the test is skipped andstartTest is never called.

While this does not cause issues with the standardTestResult orTextTestResult classes, it can cause issues with other classes which subclass those and expect every call tostopTest to be preceded by a call fromstartTest.

Most notably for me, theunittest-xml-reporting package, whosestopTest method uses data initialized instartTest.

Note: Further thinking about this problem, it seems like changing the flow of methods like this was ill thought solution to the problem in the first place, as this might be an unexpected change of behaviour for different libraries. I'm still leaving what I think could be the solution in case I'm wrong on that.

It looks like the fix would seem to be moving the call tostopTest in yet another try/finally block right after the call tostartTest, emulating the same behaviour as previous code while keeping the call tostartTest after the skip check. The call tostopTestRun would need to remain where it is asstartTestRun has already been called at this point.

So using the code as found in that commit, something like:

defrun(self,result=None):ifresultisNone:result=self.defaultTestResult()startTestRun=getattr(result,'startTestRun',None)stopTestRun=getattr(result,'stopTestRun',None)ifstartTestRunisnotNone:startTestRun()else:stopTestRun=Nonetry:testMethod=getattr(self,self._testMethodName)if (getattr(self.__class__,"__unittest_skip__",False)orgetattr(testMethod,"__unittest_skip__",False)):# If the class or method was skipped.skip_why= (getattr(self.__class__,'__unittest_skip_why__','')orgetattr(testMethod,'__unittest_skip_why__',''))_addSkip(result,self,skip_why)returnresult# Increase the number of tests only if it hasn't been skippedresult.startTest(self)try:expecting_failure= (getattr(self,"__unittest_expecting_failure__",False)orgetattr(testMethod,"__unittest_expecting_failure__",False)                )outcome=_Outcome(result)start_time=time.perf_counter()try:self._outcome=outcomewithoutcome.testPartExecutor(self):self._callSetUp()ifoutcome.success:outcome.expecting_failure=expecting_failurewithoutcome.testPartExecutor(self):self._callTestMethod(testMethod)outcome.expecting_failure=Falsewithoutcome.testPartExecutor(self):self._callTearDown()self.doCleanups()self._addDuration(result, (time.perf_counter()-start_time))ifoutcome.success:ifexpecting_failure:ifoutcome.expectedFailure:self._addExpectedFailure(result,outcome.expectedFailure)else:self._addUnexpectedSuccess(result)else:result.addSuccess(self)returnresultfinally:# explicitly break reference cycle:# outcome.expectedFailure -> frame -> outcome -> outcome.expectedFailureoutcome.expectedFailure=Noneoutcome=None# clear the outcome, no more neededself._outcome=Nonefinally:result.stopTest(self)finally:ifstopTestRunisnotNone:stopTestRun()

CPython versions tested on:

3.12

Operating systems tested on:

Linux, Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp