remove unneccessary internal __request__ funcarg.

This commit is contained in:
holger krekel 2012-10-01 09:23:39 +02:00
parent bee7543716
commit 57a832812b
2 changed files with 26 additions and 36 deletions

View File

@ -519,11 +519,8 @@ def hasinit(obj):
def fillfuncargs(function): def fillfuncargs(function):
""" fill missing funcargs. """ """ fill missing funcargs for a test function. """
#if not getattr(function, "_args", None) is not None: if getattr(function, "_args", None) is None: # not a yielded function
# request = FuncargRequest(pyfuncitem=function)
# request._fillfuncargs()
if getattr(function, "_args", None) is None:
try: try:
request = function._request request = function._request
except AttributeError: except AttributeError:
@ -941,11 +938,6 @@ def scopeproperty(name=None, doc=None):
return property(provide, None, None, func.__doc__) return property(provide, None, None, func.__doc__)
return decoratescope return decoratescope
def pytest_funcarg__request(__request__):
return __request__
#def pytest_funcarg__testcontext(__request__):
# return __request__
class FuncargRequest: class FuncargRequest:
""" A request for function arguments from a test or setup function. """ A request for function arguments from a test or setup function.
@ -963,7 +955,6 @@ class FuncargRequest:
self.scope = "function" self.scope = "function"
self.getparent = pyfuncitem.getparent self.getparent = pyfuncitem.getparent
self._funcargs = self._pyfuncitem.funcargs.copy() self._funcargs = self._pyfuncitem.funcargs.copy()
self._funcargs["__request__"] = self
self._name2factory = {} self._name2factory = {}
self.funcargmanager = pyfuncitem.session.funcargmanager self.funcargmanager = pyfuncitem.session.funcargmanager
self._currentarg = None self._currentarg = None
@ -1078,9 +1069,6 @@ class FuncargRequest:
def _fillfuncargs(self): def _fillfuncargs(self):
item = self._pyfuncitem item = self._pyfuncitem
funcargnames = getattr(item, "funcargnames", self.funcargnames) funcargnames = getattr(item, "funcargnames", self.funcargnames)
if funcargnames:
assert not getattr(item, '_args', None), (
"yielded functions cannot have funcargs")
for argname in funcargnames: for argname in funcargnames:
if argname not in item.funcargs: if argname not in item.funcargs:
item.funcargs[argname] = self.getfuncargvalue(argname) item.funcargs[argname] = self.getfuncargvalue(argname)
@ -1128,23 +1116,28 @@ class FuncargRequest:
def getfuncargvalue(self, argname): def getfuncargvalue(self, argname):
""" (deprecated) Retrieve a function argument by name for this test """ Retrieve a function argument by name for this test
function invocation. This allows one function argument factory function invocation. This allows one function argument factory
to call another function argument factory. If there are two to call another function argument factory. If there are two
funcarg factories for the same test function argument the first funcarg factories for the same test function argument the first
factory may use ``getfuncargvalue`` to call the second one and factory may use ``getfuncargvalue`` to call the second one and
do something additional with the resource. do something additional with the resource.
**Note**, however, that starting with **Note**, however, that starting with pytest-2.3 it is usually
pytest-2.3 it is easier and better to directly state the needed easier and better to directly use the needed funcarg in the
funcarg in the factory signature. This will also work seemlessly factory function signature. This will also work seemlessly
with parametrization and the new resource setup optimizations. with parametrization and the new resource setup optimizations.
""" """
try: try:
return self._funcargs[argname] return self._funcargs[argname]
except KeyError: except KeyError:
pass pass
factorydeflist = self._getfaclist(argname) try:
factorydeflist = self._getfaclist(argname)
except FuncargLookupError:
if argname == "request":
return self
raise
factorydef = factorydeflist.pop() factorydef = factorydeflist.pop()
self._factorystack.append(factorydef) self._factorystack.append(factorydef)
try: try:
@ -1338,10 +1331,6 @@ class FuncargManager:
if facdeflist is not None: if facdeflist is not None:
for facdef in facdeflist: for facdef in facdeflist:
merge(facdef.funcargnames) merge(facdef.funcargnames)
try:
funcargnames.remove("__request__")
except ValueError:
pass
return funcargnames, arg2facdeflist return funcargnames, arg2facdeflist
def pytest_generate_tests(self, metafunc): def pytest_generate_tests(self, metafunc):

View File

@ -1758,7 +1758,6 @@ class TestFuncargFactory:
"*LookupError: no factory found for argument 'missing'", "*LookupError: no factory found for argument 'missing'",
]) ])
def test_factory_setup_as_classes(self, testdir): def test_factory_setup_as_classes(self, testdir):
testdir.makepyfile(""" testdir.makepyfile("""
import pytest import pytest
@ -1779,6 +1778,19 @@ class TestFuncargFactory:
reprec = testdir.inline_run() reprec = testdir.inline_run()
reprec.assertoutcome(passed=1) reprec.assertoutcome(passed=1)
def test_request_can_be_overridden(self, testdir):
testdir.makepyfile("""
import pytest
@pytest.factory()
def request(request):
request.a = 1
return request
def test_request(request):
assert request.a == 1
""")
reprec = testdir.inline_run()
reprec.assertoutcome(passed=1)
class TestResourceIntegrationFunctional: class TestResourceIntegrationFunctional:
def test_parametrize_with_ids(self, testdir): def test_parametrize_with_ids(self, testdir):
@ -2661,18 +2673,7 @@ def test_setupdecorator_and_xunit(testdir):
reprec = testdir.inline_run() reprec = testdir.inline_run()
reprec.assertoutcome(passed=3) reprec.assertoutcome(passed=3)
def test_request_can_be_overridden(testdir):
testdir.makepyfile("""
import pytest
@pytest.factory()
def request(request):
request.a = 1
return request
def test_request(request):
assert request.a == 1
""")
reprec = testdir.inline_run()
reprec.assertoutcome(passed=1)
def test_setup_funcarg_order(testdir): def test_setup_funcarg_order(testdir):
testdir.makepyfile(""" testdir.makepyfile("""