From aa79c0a4b9c5ff31dc30532d80529c39ac41755d Mon Sep 17 00:00:00 2001 From: holger krekel Date: Mon, 22 Oct 2012 16:25:09 +0200 Subject: [PATCH] fix unittest emulation: TestCase.runTest is now ignored if there are test* methods. --- CHANGELOG | 3 +++ _pytest/__init__.py | 2 +- _pytest/unittest.py | 7 +++++-- setup.py | 2 +- testing/test_unittest.py | 15 ++++++++++++--- 5 files changed, 22 insertions(+), 7 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index b7e93862a..95b62c08c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -7,6 +7,9 @@ Changes between 2.3.1 and 2.3.2.dev - fix teardown-ordering for parametrized setups +- fix unittest behaviour: TestCase.runtest only called if there are + test methods defined + - "python setup.py test" now works with pytest itself - fix/improve internal/packaging related bits: diff --git a/_pytest/__init__.py b/_pytest/__init__.py index 4bbc15d32..337badd63 100644 --- a/_pytest/__init__.py +++ b/_pytest/__init__.py @@ -1,2 +1,2 @@ # -__version__ = '2.3.2.dev7' +__version__ = '2.3.2.dev8' diff --git a/_pytest/unittest.py b/_pytest/unittest.py index d7a02ccaf..982a6bc0d 100644 --- a/_pytest/unittest.py +++ b/_pytest/unittest.py @@ -28,6 +28,7 @@ class UnitTestCase(pytest.Class): loader = py.std.unittest.TestLoader() module = self.getparent(pytest.Module).obj cls = self.obj + foundsomething = False for name in loader.getTestCaseNames(self.obj): x = getattr(self.obj, name) funcobj = getattr(x, 'im_func', x) @@ -35,9 +36,11 @@ class UnitTestCase(pytest.Class): if hasattr(funcobj, 'todo'): pytest.mark.xfail(reason=str(funcobj.todo))(funcobj) yield TestCaseFunction(name, parent=self) + foundsomething = True - if getattr(self.obj, 'runTest', None) is not None: - yield TestCaseFunction('runTest', parent=self) + if not foundsomething: + if getattr(self.obj, 'runTest', None) is not None: + yield TestCaseFunction('runTest', parent=self) def setup(self): meth = getattr(self.obj, 'setUpClass', None) diff --git a/setup.py b/setup.py index 6478c0391..ff3263cf7 100644 --- a/setup.py +++ b/setup.py @@ -24,7 +24,7 @@ def main(): name='pytest', description='py.test: simple powerful testing with Python', long_description = long_description, - version='2.3.2.dev7', + version='2.3.2.dev8', url='http://pytest.org', license='MIT license', platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'], diff --git a/testing/test_unittest.py b/testing/test_unittest.py index ffba9fa66..94cfa17e0 100644 --- a/testing/test_unittest.py +++ b/testing/test_unittest.py @@ -18,12 +18,21 @@ def test_runTest_method(testdir): testpath=testdir.makepyfile(""" import unittest pytest_plugins = "pytest_unittest" - class MyTestCase(unittest.TestCase): + class MyTestCaseWithRunTest(unittest.TestCase): def runTest(self): self.assertEquals('foo', 'foo') + class MyTestCaseWithoutRunTest(unittest.TestCase): + def runTest(self): + self.assertEquals('foo', 'foo') + def test_something(self): + pass """) - reprec = testdir.inline_run(testpath) - assert reprec.matchreport('runTest').passed + result = testdir.runpytest("-v") + result.stdout.fnmatch_lines(""" + *MyTestCaseWithRunTest.runTest* + *MyTestCaseWithoutRunTest.test_something* + *2 passed* + """) def test_isclasscheck_issue53(testdir): testpath = testdir.makepyfile("""