From d41949a6e3476410ac649f1778d77c081d991ab1 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Thu, 6 Aug 2009 18:15:21 +0200 Subject: [PATCH] Test* classes with an __init__ method are not collected anymore --HG-- branch : 1.0.x --- CHANGELOG | 2 ++ py/test/plugin/test_pytest_runner_xunit.py | 20 +++++++----------- py/test/pycollect.py | 24 ++++++++++++++++++---- py/test/testing/test_pycollect.py | 13 ++++++++++++ 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index d7e06d730..c4120e896 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -8,6 +8,8 @@ Changes between 1.0.0 and 1.0.1 * fix issue #27: better reporting on non-collectable items given on commandline (e.g. pyc files) +* "Test" prefixed classes with an __init__ method are *not* collected by default anymore + * terser reporting of collection error tracebacks * renaming of arguments to some special rather internal hooks diff --git a/py/test/plugin/test_pytest_runner_xunit.py b/py/test/plugin/test_pytest_runner_xunit.py index 4ecf08d5c..18ff73d50 100644 --- a/py/test/plugin/test_pytest_runner_xunit.py +++ b/py/test/plugin/test_pytest_runner_xunit.py @@ -125,18 +125,12 @@ def test_func_generator_setup(testdir): def test_method_setup_uses_fresh_instances(testdir): reprec = testdir.inline_runsource(""" class TestSelfState1: - def __init__(self): - self.hello = 42 + memory = [] def test_hello(self): - self.world = 23 - def test_afterhello(self): - assert not hasattr(self, 'world') - assert self.hello == 42 - class TestSelfState2: - def test_hello(self): - self.world = 10 - def test_world(self): - assert not hasattr(self, 'world') - """) - reprec.assertoutcome(passed=4, failed=0) + self.memory.append(self) + + def test_afterhello(self): + assert self != self.memory[0] + """) + reprec.assertoutcome(passed=2, failed=0) diff --git a/py/test/pycollect.py b/py/test/pycollect.py index 25072c373..9e7bd49df 100644 --- a/py/test/pycollect.py +++ b/py/test/pycollect.py @@ -123,8 +123,7 @@ class PyCollectorMixin(PyobjMixin, py.test.collect.Collector): collector=self, name=name, obj=obj) if res is not None: return res - if (self.classnamefilter(name)) and \ - py.std.inspect.isclass(obj): + if self._istestclasscandidate(name, obj): res = self._deprecated_join(name) if res is not None: return res @@ -139,14 +138,25 @@ class PyCollectorMixin(PyobjMixin, py.test.collect.Collector): else: return self._genfunctions(name, obj) + def _istestclasscandidate(self, name, obj): + if self.classnamefilter(name) and \ + py.std.inspect.isclass(obj): + if hasinit(obj): + # XXX WARN + return False + return True + + def _genfunctions(self, name, funcobj): module = self.getparent(Module).obj # due to _buildname2items funcobj is the raw function, we need # to work to get at the class clscol = self.getparent(Class) cls = clscol and clscol.obj or None - metafunc = funcargs.Metafunc(funcobj, config=self.config, cls=cls, module=module) - gentesthook = self.config.hook.pytest_generate_tests.clone(extralookup=module) + metafunc = funcargs.Metafunc(funcobj, config=self.config, + cls=cls, module=module) + gentesthook = self.config.hook.pytest_generate_tests.clone( + extralookup=module) gentesthook(metafunc=metafunc) if not metafunc._calls: return self.Function(name, parent=self) @@ -371,3 +381,9 @@ class Function(FunctionMixin, py.test.collect.Item): def __ne__(self, other): return not self == other + +def hasinit(obj): + init = getattr(obj, '__init__', None) + if init: + if not isinstance(init, type(object.__init__)): + return True diff --git a/py/test/testing/test_pycollect.py b/py/test/testing/test_pycollect.py index e2dc190a8..dcec63e98 100644 --- a/py/test/testing/test_pycollect.py +++ b/py/test/testing/test_pycollect.py @@ -39,6 +39,19 @@ class TestModule: modcol = testdir.getmodulecol("pytest_plugins='xasdlkj',") py.test.raises(ImportError, "modcol.obj") +class TestClass: + def test_class_with_init_not_collected(self, testdir): + modcol = testdir.getmodulecol(""" + class TestClass1: + def __init__(self): + pass + class TestClass2(object): + def __init__(self): + pass + """) + l = modcol.collect() + assert len(l) == 0 + class TestDisabled: def test_disabled_module(self, testdir): modcol = testdir.getmodulecol("""