From 4eeb4751385519d54b304176f4712ec4ae01865b Mon Sep 17 00:00:00 2001 From: mbyt Date: Tue, 30 Aug 2016 21:55:49 +0200 Subject: [PATCH 1/4] avoid tearDown and cleanup for unittest debugging --- _pytest/unittest.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/_pytest/unittest.py b/_pytest/unittest.py index f24b496242a..5e87441cc4d 100644 --- a/_pytest/unittest.py +++ b/_pytest/unittest.py @@ -150,7 +150,12 @@ def stopTest(self, testcase): pass def runtest(self): - self._testcase(result=self) + if self.config.pluginmanager.get_plugin("pdbinvoke") is None: + self._testcase(result=self) + else: + # disables tearDown and cleanups for post mortem debugging + self._testcase.debug() + def _prunetraceback(self, excinfo): pytest.Function._prunetraceback(self, excinfo) From 10b3274924af304b705f98d2801bd196be4663db Mon Sep 17 00:00:00 2001 From: mbyt Date: Wed, 31 Aug 2016 20:22:54 +0200 Subject: [PATCH 2/4] adding corresponding test, authors and changelog --- AUTHORS | 1 + CHANGELOG.rst | 7 +++++++ testing/test_pdb.py | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/AUTHORS b/AUTHORS index 043de70c1b4..c74aa4af685 100644 --- a/AUTHORS +++ b/AUTHORS @@ -91,6 +91,7 @@ Martin Prusse Matt Bachmann Matt Williams Matthias Hafner +mbyt Michael Aquilina Michael Birtwell Michael Droettboom diff --git a/CHANGELOG.rst b/CHANGELOG.rst index a1aea9bbd97..b02afe03861 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -12,13 +12,20 @@ * Fix ``UnicodeEncodeError`` when string comparison with unicode has failed. (`#1864`_) Thanks `@AiOO`_ for the PR +* Do not call tearDown (and cleanups) when running unittest with ``--pdb`` + enabled. This allows proper post mortem debugging for all applications + which have significant logic in their tearDown method (`#1890`_). Thanks + `@mbyt`_ for the PR. + * .. _@joguSD: https://github.com/joguSD .. _@AiOO: https://github.com/AiOO +.. _@mbyt: https://github.com/mbyt .. _#1857: https://github.com/pytest-dev/pytest/issues/1857 .. _#1864: https://github.com/pytest-dev/pytest/issues/1864 +.. _#1890: https://github.com/pytest-dev/pytest/issues/1890 3.0.1 diff --git a/testing/test_pdb.py b/testing/test_pdb.py index 6e4f3e80582..a0fbc473e40 100644 --- a/testing/test_pdb.py +++ b/testing/test_pdb.py @@ -79,6 +79,25 @@ def test_1(): if child.isalive(): child.wait() + def test_pdb_unittest_postmortem(self, testdir): + p1 = testdir.makepyfile(""" + import unittest + class Blub(unittest.TestCase): + def tearDown(self): + self.filename = None + def test_false(self): + self.filename = 'bla' + '.txt' + assert 0 + """) + child = testdir.spawn_pytest("--pdb %s" % p1) + child.expect('(Pdb)') + child.sendline('p self.filename') + child.sendeof() + rest = child.read().decode("utf8") + assert 'bla.txt' in rest + if child.isalive(): + child.wait() + def test_pdb_interaction_capture(self, testdir): p1 = testdir.makepyfile(""" def test_1(): From 696a9112beacdaf970d06809dcb0fd04e69325e9 Mon Sep 17 00:00:00 2001 From: mbyt Date: Wed, 31 Aug 2016 22:33:47 +0200 Subject: [PATCH 3/4] integrating review commets of @nicoddemus plus small scale refactoring --- CHANGELOG.rst | 5 +++-- _pytest/unittest.py | 2 +- doc/en/unittest.rst | 7 +++++++ testing/test_pdb.py | 4 ++-- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 7d6db671bbe..44fc9f25924 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -18,9 +18,10 @@ if a test suite uses ``pytest_plugins`` to load internal plugins (`#1888`_). Thanks `@jaraco`_ for the report and `@nicoddemus`_ for the PR (`#1891`_). -* Do not call tearDown (and cleanups) when running unittest with ``--pdb`` +* Do not call tearDown and cleanups when running tests from + ``unittest.TestCase`` subclasses with ``--pdb`` enabled. This allows proper post mortem debugging for all applications - which have significant logic in their tearDown method (`#1890`_). Thanks + which have significant logic in their tearDown machinery (`#1890`_). Thanks `@mbyt`_ for the PR. * diff --git a/_pytest/unittest.py b/_pytest/unittest.py index 5e87441cc4d..47868f4483c 100644 --- a/_pytest/unittest.py +++ b/_pytest/unittest.py @@ -153,7 +153,7 @@ def runtest(self): if self.config.pluginmanager.get_plugin("pdbinvoke") is None: self._testcase(result=self) else: - # disables tearDown and cleanups for post mortem debugging + # disables tearDown and cleanups for post mortem debugging (see #1890) self._testcase.debug() diff --git a/doc/en/unittest.rst b/doc/en/unittest.rst index 9e0b4973d43..e4c09ee2458 100644 --- a/doc/en/unittest.rst +++ b/doc/en/unittest.rst @@ -33,6 +33,13 @@ distributing tests to multiple CPUs via the ``-nNUM`` option if you installed the ``pytest-xdist`` plugin. Please refer to the general ``pytest`` documentation for many more examples. +.. note:: + + Running tests from ``unittest.TestCase`` subclasses with ``--pdb`` will + disable tearDown and cleanup methods for the case that an Exception is + occurs. This allows proper post mortem debugging for all applications + which have significant logic in their tearDown machinery. + Mixing pytest fixtures into unittest.TestCase style tests ----------------------------------------------------------- diff --git a/testing/test_pdb.py b/testing/test_pdb.py index a0fbc473e40..d79d7126209 100644 --- a/testing/test_pdb.py +++ b/testing/test_pdb.py @@ -86,7 +86,7 @@ class Blub(unittest.TestCase): def tearDown(self): self.filename = None def test_false(self): - self.filename = 'bla' + '.txt' + self.filename = 'debug' + '.me' assert 0 """) child = testdir.spawn_pytest("--pdb %s" % p1) @@ -94,7 +94,7 @@ def test_false(self): child.sendline('p self.filename') child.sendeof() rest = child.read().decode("utf8") - assert 'bla.txt' in rest + assert 'debug.me' in rest if child.isalive(): child.wait() From e43d1174f7cf1b495c6ff07c807de0da9e89ba2f Mon Sep 17 00:00:00 2001 From: mbyt Date: Wed, 31 Aug 2016 22:46:40 +0200 Subject: [PATCH 4/4] spelling --- doc/en/unittest.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/en/unittest.rst b/doc/en/unittest.rst index e4c09ee2458..d400adcaa3d 100644 --- a/doc/en/unittest.rst +++ b/doc/en/unittest.rst @@ -36,7 +36,7 @@ the general ``pytest`` documentation for many more examples. .. note:: Running tests from ``unittest.TestCase`` subclasses with ``--pdb`` will - disable tearDown and cleanup methods for the case that an Exception is + disable tearDown and cleanup methods for the case that an Exception occurs. This allows proper post mortem debugging for all applications which have significant logic in their tearDown machinery.