Skip to content

Commit

Permalink
unittest: cleanup unexpected success handling (#8231)
Browse files Browse the repository at this point in the history
* unittest: cleanup unexpected success handling

* update comment
  • Loading branch information
antonblr committed Jan 14, 2021
1 parent 7a5a6cb commit 42d5545
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
11 changes: 1 addition & 10 deletions src/_pytest/skipping.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ def evaluate_xfail_marks(item: Item) -> Optional[Xfail]:
skipped_by_mark_key = StoreKey[bool]()
# Saves the xfail mark evaluation. Can be refreshed during call if None.
xfailed_key = StoreKey[Optional[Xfail]]()
unexpectedsuccess_key = StoreKey[str]()


@hookimpl(tryfirst=True)
Expand Down Expand Up @@ -271,15 +270,7 @@ def pytest_runtest_makereport(item: Item, call: CallInfo[None]):
outcome = yield
rep = outcome.get_result()
xfailed = item._store.get(xfailed_key, None)
# unittest special case, see setting of unexpectedsuccess_key
if unexpectedsuccess_key in item._store and rep.when == "call":
reason = item._store[unexpectedsuccess_key]
if reason:
rep.longrepr = f"Unexpected success: {reason}"
else:
rep.longrepr = "Unexpected success"
rep.outcome = "failed"
elif item.config.option.runxfail:
if item.config.option.runxfail:
pass # don't interfere
elif call.excinfo and isinstance(call.excinfo.value, xfail.Exception):
assert call.excinfo.value.msg is not None
Expand Down
24 changes: 12 additions & 12 deletions src/_pytest/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
from _pytest.python import PyCollector
from _pytest.runner import CallInfo
from _pytest.skipping import skipped_by_mark_key
from _pytest.skipping import unexpectedsuccess_key

if TYPE_CHECKING:
import unittest
import twisted.trial.unittest

from _pytest.fixtures import _Scope

Expand Down Expand Up @@ -273,25 +273,25 @@ def addExpectedFailure(
self._addexcinfo(sys.exc_info())

def addUnexpectedSuccess(
self, testcase: "unittest.TestCase", reason: str = ""
self,
testcase: "unittest.TestCase",
reason: Optional["twisted.trial.unittest.Todo"] = None,
) -> None:
self._store[unexpectedsuccess_key] = reason
msg = "Unexpected success"
if reason:
msg += f": {reason.reason}"
# Preserve unittest behaviour - fail the test. Explicitly not an XPASS.
try:
fail(msg, pytrace=False)
except fail.Exception:
self._addexcinfo(sys.exc_info())

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

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

def _expecting_failure(self, test_method) -> bool:
"""Return True if the given unittest method (or the entire class) is marked
with @expectedFailure."""
expecting_failure_method = getattr(
test_method, "__unittest_expecting_failure__", False
)
expecting_failure_class = getattr(self, "__unittest_expecting_failure__", False)
return bool(expecting_failure_class or expecting_failure_method)

def runtest(self) -> None:
from _pytest.debugging import maybe_wrap_pytest_function_for_tracing

Expand Down
9 changes: 7 additions & 2 deletions testing/test_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,8 @@ def test_failing_test_is_xfail(self):

@pytest.mark.parametrize("runner", ["pytest", "unittest"])
def test_unittest_expected_failure_for_passing_test_is_fail(
pytester: Pytester, runner
pytester: Pytester,
runner: str,
) -> None:
script = pytester.makepyfile(
"""
Expand All @@ -782,7 +783,11 @@ def test_passing_test_is_fail(self):
if runner == "pytest":
result = pytester.runpytest("-rxX")
result.stdout.fnmatch_lines(
["*MyTestCase*test_passing_test_is_fail*", "*1 failed*"]
[
"*MyTestCase*test_passing_test_is_fail*",
"Unexpected success",
"*1 failed*",
]
)
else:
result = pytester.runpython(script)
Expand Down

0 comments on commit 42d5545

Please sign in to comment.