From ee902a2d06fa461dc55422a8b7b0037a4ec5d664 Mon Sep 17 00:00:00 2001 From: hpk Date: Thu, 26 Mar 2009 10:14:09 +0100 Subject: [PATCH] [svn r63337] provide more info for the pyfuncarg failing lookup improve docstring --HG-- branch : trunk --- py/test/plugin/pytest_xfail.py | 2 +- py/test/pycollect.py | 24 ++++++++++++++++++------ py/test/testing/test_pycollect.py | 9 ++++++++- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/py/test/plugin/pytest_xfail.py b/py/test/plugin/pytest_xfail.py index 208d2e31c..413eb7aaa 100644 --- a/py/test/plugin/pytest_xfail.py +++ b/py/test/plugin/pytest_xfail.py @@ -1,6 +1,6 @@ """ for marking and reporting "expected to fail" tests. - @py.test.mark(xfail="needs refactoring") + @py.test.mark.xfail("needs refactoring") def test_hello(): ... assert 0 diff --git a/py/test/pycollect.py b/py/test/pycollect.py index df16e4d0f..cbe680f82 100644 --- a/py/test/pycollect.py +++ b/py/test/pycollect.py @@ -375,11 +375,12 @@ class Function(FunctionMixin, py.test.collect.Item): return kwargs def lookup_onearg(self, argname): + prefix = "pytest_pyfuncarg_" try: makerlist = self.config._getmakerlist(argname) except KeyError: makerlist = [] - l = self.config.pytestplugins.listattr("pytest_pyfuncarg_" + argname) + l = self.config.pytestplugins.listattr(prefix + argname) makerlist.extend(l) mc = py._com.MultiCall(makerlist, self) #print "mc.methods", mc.methods @@ -387,11 +388,22 @@ class Function(FunctionMixin, py.test.collect.Item): if value is not None: return value else: - metainfo = self.repr_metainfo() - #self.config.bus.notify("pyfuncarg_lookuperror", argname) - msg = "funcargument %r not found for: %s" %(argname,metainfo.verboseline()) - msg += "\n list of makers: %r" %(l,) - raise LookupError(msg) + self._raisefuncargerror(argname, prefix) + + def _raisefuncargerror(self, argname, prefix="pytest_pyfuncarg_"): + metainfo = self.repr_metainfo() + available = [] + plugins = self.config.pytestplugins._plugins.values() + plugins.extend(self.config.pytestplugins.pyplugins._plugins) + for plugin in plugins: + for name in vars(plugin.__class__): + if name.startswith(prefix): + name = name[len(prefix):] + if name not in available: + available.append(name) + msg = "funcargument %r not found for: %s" %(argname,metainfo.verboseline()) + msg += "\n available funcargs: %s" %(", ".join(available),) + raise LookupError(msg) def __eq__(self, other): try: diff --git a/py/test/testing/test_pycollect.py b/py/test/testing/test_pycollect.py index 64f4f8705..0e23a7d34 100644 --- a/py/test/testing/test_pycollect.py +++ b/py/test/testing/test_pycollect.py @@ -250,8 +250,15 @@ class TestFunction: assert not f1 != f1_b def test_pyfuncarg_lookupfails(self, testdir): + testdir.makeconftest(""" + class ConftestPlugin: + def pytest_pyfuncarg_something(self, pyfuncitem): + return 42 + """) item = testdir.getitem("def test_func(some): pass") - kw = py.test.raises(LookupError, "item.lookup_allargs()") + exc = py.test.raises(LookupError, "item.lookup_allargs()") + s = str(exc.value) + assert s.find("something") != -1 def test_pyfuncarg_lookup_default(self, testdir): item = testdir.getitem("def test_func(some, other=42): pass")