From 70840f605e9c230d5a100e998509e4d8e957b996 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Tue, 14 Apr 2009 02:23:42 +0200 Subject: [PATCH] move tests to test_funcargs.py and start with new request object tests --HG-- branch : trunk --- py/test/pluginmanager.py | 4 +- py/test/pycollect.py | 18 +++++- py/test/testing/test_funcargs.py | 103 ++++++++++++++++++++++++++++++ py/test/testing/test_pycollect.py | 82 ------------------------ 4 files changed, 121 insertions(+), 86 deletions(-) create mode 100644 py/test/testing/test_funcargs.py diff --git a/py/test/pluginmanager.py b/py/test/pluginmanager.py index b2aa238d6..08a480ea8 100644 --- a/py/test/pluginmanager.py +++ b/py/test/pluginmanager.py @@ -79,8 +79,8 @@ class PluginManager(object): for x in self.comregistry.listattr(attrname): return x - def listattr(self, attrname, plugins=None): - return self.comregistry.listattr(attrname, plugins=plugins) + def listattr(self, attrname, plugins=None, extra=()): + return self.comregistry.listattr(attrname, plugins=plugins, extra=extra) def call_firstresult(self, *args, **kwargs): return self.comregistry.call_firstresult(*args, **kwargs) diff --git a/py/test/pycollect.py b/py/test/pycollect.py index eac49c3b3..0d50ae04e 100644 --- a/py/test/pycollect.py +++ b/py/test/pycollect.py @@ -410,6 +410,20 @@ class Function(FunctionMixin, py.test.collect.Item): def __ne__(self, other): return not self == other + def getrequest(self, argname): + return FuncargRequest(pyfuncitem=self, argname=argname) + -# DEPRECATED -#from py.__.test.plugin.pytest_doctest import DoctestFile +class FuncargRequest: + def __init__(self, pyfuncitem, argname): + self.pyfuncitem = pyfuncitem + self.argname = argname + funcargname = "pytest_funcarg__" + str(argname) + pm = self.pyfuncitem.config.pluginmanager + extra = [] + current = pyfuncitem + while not isinstance(current, Module): + current = current.parent + if isinstance(current, (Instance, Module)): + extra.insert(0, current.obj) + self._methods = pm.listattr(funcargname, extra=extra) diff --git a/py/test/testing/test_funcargs.py b/py/test/testing/test_funcargs.py new file mode 100644 index 000000000..582520f4f --- /dev/null +++ b/py/test/testing/test_funcargs.py @@ -0,0 +1,103 @@ +import py + +class TestFuncargs: + def test_funcarg_lookupfails(self, testdir): + testdir.makeconftest(""" + class ConftestPlugin: + def pytest_funcarg__something(self, pyfuncitem): + return 42 + """) + item = testdir.getitem("def test_func(some): pass") + exc = py.test.raises(LookupError, "item.setupargs()") + s = str(exc.value) + assert s.find("something") != -1 + + def test_funcarg_lookup_default(self, testdir): + item = testdir.getitem("def test_func(some, other=42): pass") + class Provider: + def pytest_funcarg__some(self, pyfuncitem): + return pyfuncitem.name + item.config.pluginmanager.register(Provider()) + item.setupargs() + assert len(item.funcargs) == 1 + + def test_funcarg_lookup_default_gets_overriden(self, testdir): + item = testdir.getitem("def test_func(some=42, other=13): pass") + class Provider: + def pytest_funcarg__other(self, pyfuncitem): + return pyfuncitem.name + item.config.pluginmanager.register(Provider()) + item.setupargs() + assert len(item.funcargs) == 1 + name, value = item.funcargs.popitem() + assert name == "other" + assert value == item.name + + def test_funcarg_basic(self, testdir): + item = testdir.getitem("def test_func(some, other): pass") + class Provider: + def pytest_funcarg__some(self, pyfuncitem): + return pyfuncitem.name + def pytest_funcarg__other(self, pyfuncitem): + return 42 + item.config.pluginmanager.register(Provider()) + item.setupargs() + assert len(item.funcargs) == 2 + assert item.funcargs['some'] == "test_func" + assert item.funcargs['other'] == 42 + + def test_funcarg_addfinalizer(self, testdir): + item = testdir.getitem("def test_func(some): pass") + l = [] + class Provider: + def pytest_funcarg__some(self, pyfuncitem): + pyfuncitem.addfinalizer(lambda: l.append(42)) + return 3 + item.config.pluginmanager.register(Provider()) + item.setupargs() + assert len(item.funcargs) == 1 + assert item.funcargs['some'] == 3 + assert len(l) == 0 + item.teardown() + assert len(l) == 1 + assert l[0] == 42 + + def test_funcarg_lookup_modulelevel(self, testdir): + modcol = testdir.getmodulecol(""" + def pytest_funcarg__something(pyfuncitem): + return pyfuncitem.name + + class TestClass: + def test_method(self, something): + pass + def test_func(something): + pass + """) + item1, item2 = testdir.genitems([modcol]) + modcol.setup() + assert modcol.config.pluginmanager.isregistered(modcol.obj) + item1.setupargs() + assert item1.funcargs['something'] == "test_method" + item2.setupargs() + assert item2.funcargs['something'] == "test_func" + modcol.teardown() + assert not modcol.config.pluginmanager.isregistered(modcol.obj) + +class TestRequest: + def test_request_contains_funcargs_methods(self, testdir): + modcol = testdir.getmodulecol(""" + def pytest_funcarg__something(request): + pass + class TestClass: + def pytest_funcarg__something(self, request): + pass + def test_method(self, something): + pass + """) + item1, = testdir.genitems([modcol]) + assert item1.name == "test_method" + methods = item1.getrequest("something")._methods + assert len(methods) == 2 + method1, method2 = methods + assert not hasattr(method1, 'im_self') + assert method2.im_self is not None diff --git a/py/test/testing/test_pycollect.py b/py/test/testing/test_pycollect.py index b2fb2f8fa..cd680eaed 100644 --- a/py/test/testing/test_pycollect.py +++ b/py/test/testing/test_pycollect.py @@ -243,88 +243,6 @@ class TestFunction: assert f1 == f1_b assert not f1 != f1_b - def test_funcarg_lookupfails(self, testdir): - testdir.makeconftest(""" - class ConftestPlugin: - def pytest_funcarg__something(self, pyfuncitem): - return 42 - """) - item = testdir.getitem("def test_func(some): pass") - exc = py.test.raises(LookupError, "item.setupargs()") - s = str(exc.value) - assert s.find("something") != -1 - - def test_funcarg_lookup_default(self, testdir): - item = testdir.getitem("def test_func(some, other=42): pass") - class Provider: - def pytest_funcarg__some(self, pyfuncitem): - return pyfuncitem.name - item.config.pluginmanager.register(Provider()) - item.setupargs() - assert len(item.funcargs) == 1 - - def test_funcarg_lookup_default_gets_overriden(self, testdir): - item = testdir.getitem("def test_func(some=42, other=13): pass") - class Provider: - def pytest_funcarg__other(self, pyfuncitem): - return pyfuncitem.name - item.config.pluginmanager.register(Provider()) - item.setupargs() - assert len(item.funcargs) == 1 - name, value = item.funcargs.popitem() - assert name == "other" - assert value == item.name - - def test_funcarg_basic(self, testdir): - item = testdir.getitem("def test_func(some, other): pass") - class Provider: - def pytest_funcarg__some(self, pyfuncitem): - return pyfuncitem.name - def pytest_funcarg__other(self, pyfuncitem): - return 42 - item.config.pluginmanager.register(Provider()) - item.setupargs() - assert len(item.funcargs) == 2 - assert item.funcargs['some'] == "test_func" - assert item.funcargs['other'] == 42 - - def test_funcarg_addfinalizer(self, testdir): - item = testdir.getitem("def test_func(some): pass") - l = [] - class Provider: - def pytest_funcarg__some(self, pyfuncitem): - pyfuncitem.addfinalizer(lambda: l.append(42)) - return 3 - item.config.pluginmanager.register(Provider()) - item.setupargs() - assert len(item.funcargs) == 1 - assert item.funcargs['some'] == 3 - assert len(l) == 0 - item.teardown() - assert len(l) == 1 - assert l[0] == 42 - - def test_funcarg_lookup_modulelevel(self, testdir): - modcol = testdir.getmodulecol(""" - def pytest_funcarg__something(pyfuncitem): - return pyfuncitem.name - - class TestClass: - def test_method(self, something): - pass - def test_func(something): - pass - """) - item1, item2 = testdir.genitems([modcol]) - modcol.setup() - assert modcol.config.pluginmanager.isregistered(modcol.obj) - item1.setupargs() - assert item1.funcargs['something'] == "test_method" - item2.setupargs() - assert item2.funcargs['something'] == "test_func" - modcol.teardown() - assert not modcol.config.pluginmanager.isregistered(modcol.obj) - class TestSorting: def test_check_equality_and_cmp_basic(self, testdir): modcol = testdir.getmodulecol("""