Skip to content

Commit 42d5545

Browse files
authored
unittest: cleanup unexpected success handling (#8231)
* unittest: cleanup unexpected success handling * update comment
1 parent 7a5a6cb commit 42d5545

File tree

3 files changed

+20
-24
lines changed

3 files changed

+20
-24
lines changed

src/_pytest/skipping.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ def evaluate_xfail_marks(item: Item) -> Optional[Xfail]:
234234
skipped_by_mark_key = StoreKey[bool]()
235235
# Saves the xfail mark evaluation. Can be refreshed during call if None.
236236
xfailed_key = StoreKey[Optional[Xfail]]()
237-
unexpectedsuccess_key = StoreKey[str]()
238237

239238

240239
@hookimpl(tryfirst=True)
@@ -271,15 +270,7 @@ def pytest_runtest_makereport(item: Item, call: CallInfo[None]):
271270
outcome = yield
272271
rep = outcome.get_result()
273272
xfailed = item._store.get(xfailed_key, None)
274-
# unittest special case, see setting of unexpectedsuccess_key
275-
if unexpectedsuccess_key in item._store and rep.when == "call":
276-
reason = item._store[unexpectedsuccess_key]
277-
if reason:
278-
rep.longrepr = f"Unexpected success: {reason}"
279-
else:
280-
rep.longrepr = "Unexpected success"
281-
rep.outcome = "failed"
282-
elif item.config.option.runxfail:
273+
if item.config.option.runxfail:
283274
pass # don't interfere
284275
elif call.excinfo and isinstance(call.excinfo.value, xfail.Exception):
285276
assert call.excinfo.value.msg is not None

src/_pytest/unittest.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030
from _pytest.python import PyCollector
3131
from _pytest.runner import CallInfo
3232
from _pytest.skipping import skipped_by_mark_key
33-
from _pytest.skipping import unexpectedsuccess_key
3433

3534
if TYPE_CHECKING:
3635
import unittest
36+
import twisted.trial.unittest
3737

3838
from _pytest.fixtures import _Scope
3939

@@ -273,25 +273,25 @@ def addExpectedFailure(
273273
self._addexcinfo(sys.exc_info())
274274

275275
def addUnexpectedSuccess(
276-
self, testcase: "unittest.TestCase", reason: str = ""
276+
self,
277+
testcase: "unittest.TestCase",
278+
reason: Optional["twisted.trial.unittest.Todo"] = None,
277279
) -> None:
278-
self._store[unexpectedsuccess_key] = reason
280+
msg = "Unexpected success"
281+
if reason:
282+
msg += f": {reason.reason}"
283+
# Preserve unittest behaviour - fail the test. Explicitly not an XPASS.
284+
try:
285+
fail(msg, pytrace=False)
286+
except fail.Exception:
287+
self._addexcinfo(sys.exc_info())
279288

280289
def addSuccess(self, testcase: "unittest.TestCase") -> None:
281290
pass
282291

283292
def stopTest(self, testcase: "unittest.TestCase") -> None:
284293
pass
285294

286-
def _expecting_failure(self, test_method) -> bool:
287-
"""Return True if the given unittest method (or the entire class) is marked
288-
with @expectedFailure."""
289-
expecting_failure_method = getattr(
290-
test_method, "__unittest_expecting_failure__", False
291-
)
292-
expecting_failure_class = getattr(self, "__unittest_expecting_failure__", False)
293-
return bool(expecting_failure_class or expecting_failure_method)
294-
295295
def runtest(self) -> None:
296296
from _pytest.debugging import maybe_wrap_pytest_function_for_tracing
297297

testing/test_unittest.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -765,7 +765,8 @@ def test_failing_test_is_xfail(self):
765765

766766
@pytest.mark.parametrize("runner", ["pytest", "unittest"])
767767
def test_unittest_expected_failure_for_passing_test_is_fail(
768-
pytester: Pytester, runner
768+
pytester: Pytester,
769+
runner: str,
769770
) -> None:
770771
script = pytester.makepyfile(
771772
"""
@@ -782,7 +783,11 @@ def test_passing_test_is_fail(self):
782783
if runner == "pytest":
783784
result = pytester.runpytest("-rxX")
784785
result.stdout.fnmatch_lines(
785-
["*MyTestCase*test_passing_test_is_fail*", "*1 failed*"]
786+
[
787+
"*MyTestCase*test_passing_test_is_fail*",
788+
"Unexpected success",
789+
"*1 failed*",
790+
]
786791
)
787792
else:
788793
result = pytester.runpython(script)

0 commit comments

Comments
 (0)