From 4a568f43fe2598ca6f521eb7424ecc50a7f2efd3 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Fri, 15 Jan 2010 17:50:02 +0100 Subject: [PATCH] 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 --- doc/example/assertion/test_failures.py | 2 +- py/_test/funcargs.py | 25 ------------------------- py/_test/pycollect.py | 24 +++++++++++++++++++----- testing/test_funcargs.py | 18 ++++++++++++++++++ 4 files changed, 38 insertions(+), 31 deletions(-) diff --git a/doc/example/assertion/test_failures.py b/doc/example/assertion/test_failures.py index b2b2bae6f..a4d66bf1a 100644 --- a/doc/example/assertion/test_failures.py +++ b/doc/example/assertion/test_failures.py @@ -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 diff --git a/py/_test/funcargs.py b/py/_test/funcargs.py index 29af77c48..52eea0762 100644 --- a/py/_test/funcargs.py +++ b/py/_test/funcargs.py @@ -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 diff --git a/py/_test/pycollect.py b/py/_test/pycollect.py index e8c1c7ce1..34f5f6a5c 100644 --- a/py/_test/pycollect.py +++ b/py/_test/pycollect.py @@ -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 diff --git a/testing/test_funcargs.py b/testing/test_funcargs.py index 5c48a34b8..6a621cc2e 100644 --- a/testing/test_funcargs.py +++ b/testing/test_funcargs.py @@ -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")