Skip to content

Commit 693e9d0

Browse files
authored
Merge pull request #5634 from blueyed/fix-unittest-exit
unittest: handle outcomes.Exit
2 parents ec4ca59 + 5c09cc1 commit 693e9d0

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

changelog/5634.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``pytest.exit`` and ``bdb.BdbQuit`` are now correctly handled in ``unittest`` cases.

src/_pytest/unittest.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pytest
77
from _pytest.compat import getimfunc
88
from _pytest.config import hookimpl
9+
from _pytest.outcomes import exit
910
from _pytest.outcomes import fail
1011
from _pytest.outcomes import skip
1112
from _pytest.outcomes import xfail
@@ -153,6 +154,11 @@ def _addexcinfo(self, rawexcinfo):
153154
self.__dict__.setdefault("_excinfo", []).append(excinfo)
154155

155156
def addError(self, testcase, rawexcinfo):
157+
try:
158+
if isinstance(rawexcinfo[1], exit.Exception):
159+
exit(rawexcinfo[1].msg)
160+
except TypeError:
161+
pass
156162
self._addexcinfo(rawexcinfo)
157163

158164
def addFailure(self, testcase, rawexcinfo):

testing/test_unittest.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,3 +1050,39 @@ def test_setup_inheritance_skipping(testdir, test_name, expected_outcome):
10501050
testdir.copy_example("unittest/{}".format(test_name))
10511051
result = testdir.runpytest()
10521052
result.stdout.fnmatch_lines(["* {} in *".format(expected_outcome)])
1053+
1054+
1055+
def test_BdbQuit(testdir):
1056+
testdir.makepyfile(
1057+
test_foo="""
1058+
import unittest
1059+
1060+
class MyTestCase(unittest.TestCase):
1061+
def test_bdbquit(self):
1062+
import bdb
1063+
raise bdb.BdbQuit()
1064+
1065+
def test_should_not_run(self):
1066+
pass
1067+
"""
1068+
)
1069+
reprec = testdir.inline_run()
1070+
reprec.assertoutcome(failed=1, passed=1)
1071+
1072+
1073+
def test_exit_outcome(testdir):
1074+
testdir.makepyfile(
1075+
test_foo="""
1076+
import pytest
1077+
import unittest
1078+
1079+
class MyTestCase(unittest.TestCase):
1080+
def test_exit_outcome(self):
1081+
pytest.exit("pytest_exit called")
1082+
1083+
def test_should_not_run(self):
1084+
pass
1085+
"""
1086+
)
1087+
result = testdir.runpytest()
1088+
result.stdout.fnmatch_lines("*Exit: pytest_exit called*")

0 commit comments

Comments
 (0)