get rid of the funccollector node, which nice-ifies names of funcarg-generated tests nodes, also test and fix one anomaly wrt to funcarg setups and instance uniqueness

--HG--
branch : trunk
This commit is contained in:
holger krekel 2010-01-15 17:50:02 +01:00
parent ee2f292efa
commit 4a568f43fe
4 changed files with 38 additions and 31 deletions

View File

@ -11,4 +11,4 @@ def test_failure_demo_fails_properly(testdir):
assert failed == 20, failed
colreports = reprec.getreports("pytest_collectreport")
failed = len([x.failed for x in colreports])
assert failed == 4
assert failed == 3

View File

@ -60,31 +60,6 @@ class Metafunc:
self._ids.add(id)
self._calls.append(CallSpec(funcargs, id, param))
class FunctionCollector(py.test.collect.Collector):
def __init__(self, name, parent, calls):
super(FunctionCollector, self).__init__(name, parent)
self.calls = calls
self.obj = getattr(self.parent.obj, name)
def collect(self):
l = []
for callspec in self.calls:
name = "%s[%s]" %(self.name, callspec.id)
function = self.parent.Function(name=name, parent=self,
callspec=callspec, callobj=self.obj)
l.append(function)
return l
def reportinfo(self):
try:
return self._fslineno, self.name
except AttributeError:
pass
fspath, lineno = py.code.getfslineno(self.obj)
self._fslineno = fspath, lineno
return fspath, lineno, self.name
class FuncargRequest:
_argprefix = "pytest_funcarg__"
_argname = None

View File

@ -89,8 +89,11 @@ class PyCollectorMixin(PyobjMixin, py.test.collect.Collector):
seen[name] = True
if name[0] != "_":
res = self.makeitem(name, obj)
if res is not None:
l.append(res)
if res is None:
continue
if not isinstance(res, list):
res = [res]
l.extend(res)
l.sort(key=lambda item: item.reportinfo()[:2])
return l
@ -122,9 +125,13 @@ class PyCollectorMixin(PyobjMixin, py.test.collect.Collector):
gentesthook.pcall(plugins, metafunc=metafunc)
if not metafunc._calls:
return self.Function(name, parent=self)
return funcargs.FunctionCollector(name=name,
parent=self, calls=metafunc._calls)
l = []
for callspec in metafunc._calls:
subname = "%s[%s]" %(name, callspec.id)
function = self.Function(name=subname, parent=self,
callspec=callspec, callobj=funcobj)
l.append(function)
return l
class Module(py.test.collect.File, PyCollectorMixin):
def _getobj(self):
@ -313,6 +320,13 @@ class Function(FunctionMixin, py.test.collect.Item):
self._obj = callobj
self.function = getattr(self.obj, 'im_func', self.obj)
def _getobj(self):
name = self.name
i = name.find("[") # parametrization
if i != -1:
name = name[:i]
return getattr(self.parent.obj, name)
def _isyieldedfunction(self):
return self._args is not None

View File

@ -498,6 +498,24 @@ class TestGenfuncFunctional:
"*1 passed*"
])
def test_two_functions_not_same_instance(self, testdir):
p = testdir.makepyfile("""
def pytest_generate_tests(metafunc):
metafunc.addcall({'arg1': 10})
metafunc.addcall({'arg1': 20})
class TestClass:
def test_func(self, arg1):
assert not hasattr(self, 'x')
self.x = 1
""")
result = testdir.runpytest("-v", p)
assert result.stdout.fnmatch_lines([
"*test_func*0*PASS*",
"*test_func*1*PASS*",
"*2 pass*",
])
def test_conftest_funcargs_only_available_in_subdir(testdir):
sub1 = testdir.mkpydir("sub1")