Test* classes with an __init__ method are not collected anymore

--HG--
branch : 1.0.x
This commit is contained in:
holger krekel 2009-08-06 18:15:21 +02:00
parent 58c6971dc8
commit d41949a6e3
4 changed files with 42 additions and 17 deletions

View File

@ -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

View File

@ -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)

View File

@ -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

View File

@ -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("""