Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.6] unittest: handle outcomes.Exit #5649

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/5634.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``pytest.exit`` and ``bdb.BdbQuit`` are now correctly handled in ``unittest`` cases.
6 changes: 6 additions & 0 deletions src/_pytest/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import pytest
from _pytest.compat import getimfunc
from _pytest.config import hookimpl
from _pytest.outcomes import exit
from _pytest.outcomes import fail
from _pytest.outcomes import skip
from _pytest.outcomes import xfail
Expand Down Expand Up @@ -172,6 +173,11 @@ def _addexcinfo(self, rawexcinfo):
self.__dict__.setdefault("_excinfo", []).append(excinfo)

def addError(self, testcase, rawexcinfo):
try:
if isinstance(rawexcinfo[1], exit.Exception):
exit(rawexcinfo[1].msg)
except TypeError:
pass
self._addexcinfo(rawexcinfo)

def addFailure(self, testcase, rawexcinfo):
Expand Down
36 changes: 36 additions & 0 deletions testing/test_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1065,3 +1065,39 @@ def test_setup_inheritance_skipping(testdir, test_name, expected_outcome):
testdir.copy_example("unittest/{}".format(test_name))
result = testdir.runpytest()
result.stdout.fnmatch_lines(["* {} in *".format(expected_outcome)])


def test_BdbQuit(testdir):
testdir.makepyfile(
test_foo="""
import unittest

class MyTestCase(unittest.TestCase):
def test_bdbquit(self):
import bdb
raise bdb.BdbQuit()

def test_should_not_run(self):
pass
"""
)
reprec = testdir.inline_run()
reprec.assertoutcome(failed=1, passed=1)


def test_exit_outcome(testdir):
testdir.makepyfile(
test_foo="""
import pytest
import unittest

class MyTestCase(unittest.TestCase):
def test_exit_outcome(self):
pytest.exit("pytest_exit called")

def test_should_not_run(self):
pass
"""
)
result = testdir.runpytest()
result.stdout.fnmatch_lines("*Exit: pytest_exit called*")