From 76785e32c30f446da93a0ee4eefa6f2928f19ad6 Mon Sep 17 00:00:00 2001 From: AbdealiJK Date: Wed, 18 May 2016 22:39:58 +0530 Subject: [PATCH] unittest.UnitTestCase: Allow __test__ for methods __test__ needs to be checked for methods of a class too. Earlier, this was not done, and all methods in a class was assumed to be a test. This commit adds the appropriate condition to ensure that if the __test__ is set to False, it does not collect that method. Fixes https://github.com/pytest-dev/pytest/issues/1558 --- AUTHORS | 1 + CHANGELOG.rst | 3 +++ _pytest/unittest.py | 2 ++ testing/test_unittest.py | 14 ++++++++++++++ 4 files changed, 20 insertions(+) diff --git a/AUTHORS b/AUTHORS index f4a21b22dd4..e25d1727ab7 100644 --- a/AUTHORS +++ b/AUTHORS @@ -3,6 +3,7 @@ merlinux GmbH, Germany, office at merlinux eu Contributors include:: +Abdeali JK Abhijeet Kasurde Anatoly Bubenkoff Andreas Zeidler diff --git a/CHANGELOG.rst b/CHANGELOG.rst index ab5da9d01c2..ff921d1064f 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -5,6 +5,9 @@ * +* Support nose-style ``__test__`` attribute on methods of classes. + If set to False, the test will not be collected. + * Fix win32 path issue when puttinging custom config file with absolute path in ``pytest.main("-c your_absolute_path")``. diff --git a/_pytest/unittest.py b/_pytest/unittest.py index 8120e94fbf4..f24b496242a 100644 --- a/_pytest/unittest.py +++ b/_pytest/unittest.py @@ -50,6 +50,8 @@ def collect(self): foundsomething = False for name in loader.getTestCaseNames(self.obj): x = getattr(self.obj, name) + if not getattr(x, '__test__', True): + continue funcobj = getattr(x, 'im_func', x) transfer_markers(funcobj, cls, module) yield TestCaseFunction(name, parent=self) diff --git a/testing/test_unittest.py b/testing/test_unittest.py index 144aad79bf4..73735b8cd0d 100644 --- a/testing/test_unittest.py +++ b/testing/test_unittest.py @@ -735,3 +735,17 @@ def test_skip(self): *SKIP*[1]*skipping due to reasons* *1 skipped* """) + +def test_class_method_containing_test_issue1558(testdir): + testdir.makepyfile(test_foo=""" + import unittest + + class MyTestCase(unittest.TestCase): + def test_should_run(self): + pass + def test_should_not_run(self): + pass + test_should_not_run.__test__ = False + """) + reprec = testdir.inline_run() + reprec.assertoutcome(passed=1)