From fbe8315f7620f1dbc6a83353964145a6a6265337 Mon Sep 17 00:00:00 2001 From: hpk Date: Mon, 2 Mar 2009 23:43:31 +0100 Subject: [PATCH] [svn r62430] honour unitttest function default values for pyfuncarg protocol --HG-- branch : trunk --- py/test/pycollect.py | 14 ++++++++++++-- py/test/testing/test_pycollect.py | 23 ++++++++++++++++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/py/test/pycollect.py b/py/test/pycollect.py index 30a44217c..df610675e 100644 --- a/py/test/pycollect.py +++ b/py/test/pycollect.py @@ -360,8 +360,18 @@ class Function(FunctionMixin, py.test.collect.Item): # standard Python Test function/method case funcobj = self.obj startindex = getattr(funcobj, 'im_self', None) and 1 or 0 - for argname in py.std.inspect.getargs(self.obj.func_code)[0][startindex:]: - kwargs[argname] = self.lookup_onearg(argname) + argnames = py.std.inspect.getargs(self.obj.func_code)[0] + for i, argname in py.builtin.enumerate(argnames): + if i < startindex: + continue + try: + kwargs[argname] = self.lookup_onearg(argname) + except LookupError, e: + numdefaults = len(funcobj.func_defaults or ()) + if i + numdefaults >= len(argnames): + continue # continue # seems that our args have defaults + else: + raise else: pass # XXX lookup of arguments for yielded/generated tests as well return kwargs diff --git a/py/test/testing/test_pycollect.py b/py/test/testing/test_pycollect.py index 3d8fd946d..5c7c7d1d5 100644 --- a/py/test/testing/test_pycollect.py +++ b/py/test/testing/test_pycollect.py @@ -229,9 +229,30 @@ class TestFunction: assert not f1 != f1_b def test_pyfuncarg_lookupfails(self, testdir): - item = testdir.getitem("def test_func(some, other): pass") + item = testdir.getitem("def test_func(some): pass") kw = py.test.raises(LookupError, "item.lookup_allargs()") + def test_pyfuncarg_lookup_default(self, testdir): + item = testdir.getitem("def test_func(some, other=42): pass") + class Provider: + def pytest_pyfuncarg_some(self, pyfuncitem): + return pyfuncitem.name + item._config.pytestplugins.register(Provider()) + kw = item.lookup_allargs() + assert len(kw) == 1 + + def test_pyfuncarg_lookup_default_gets_overriden(self, testdir): + item = testdir.getitem("def test_func(some=42, other=13): pass") + class Provider: + def pytest_pyfuncarg_other(self, pyfuncitem): + return pyfuncitem.name + item._config.pytestplugins.register(Provider()) + kw = item.lookup_allargs() + assert len(kw) == 1 + name, value = kw.popitem() + assert name == "other" + assert value == item.name + def test_pyfuncarg_basic(self, testdir): item = testdir.getitem("def test_func(some, other): pass") class Provider: