From d35d09f82dd2e968e138213ddce66e7afa2ed261 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sat, 20 Jul 2019 07:04:38 +0200 Subject: [PATCH 1/3] unittest: handle outcomes.Exit This is required for pytest to stop when using "quit" in pdb, and should get honored/handled in general. --- src/_pytest/unittest.py | 6 ++++++ testing/test_unittest.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/_pytest/unittest.py b/src/_pytest/unittest.py index 216266979..337490d13 100644 --- a/src/_pytest/unittest.py +++ b/src/_pytest/unittest.py @@ -6,6 +6,7 @@ import _pytest._code 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 @@ -153,6 +154,11 @@ class TestCaseFunction(Function): 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): diff --git a/testing/test_unittest.py b/testing/test_unittest.py index 2467ddd39..d86afd510 100644 --- a/testing/test_unittest.py +++ b/testing/test_unittest.py @@ -1050,3 +1050,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") + + def test_should_not_run(self): + pass + """ + ) + reprec = testdir.inline_run() + reprec.assertoutcome() From 0824789459b40cc1c4126dfa33ea0d20493fbba5 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 23 Jul 2019 08:52:52 -0300 Subject: [PATCH 2/3] Improve test for pytest.exit handling --- testing/test_unittest.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/testing/test_unittest.py b/testing/test_unittest.py index d86afd510..153b76a89 100644 --- a/testing/test_unittest.py +++ b/testing/test_unittest.py @@ -1078,11 +1078,11 @@ def test_exit_outcome(testdir): class MyTestCase(unittest.TestCase): def test_exit_outcome(self): - pytest.exit("pytest_exit") + pytest.exit("pytest_exit called") def test_should_not_run(self): pass """ ) - reprec = testdir.inline_run() - reprec.assertoutcome() + result = testdir.runpytest() + result.stdout.fnmatch_lines("*Exit: pytest_exit called*") From 5c09cc16f22df3b1853d28c551ea3738abf97479 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Tue, 23 Jul 2019 08:55:14 -0300 Subject: [PATCH 3/3] Add changelog entry for #5634 --- changelog/5634.bugfix.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/5634.bugfix.rst diff --git a/changelog/5634.bugfix.rst b/changelog/5634.bugfix.rst new file mode 100644 index 000000000..f4699919b --- /dev/null +++ b/changelog/5634.bugfix.rst @@ -0,0 +1 @@ +``pytest.exit`` and ``bdb.BdbQuit`` are now correctly handled in ``unittest`` cases.