From 170346654c34b19f12a496bc35f1ff589e4eae46 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Sat, 16 Oct 2010 23:59:38 +0200 Subject: [PATCH] turn most funcargrequest attributes into properties and document them. --HG-- branch : trunk --- doc/funcargs.txt | 12 ++-------- pytest/plugin/python.py | 50 +++++++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 19 deletions(-) diff --git a/doc/funcargs.txt b/doc/funcargs.txt index 8695a8d18..1cdeb4766 100644 --- a/doc/funcargs.txt +++ b/doc/funcargs.txt @@ -90,16 +90,8 @@ Each funcarg factory receives a **request** object which is tied to a specific test function call. A request object is passed to a funcarg factory and provides access to test configuration and context: -``request.function``: python function object requesting the argument - -``request.cls``: class object where the test function is defined in or None. - -``request.module``: module object where the test function is defined in. - -``request.config``: access to command line opts and general config - -``request.param``: if exists was passed by a previous :py:meth:`~Metafunc.addcall` - +.. autoclass:: pytest.plugin.python.FuncargRequest() + :members: function,cls,module,keywords,config .. _`useful caching and finalization helpers`: diff --git a/pytest/plugin/python.py b/pytest/plugin/python.py index 20ed391d3..8a53cf5a9 100644 --- a/pytest/plugin/python.py +++ b/pytest/plugin/python.py @@ -527,22 +527,15 @@ class Metafunc: self._calls.append(CallSpec(funcargs, id, param)) class FuncargRequest: + """ A request for function arguments from a test function. """ _argprefix = "pytest_funcarg__" _argname = None class LookupError(LookupError): """ error on performing funcarg request. """ - + def __init__(self, pyfuncitem): self._pyfuncitem = pyfuncitem - self.function = pyfuncitem.obj - self.keywords = pyfuncitem.keywords - self.module = pyfuncitem.getparent(pytest.collect.Module).obj - clscol = pyfuncitem.getparent(pytest.collect.Class) - self.cls = clscol and clscol.obj or None - self.instance = py.builtin._getimself(self.function) - self.config = pyfuncitem.config - self.fspath = pyfuncitem.fspath if hasattr(pyfuncitem, '_requestparam'): self.param = pyfuncitem._requestparam self._plugins = getplugins(pyfuncitem, withpy=True) @@ -550,6 +543,45 @@ class FuncargRequest: self._name2factory = {} self._currentarg = None + @property + def function(self): + """ function object of the test invocation. """ + return self._pyfuncitem.obj + + @property + def keywords(self): + """ keywords of the test function item. + + .. versionadded:: 2.0 + """ + return self._pyfuncitem.keywords + + @property + def module(self): + """ module where the test function was collected. """ + return self._pyfuncitem.getparent(pytest.collect.Module).obj + + @property + def cls(self): + """ class (can be None) where the test function was collected. """ + clscol = self._pyfuncitem.getparent(pytest.collect.Class) + if clscol: + return clscol.obj + @property + def instance(self): + """ instance (can be None) on which test function was collected. """ + return py.builtin._getimself(self.function) + + @property + def config(self): + """ the pytest config object associated with this request. """ + return self._pyfuncitem.config + + @property + def fspath(self): + """ the file system path of the test module which collected this test. """ + return self._pyfuncitem.fspath + def _fillfuncargs(self): argnames = getfuncargnames(self.function) if argnames: