Skip to content

Commit 3d9c5cf

Browse files
authored
Merge pull request #2225 from mbyt/allow_skipping_unittests_with_pdb_active
Allow to skip unittests if --pdb active
2 parents a4fb971 + 6a097aa commit 3d9c5cf

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

CHANGELOG.rst

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
3.0.7 (unreleased)
22
==================
33

4-
*
4+
* Fix regression, pytest now skips unittest correctly if run with ``--pdb``
5+
(`#2137`_). Thanks to `@gst`_ for the report and `@mbyt`_ for the PR.
56

67
* Ignore exceptions raised from descriptors (e.g. properties) during Python test collection (`#2234`_).
78
Thanks to `@bluetech`_.
9+
10+
*
811

912
* Replace ``raise StopIteration`` usages in the code by simple ``returns`` to finish generators, in accordance to `PEP-479`_ (`#2160`_).
1013
Thanks `@tgoodlet`_ for the report and `@nicoddemus`_ for the PR.
14+
15+
*
1116

1217
* Skipping plugin now also works with test items generated by custom collectors (`#2231`_).
1318
Thanks to `@vidartf`_.
19+
20+
*
1421

1522
* Conditionless ``xfail`` markers no longer rely on the underlying test item
1623
being an instance of ``PyobjMixin``, and can therefore apply to tests not
@@ -19,17 +26,16 @@
1926

2027
*
2128

22-
.. _#2234: https://github.com/pytest-dev/pytest/issues/2234
23-
2429
.. _@bluetech: https://github.com/bluetech
30+
.. _@gst: https://github.com/gst
31+
.. _@vidartf: https://github.com/vidartf
2532

33+
.. _#2137: https://github.com/pytest-dev/pytest/issues/2137
2634
.. _#2160: https://github.com/pytest-dev/pytest/issues/2160
27-
28-
.. _PEP-479: https://www.python.org/dev/peps/pep-0479/
29-
3035
.. _#2231: https://github.com/pytest-dev/pytest/issues/2231
36+
.. _#2234: https://github.com/pytest-dev/pytest/issues/2234
3137

32-
.. _@vidartf: https://github.com/vidartf
38+
.. _PEP-479: https://www.python.org/dev/peps/pep-0479/
3339

3440

3541
3.0.6 (2017-01-22)

_pytest/unittest.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ def collect(self):
6565
yield TestCaseFunction('runTest', parent=self)
6666

6767

68-
6968
class TestCaseFunction(pytest.Function):
7069
_excinfo = None
7170

@@ -152,14 +151,33 @@ def addSuccess(self, testcase):
152151
def stopTest(self, testcase):
153152
pass
154153

154+
def _handle_skip(self):
155+
# implements the skipping machinery (see #2137)
156+
# analog to pythons Lib/unittest/case.py:run
157+
testMethod = getattr(self._testcase, self._testcase._testMethodName)
158+
if (getattr(self._testcase.__class__, "__unittest_skip__", False) or
159+
getattr(testMethod, "__unittest_skip__", False)):
160+
# If the class or method was skipped.
161+
skip_why = (getattr(self._testcase.__class__, '__unittest_skip_why__', '') or
162+
getattr(testMethod, '__unittest_skip_why__', ''))
163+
try: # PY3, unittest2 on PY2
164+
self._testcase._addSkip(self, self._testcase, skip_why)
165+
except TypeError: # PY2
166+
if sys.version_info[0] != 2:
167+
raise
168+
self._testcase._addSkip(self, skip_why)
169+
return True
170+
return False
171+
155172
def runtest(self):
156173
if self.config.pluginmanager.get_plugin("pdbinvoke") is None:
157174
self._testcase(result=self)
158175
else:
159176
# disables tearDown and cleanups for post mortem debugging (see #1890)
177+
if self._handle_skip():
178+
return
160179
self._testcase.debug()
161180

162-
163181
def _prunetraceback(self, excinfo):
164182
pytest.Function._prunetraceback(self, excinfo)
165183
traceback = excinfo.traceback.filter(

testing/test_pdb.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,21 @@ def test_false(self):
106106
assert 'debug.me' in rest
107107
self.flush(child)
108108

109+
def test_pdb_unittest_skip(self, testdir):
110+
"""Test for issue #2137"""
111+
p1 = testdir.makepyfile("""
112+
import unittest
113+
@unittest.skipIf(True, 'Skipping also with pdb active')
114+
class MyTestCase(unittest.TestCase):
115+
def test_one(self):
116+
assert 0
117+
""")
118+
child = testdir.spawn_pytest("-rs --pdb %s" % p1)
119+
child.expect('Skipping also with pdb active')
120+
child.expect('1 skipped in')
121+
child.sendeof()
122+
self.flush(child)
123+
109124
def test_pdb_interaction_capture(self, testdir):
110125
p1 = testdir.makepyfile("""
111126
def test_1():

0 commit comments

Comments
 (0)