- rename @funcarg to @factory
- introduce a "testcontext" object for new-style funcargs and setup methods - New-style funcargs and setup methods cannot use the "request" object anymore.
This commit is contained in:
parent
cb2eb9ba33
commit
535d892f27
|
@ -921,6 +921,11 @@ class FuncargRequest:
|
||||||
"""
|
"""
|
||||||
return self._pyfuncitem.keywords
|
return self._pyfuncitem.keywords
|
||||||
|
|
||||||
|
@property
|
||||||
|
def session(self):
|
||||||
|
""" pytest session object. """
|
||||||
|
return self._pyfuncitem.session
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def module(self):
|
def module(self):
|
||||||
""" module where the test function was collected. """
|
""" module where the test function was collected. """
|
||||||
|
@ -1029,13 +1034,18 @@ class FuncargRequest:
|
||||||
|
|
||||||
def _getfuncargvalue(self, factorydef):
|
def _getfuncargvalue(self, factorydef):
|
||||||
# collect funcargs from the factory
|
# collect funcargs from the factory
|
||||||
newnames = list(factorydef.funcargnames)
|
newnames = factorydef.funcargnames
|
||||||
newnames.remove("request")
|
|
||||||
argname = factorydef.argname
|
argname = factorydef.argname
|
||||||
factory_kwargs = {"request": self}
|
factory_kwargs = {}
|
||||||
def fillfactoryargs():
|
def fillfactoryargs():
|
||||||
for newname in newnames:
|
for newname in newnames:
|
||||||
factory_kwargs[newname] = self.getfuncargvalue(newname)
|
if newname == "testcontext":
|
||||||
|
val = TestContextResource(self)
|
||||||
|
elif newname == "request" and not factorydef.new:
|
||||||
|
val = self
|
||||||
|
else:
|
||||||
|
val = self.getfuncargvalue(newname)
|
||||||
|
factory_kwargs[newname] = val
|
||||||
|
|
||||||
node = self._pyfuncitem
|
node = self._pyfuncitem
|
||||||
mp = monkeypatch()
|
mp = monkeypatch()
|
||||||
|
@ -1237,20 +1247,22 @@ class FuncargManager:
|
||||||
obj = getattr(holderobj, name)
|
obj = getattr(holderobj, name)
|
||||||
if not callable(obj):
|
if not callable(obj):
|
||||||
continue
|
continue
|
||||||
# funcarg factories either have a pytest_funcarg__ prefix
|
# resource factories either have a pytest_funcarg__ prefix
|
||||||
# or are "funcarg" marked
|
# or are "funcarg" marked
|
||||||
if not callable(obj):
|
if not callable(obj):
|
||||||
continue
|
continue
|
||||||
marker = getattr(obj, "funcarg", None)
|
marker = getattr(obj, "factory", None)
|
||||||
if marker is not None and isinstance(marker, MarkInfo):
|
if marker is not None and isinstance(marker, MarkInfo):
|
||||||
assert not name.startswith(self._argprefix)
|
assert not name.startswith(self._argprefix)
|
||||||
argname = name
|
argname = name
|
||||||
scope = marker.kwargs.get("scope")
|
scope = marker.kwargs.get("scope")
|
||||||
params = marker.kwargs.get("params")
|
params = marker.kwargs.get("params")
|
||||||
|
new = True
|
||||||
elif name.startswith(self._argprefix):
|
elif name.startswith(self._argprefix):
|
||||||
argname = name[len(self._argprefix):]
|
argname = name[len(self._argprefix):]
|
||||||
scope = None
|
scope = None
|
||||||
params = None
|
params = None
|
||||||
|
new = False
|
||||||
else:
|
else:
|
||||||
# no funcargs. check if we have a setup function.
|
# no funcargs. check if we have a setup function.
|
||||||
setup = getattr(obj, "setup", None)
|
setup = getattr(obj, "setup", None)
|
||||||
|
@ -1260,7 +1272,8 @@ class FuncargManager:
|
||||||
self.setuplist.append(sf)
|
self.setuplist.append(sf)
|
||||||
continue
|
continue
|
||||||
faclist = self.arg2facspec.setdefault(argname, [])
|
faclist = self.arg2facspec.setdefault(argname, [])
|
||||||
factorydef = FactoryDef(self, nodeid, argname, obj, scope, params)
|
factorydef = FactoryDef(self, nodeid, argname, obj, scope, params,
|
||||||
|
new)
|
||||||
faclist.append(factorydef)
|
faclist.append(factorydef)
|
||||||
### check scope/params mismatch?
|
### check scope/params mismatch?
|
||||||
|
|
||||||
|
@ -1307,13 +1320,16 @@ class FuncargManager:
|
||||||
for setupcall in setuplist:
|
for setupcall in setuplist:
|
||||||
if setupcall.active:
|
if setupcall.active:
|
||||||
continue
|
continue
|
||||||
setuprequest = SetupRequest(request, setupcall)
|
testcontext = TestContextSetup(request, setupcall)
|
||||||
kwargs = {}
|
kwargs = {}
|
||||||
for name in setupcall.funcargnames:
|
for name in setupcall.funcargnames:
|
||||||
if name == "request":
|
try:
|
||||||
kwargs[name] = setuprequest
|
|
||||||
else:
|
|
||||||
kwargs[name] = request.getfuncargvalue(name)
|
kwargs[name] = request.getfuncargvalue(name)
|
||||||
|
except FuncargLookupError:
|
||||||
|
if name == "testcontext":
|
||||||
|
kwargs[name] = testcontext
|
||||||
|
else:
|
||||||
|
raise
|
||||||
scope = setupcall.scope or "function"
|
scope = setupcall.scope or "function"
|
||||||
scol = setupcall.scopeitem = request._getscopeitem(scope)
|
scol = setupcall.scopeitem = request._getscopeitem(scope)
|
||||||
self.session._setupstate.addfinalizer(setupcall.finish, scol)
|
self.session._setupstate.addfinalizer(setupcall.finish, scol)
|
||||||
|
@ -1332,30 +1348,67 @@ class FuncargManager:
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
scope2props = dict(session=())
|
||||||
|
scope2props["module"] = ("fspath", "module")
|
||||||
|
scope2props["class"] = scope2props["module"] + ("cls",)
|
||||||
|
scope2props["function"] = scope2props["class"] + ("function", "keywords")
|
||||||
|
|
||||||
|
def scopeprop(attr, name=None, doc=None):
|
||||||
|
if doc is None:
|
||||||
|
doc = "%s of underlying test context" % (attr,)
|
||||||
|
name = name or attr
|
||||||
|
def get(self):
|
||||||
|
if name in scope2props[self.scope]:
|
||||||
|
return getattr(self._request, name)
|
||||||
|
raise AttributeError("%s not available in %s-scoped context" % (
|
||||||
|
name, self.scope))
|
||||||
|
return property(get, doc=doc)
|
||||||
|
|
||||||
def rprop(attr, doc=None):
|
def rprop(attr, doc=None):
|
||||||
if doc is None:
|
if doc is None:
|
||||||
doc = "%r of underlying test item"
|
doc = "%s of underlying test context" % attr
|
||||||
return property(lambda x: getattr(x._request, attr), doc=doc)
|
return property(lambda x: getattr(x._request, attr), doc=doc)
|
||||||
|
|
||||||
class SetupRequest:
|
class TestContext(object):
|
||||||
def __init__(self, request, setupcall):
|
def __init__(self, request, scope):
|
||||||
self._request = request
|
self._request = request
|
||||||
self._setupcall = setupcall
|
self.scope = scope
|
||||||
self._finalizers = []
|
|
||||||
|
|
||||||
# no getfuncargvalue(), cached_setup, applymarker helpers here
|
# no getfuncargvalue(), cached_setup, applymarker helpers here
|
||||||
# on purpose
|
# on purpose
|
||||||
|
|
||||||
function = rprop("function")
|
|
||||||
cls = rprop("cls")
|
|
||||||
instance = rprop("instance")
|
|
||||||
fspath = rprop("fspath")
|
|
||||||
keywords = rprop("keywords")
|
|
||||||
config = rprop("config", "pytest config object.")
|
config = rprop("config", "pytest config object.")
|
||||||
|
session = rprop("session", "pytest session object.")
|
||||||
|
param = rprop("param")
|
||||||
|
|
||||||
|
function = scopeprop("function")
|
||||||
|
module = scopeprop("module")
|
||||||
|
cls = scopeprop("class", "cls")
|
||||||
|
instance = scopeprop("instance")
|
||||||
|
fspath = scopeprop("fspath")
|
||||||
|
#keywords = scopeprop("keywords")
|
||||||
|
|
||||||
|
class TestContextSetup(TestContext):
|
||||||
|
def __init__(self, request, setupcall):
|
||||||
|
self._setupcall = setupcall
|
||||||
|
self._finalizers = []
|
||||||
|
super(TestContextSetup, self).__init__(request, setupcall.scope)
|
||||||
|
|
||||||
def addfinalizer(self, finalizer):
|
def addfinalizer(self, finalizer):
|
||||||
|
""" Add a finalizer to be called after the last test in the
|
||||||
|
test context executes. """
|
||||||
self._setupcall.addfinalizer(finalizer)
|
self._setupcall.addfinalizer(finalizer)
|
||||||
|
|
||||||
|
class TestContextResource(TestContext):
|
||||||
|
def __init__(self, request):
|
||||||
|
super(TestContextResource, self).__init__(request, request.scope)
|
||||||
|
|
||||||
|
def addfinalizer(self, finalizer):
|
||||||
|
""" Add a finalizer to be called after the last test in the
|
||||||
|
test context executes. """
|
||||||
|
self._request.addfinalizer(finalizer)
|
||||||
|
|
||||||
|
|
||||||
class SetupCall:
|
class SetupCall:
|
||||||
""" a container/helper for managing calls to setup functions. """
|
""" a container/helper for managing calls to setup functions. """
|
||||||
def __init__(self, funcargmanager, baseid, func, scope):
|
def __init__(self, funcargmanager, baseid, func, scope):
|
||||||
|
@ -1386,16 +1439,17 @@ class SetupCall:
|
||||||
|
|
||||||
class FactoryDef:
|
class FactoryDef:
|
||||||
""" A container for a factory definition. """
|
""" A container for a factory definition. """
|
||||||
def __init__(self, funcargmanager, baseid, argname, func, scope, params):
|
def __init__(self, funcargmanager, baseid, argname, func, scope, params,
|
||||||
|
new):
|
||||||
self.funcargmanager = funcargmanager
|
self.funcargmanager = funcargmanager
|
||||||
self.baseid = baseid
|
self.baseid = baseid
|
||||||
self.func = func
|
self.func = func
|
||||||
self.argname = argname
|
self.argname = argname
|
||||||
self.scope = scope
|
self.scope = scope
|
||||||
self.params = params
|
self.params = params
|
||||||
|
self.new = new
|
||||||
self.funcargnames = getfuncargnames(func)
|
self.funcargnames = getfuncargnames(func)
|
||||||
|
|
||||||
|
|
||||||
def getfuncargnames(function, startindex=None):
|
def getfuncargnames(function, startindex=None):
|
||||||
# XXX merge with main.py's varnames
|
# XXX merge with main.py's varnames
|
||||||
argnames = py.std.inspect.getargs(py.code.getrawcode(function))[0]
|
argnames = py.std.inspect.getargs(py.code.getrawcode(function))[0]
|
||||||
|
|
|
@ -1595,12 +1595,12 @@ class TestFuncargFactory:
|
||||||
def test_receives_funcargs(self, testdir):
|
def test_receives_funcargs(self, testdir):
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
import pytest
|
import pytest
|
||||||
@pytest.mark.funcarg
|
@pytest.mark.factory
|
||||||
def arg1(request):
|
def arg1():
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
@pytest.mark.funcarg
|
@pytest.mark.factory
|
||||||
def arg2(request, arg1):
|
def arg2(arg1):
|
||||||
return arg1 + 1
|
return arg1 + 1
|
||||||
|
|
||||||
def test_add(arg2):
|
def test_add(arg2):
|
||||||
|
@ -1615,12 +1615,12 @@ class TestFuncargFactory:
|
||||||
def test_receives_funcargs_scope_mismatch(self, testdir):
|
def test_receives_funcargs_scope_mismatch(self, testdir):
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
import pytest
|
import pytest
|
||||||
@pytest.mark.funcarg(scope="function")
|
@pytest.mark.factory(scope="function")
|
||||||
def arg1(request):
|
def arg1():
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
@pytest.mark.funcarg(scope="module")
|
@pytest.mark.factory(scope="module")
|
||||||
def arg2(request, arg1):
|
def arg2(arg1):
|
||||||
return arg1 + 1
|
return arg1 + 1
|
||||||
|
|
||||||
def test_add(arg2):
|
def test_add(arg2):
|
||||||
|
@ -1638,13 +1638,13 @@ class TestFuncargFactory:
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
import pytest
|
import pytest
|
||||||
l = []
|
l = []
|
||||||
@pytest.mark.funcarg(params=[1,2])
|
@pytest.mark.factory(params=[1,2])
|
||||||
def arg1(request):
|
def arg1(testcontext):
|
||||||
l.append(1)
|
l.append(1)
|
||||||
return request.param
|
return testcontext.param
|
||||||
|
|
||||||
@pytest.mark.funcarg
|
@pytest.mark.factory
|
||||||
def arg2(request, arg1):
|
def arg2(arg1):
|
||||||
return arg1 + 1
|
return arg1 + 1
|
||||||
|
|
||||||
def test_add(arg1, arg2):
|
def test_add(arg1, arg2):
|
||||||
|
@ -1675,7 +1675,6 @@ class TestResourceIntegrationFunctional:
|
||||||
"*test_function*advanced*FAILED",
|
"*test_function*advanced*FAILED",
|
||||||
])
|
])
|
||||||
|
|
||||||
### XXX shift to test_session.py
|
|
||||||
class TestFuncargManager:
|
class TestFuncargManager:
|
||||||
def pytest_funcarg__testdir(self, request):
|
def pytest_funcarg__testdir(self, request):
|
||||||
testdir = request.getfuncargvalue("testdir")
|
testdir = request.getfuncargvalue("testdir")
|
||||||
|
@ -1727,14 +1726,14 @@ class TestSetupDiscovery:
|
||||||
testdir.makeconftest("""
|
testdir.makeconftest("""
|
||||||
import pytest
|
import pytest
|
||||||
@pytest.mark.setup
|
@pytest.mark.setup
|
||||||
def perfunction(request, tmpdir):
|
def perfunction(testcontext, tmpdir):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@pytest.mark.funcarg
|
@pytest.mark.factory
|
||||||
def arg1(request, tmpdir):
|
def arg1(tmpdir):
|
||||||
pass
|
pass
|
||||||
@pytest.mark.setup
|
@pytest.mark.setup
|
||||||
def perfunction2(request, arg1):
|
def perfunction2(arg1):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def pytest_funcarg__fm(request):
|
def pytest_funcarg__fm(request):
|
||||||
|
@ -1751,10 +1750,10 @@ class TestSetupDiscovery:
|
||||||
setupcalls, allnames = fm.getsetuplist(item.nodeid)
|
setupcalls, allnames = fm.getsetuplist(item.nodeid)
|
||||||
assert len(setupcalls) == 2
|
assert len(setupcalls) == 2
|
||||||
assert setupcalls[0].func.__name__ == "perfunction"
|
assert setupcalls[0].func.__name__ == "perfunction"
|
||||||
assert "request" in setupcalls[0].funcargnames
|
assert "testcontext" in setupcalls[0].funcargnames
|
||||||
assert "tmpdir" in setupcalls[0].funcargnames
|
assert "tmpdir" in setupcalls[0].funcargnames
|
||||||
assert setupcalls[1].func.__name__ == "perfunction2"
|
assert setupcalls[1].func.__name__ == "perfunction2"
|
||||||
assert "request" in setupcalls[1].funcargnames
|
assert "testcontext" not in setupcalls[1].funcargnames
|
||||||
assert "arg1" in setupcalls[1].funcargnames
|
assert "arg1" in setupcalls[1].funcargnames
|
||||||
assert "tmpdir" not in setupcalls[1].funcargnames
|
assert "tmpdir" not in setupcalls[1].funcargnames
|
||||||
#assert "tmpdir" in setupcalls[1].depfuncargs
|
#assert "tmpdir" in setupcalls[1].depfuncargs
|
||||||
|
@ -1768,12 +1767,12 @@ class TestSetupManagement:
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
import pytest
|
import pytest
|
||||||
l = []
|
l = []
|
||||||
@pytest.mark.funcarg(scope="module")
|
@pytest.mark.factory(scope="module")
|
||||||
def arg(request):
|
def arg():
|
||||||
l.append(1)
|
l.append(1)
|
||||||
return 0
|
return 0
|
||||||
@pytest.mark.setup(scope="class")
|
@pytest.mark.setup(scope="class")
|
||||||
def something(request, arg):
|
def something(arg):
|
||||||
l.append(2)
|
l.append(2)
|
||||||
|
|
||||||
def test_hello(arg):
|
def test_hello(arg):
|
||||||
|
@ -1793,12 +1792,12 @@ class TestSetupManagement:
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
import pytest
|
import pytest
|
||||||
l = []
|
l = []
|
||||||
@pytest.mark.funcarg(params=[1,2])
|
@pytest.mark.factory(params=[1,2])
|
||||||
def arg(request):
|
def arg(testcontext):
|
||||||
return request.param
|
return testcontext.param
|
||||||
|
|
||||||
@pytest.mark.setup
|
@pytest.mark.setup
|
||||||
def something(request, arg):
|
def something(arg):
|
||||||
l.append(arg)
|
l.append(arg)
|
||||||
|
|
||||||
def test_hello():
|
def test_hello():
|
||||||
|
@ -1819,13 +1818,13 @@ class TestSetupManagement:
|
||||||
|
|
||||||
l = []
|
l = []
|
||||||
|
|
||||||
@pytest.mark.funcarg(scope="session", params=[1,2])
|
@pytest.mark.factory(scope="session", params=[1,2])
|
||||||
def arg(request):
|
def arg(testcontext):
|
||||||
return request.param
|
return testcontext.param
|
||||||
|
|
||||||
@pytest.mark.setup(scope="function")
|
@pytest.mark.setup(scope="function")
|
||||||
def append(request, arg):
|
def append(testcontext, arg):
|
||||||
if request.function.__name__ == "test_some":
|
if testcontext.function.__name__ == "test_some":
|
||||||
l.append(arg)
|
l.append(arg)
|
||||||
|
|
||||||
def test_some():
|
def test_some():
|
||||||
|
@ -1845,19 +1844,19 @@ class TestSetupManagement:
|
||||||
|
|
||||||
l = []
|
l = []
|
||||||
|
|
||||||
@pytest.mark.funcarg(scope="function", params=[1,2])
|
@pytest.mark.factory(scope="function", params=[1,2])
|
||||||
def farg(request):
|
def farg(testcontext):
|
||||||
return request.param
|
return testcontext.param
|
||||||
|
|
||||||
@pytest.mark.funcarg(scope="class", params=list("ab"))
|
@pytest.mark.factory(scope="class", params=list("ab"))
|
||||||
def carg(request):
|
def carg(testcontext):
|
||||||
return request.param
|
return testcontext.param
|
||||||
|
|
||||||
@pytest.mark.setup(scope="class")
|
@pytest.mark.setup(scope="class")
|
||||||
def append(request, farg, carg):
|
def append(testcontext, farg, carg):
|
||||||
def fin():
|
def fin():
|
||||||
l.append("fin_%s%s" % (carg, farg))
|
l.append("fin_%s%s" % (carg, farg))
|
||||||
request.addfinalizer(fin)
|
testcontext.addfinalizer(fin)
|
||||||
""")
|
""")
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
import pytest
|
import pytest
|
||||||
|
@ -1879,9 +1878,9 @@ class TestFuncargMarker:
|
||||||
def test_parametrize(self, testdir):
|
def test_parametrize(self, testdir):
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
import pytest
|
import pytest
|
||||||
@pytest.mark.funcarg(params=["a", "b", "c"])
|
@pytest.mark.factory(params=["a", "b", "c"])
|
||||||
def arg(request):
|
def arg(testcontext):
|
||||||
return request.param
|
return testcontext.param
|
||||||
l = []
|
l = []
|
||||||
def test_param(arg):
|
def test_param(arg):
|
||||||
l.append(arg)
|
l.append(arg)
|
||||||
|
@ -1895,8 +1894,8 @@ class TestFuncargMarker:
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
import pytest
|
import pytest
|
||||||
l = []
|
l = []
|
||||||
@pytest.mark.funcarg(scope="module")
|
@pytest.mark.factory(scope="module")
|
||||||
def arg(request):
|
def arg():
|
||||||
l.append(1)
|
l.append(1)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
@ -1917,8 +1916,8 @@ class TestFuncargMarker:
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
import pytest
|
import pytest
|
||||||
l = []
|
l = []
|
||||||
@pytest.mark.funcarg(scope="module")
|
@pytest.mark.factory(scope="module")
|
||||||
def arg(request):
|
def arg():
|
||||||
l.append(1)
|
l.append(1)
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
@ -1940,11 +1939,11 @@ class TestFuncargMarker:
|
||||||
import pytest
|
import pytest
|
||||||
finalized = []
|
finalized = []
|
||||||
created = []
|
created = []
|
||||||
@pytest.mark.funcarg(scope="module")
|
@pytest.mark.factory(scope="module")
|
||||||
def arg(request):
|
def arg(testcontext):
|
||||||
created.append(1)
|
created.append(1)
|
||||||
assert request.scope == "module"
|
assert testcontext.scope == "module"
|
||||||
request.addfinalizer(lambda: finalized.append(1))
|
testcontext.addfinalizer(lambda: finalized.append(1))
|
||||||
def pytest_funcarg__created(request):
|
def pytest_funcarg__created(request):
|
||||||
return len(created)
|
return len(created)
|
||||||
def pytest_funcarg__finalized(request):
|
def pytest_funcarg__finalized(request):
|
||||||
|
@ -1979,15 +1978,15 @@ class TestFuncargMarker:
|
||||||
import pytest
|
import pytest
|
||||||
finalized = []
|
finalized = []
|
||||||
created = []
|
created = []
|
||||||
@pytest.mark.funcarg(scope="function")
|
@pytest.mark.factory(scope="function")
|
||||||
def arg(request):
|
def arg(testcontext):
|
||||||
pass
|
pass
|
||||||
""")
|
""")
|
||||||
testdir.makepyfile(
|
testdir.makepyfile(
|
||||||
test_mod1="""
|
test_mod1="""
|
||||||
import pytest
|
import pytest
|
||||||
@pytest.mark.funcarg(scope="session")
|
@pytest.mark.factory(scope="session")
|
||||||
def arg(request):
|
def arg(testcontext):
|
||||||
%s
|
%s
|
||||||
def test_1(arg):
|
def test_1(arg):
|
||||||
pass
|
pass
|
||||||
|
@ -2001,18 +2000,16 @@ class TestFuncargMarker:
|
||||||
def test_register_only_with_mark(self, testdir):
|
def test_register_only_with_mark(self, testdir):
|
||||||
testdir.makeconftest("""
|
testdir.makeconftest("""
|
||||||
import pytest
|
import pytest
|
||||||
finalized = []
|
@pytest.mark.factory
|
||||||
created = []
|
def arg():
|
||||||
@pytest.mark.funcarg
|
|
||||||
def arg(request):
|
|
||||||
return 1
|
return 1
|
||||||
""")
|
""")
|
||||||
testdir.makepyfile(
|
testdir.makepyfile(
|
||||||
test_mod1="""
|
test_mod1="""
|
||||||
import pytest
|
import pytest
|
||||||
@pytest.mark.funcarg
|
@pytest.mark.factory
|
||||||
def arg(request):
|
def arg(arg):
|
||||||
return request.getfuncargvalue("arg") + 1
|
return arg + 1
|
||||||
def test_1(arg):
|
def test_1(arg):
|
||||||
assert arg == 2
|
assert arg == 2
|
||||||
""")
|
""")
|
||||||
|
@ -2022,9 +2019,9 @@ class TestFuncargMarker:
|
||||||
def test_parametrize_and_scope(self, testdir):
|
def test_parametrize_and_scope(self, testdir):
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
import pytest
|
import pytest
|
||||||
@pytest.mark.funcarg(scope="module", params=["a", "b", "c"])
|
@pytest.mark.factory(scope="module", params=["a", "b", "c"])
|
||||||
def arg(request):
|
def arg(testcontext):
|
||||||
return request.param
|
return testcontext.param
|
||||||
l = []
|
l = []
|
||||||
def test_param(arg):
|
def test_param(arg):
|
||||||
l.append(arg)
|
l.append(arg)
|
||||||
|
@ -2040,14 +2037,14 @@ class TestFuncargMarker:
|
||||||
def test_scope_mismatch(self, testdir):
|
def test_scope_mismatch(self, testdir):
|
||||||
testdir.makeconftest("""
|
testdir.makeconftest("""
|
||||||
import pytest
|
import pytest
|
||||||
@pytest.mark.funcarg(scope="function")
|
@pytest.mark.factory(scope="function")
|
||||||
def arg(request):
|
def arg(testcontext):
|
||||||
pass
|
pass
|
||||||
""")
|
""")
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
import pytest
|
import pytest
|
||||||
@pytest.mark.funcarg(scope="session")
|
@pytest.mark.factory(scope="session")
|
||||||
def arg(request, arg):
|
def arg(arg):
|
||||||
pass
|
pass
|
||||||
def test_mismatch(arg):
|
def test_mismatch(arg):
|
||||||
pass
|
pass
|
||||||
|
@ -2062,9 +2059,9 @@ class TestFuncargMarker:
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@pytest.mark.funcarg(scope="module", params=[1, 2])
|
@pytest.mark.factory(scope="module", params=[1, 2])
|
||||||
def arg(request):
|
def arg(testcontext):
|
||||||
return request.param
|
return testcontext.param
|
||||||
|
|
||||||
l = []
|
l = []
|
||||||
def test_1(arg):
|
def test_1(arg):
|
||||||
|
@ -2081,11 +2078,11 @@ class TestFuncargMarker:
|
||||||
testdir.makeconftest("""
|
testdir.makeconftest("""
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@pytest.mark.funcarg(scope="session", params="s1 s2".split())
|
@pytest.mark.factory(scope="session", params="s1 s2".split())
|
||||||
def sarg(request):
|
def sarg():
|
||||||
pass
|
pass
|
||||||
@pytest.mark.funcarg(scope="module", params="m1 m2".split())
|
@pytest.mark.factory(scope="module", params="m1 m2".split())
|
||||||
def marg(request):
|
def marg():
|
||||||
pass
|
pass
|
||||||
""")
|
""")
|
||||||
testdir.makepyfile(test_mod1="""
|
testdir.makepyfile(test_mod1="""
|
||||||
|
@ -2129,19 +2126,19 @@ class TestFuncargMarker:
|
||||||
|
|
||||||
l = []
|
l = []
|
||||||
|
|
||||||
@pytest.mark.funcarg(scope="function", params=[1,2])
|
@pytest.mark.factory(scope="function", params=[1,2])
|
||||||
def farg(request):
|
def farg(testcontext):
|
||||||
return request.param
|
return testcontext.param
|
||||||
|
|
||||||
@pytest.mark.funcarg(scope="class", params=list("ab"))
|
@pytest.mark.factory(scope="class", params=list("ab"))
|
||||||
def carg(request):
|
def carg(testcontext):
|
||||||
return request.param
|
return testcontext.param
|
||||||
|
|
||||||
@pytest.mark.setup(scope="class")
|
@pytest.mark.setup(scope="class")
|
||||||
def append(request, farg, carg):
|
def append(testcontext, farg, carg):
|
||||||
def fin():
|
def fin():
|
||||||
l.append("fin_%s%s" % (carg, farg))
|
l.append("fin_%s%s" % (carg, farg))
|
||||||
request.addfinalizer(fin)
|
testcontext.addfinalizer(fin)
|
||||||
""")
|
""")
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
import pytest
|
import pytest
|
||||||
|
@ -2175,19 +2172,19 @@ class TestFuncargMarker:
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@pytest.mark.funcarg(scope="function", params=[1, 2])
|
@pytest.mark.factory(scope="function", params=[1, 2])
|
||||||
def arg(request):
|
def arg(testcontext):
|
||||||
param = request.param
|
param = testcontext.param
|
||||||
request.addfinalizer(lambda: l.append("fin:%s" % param))
|
testcontext.addfinalizer(lambda: l.append("fin:%s" % param))
|
||||||
l.append("create:%s" % param)
|
l.append("create:%s" % param)
|
||||||
return request.param
|
return testcontext.param
|
||||||
|
|
||||||
@pytest.mark.funcarg(scope="module", params=["mod1", "mod2"])
|
@pytest.mark.factory(scope="module", params=["mod1", "mod2"])
|
||||||
def modarg(request):
|
def modarg(testcontext):
|
||||||
param = request.param
|
param = testcontext.param
|
||||||
request.addfinalizer(lambda: l.append("fin:%s" % param))
|
testcontext.addfinalizer(lambda: l.append("fin:%s" % param))
|
||||||
l.append("create:%s" % param)
|
l.append("create:%s" % param)
|
||||||
return request.param
|
return testcontext.param
|
||||||
|
|
||||||
l = []
|
l = []
|
||||||
def test_1(arg):
|
def test_1(arg):
|
||||||
|
@ -2219,12 +2216,12 @@ class TestFuncargMarker:
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@pytest.mark.funcarg(scope="module", params=[1, 2])
|
@pytest.mark.factory(scope="module", params=[1, 2])
|
||||||
def arg(request):
|
def arg(testcontext):
|
||||||
request.config.l = l # to access from outer
|
testcontext.config.l = l # to access from outer
|
||||||
x = request.param
|
x = testcontext.param
|
||||||
request.addfinalizer(lambda: l.append("fin%s" % x))
|
testcontext.addfinalizer(lambda: l.append("fin%s" % x))
|
||||||
return request.param
|
return testcontext.param
|
||||||
|
|
||||||
l = []
|
l = []
|
||||||
def test_1(arg):
|
def test_1(arg):
|
||||||
|
@ -2248,11 +2245,11 @@ class TestFuncargMarker:
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@pytest.mark.funcarg(scope="function", params=[1, 2])
|
@pytest.mark.factory(scope="function", params=[1, 2])
|
||||||
def arg(request):
|
def arg(testcontext):
|
||||||
x = request.param
|
x = testcontext.param
|
||||||
request.addfinalizer(lambda: l.append("fin%s" % x))
|
testcontext.addfinalizer(lambda: l.append("fin%s" % x))
|
||||||
return request.param
|
return testcontext.param
|
||||||
|
|
||||||
l = []
|
l = []
|
||||||
def test_1(arg):
|
def test_1(arg):
|
||||||
|
@ -2270,13 +2267,13 @@ class TestFuncargMarker:
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@pytest.mark.funcarg(scope="module", params=[1, 2])
|
@pytest.mark.factory(scope="module", params=[1, 2])
|
||||||
def arg(request):
|
def arg(testcontext):
|
||||||
return request.param
|
return testcontext.param
|
||||||
|
|
||||||
@pytest.mark.setup(scope="module")
|
@pytest.mark.setup(scope="module")
|
||||||
def mysetup(request, arg):
|
def mysetup(testcontext, arg):
|
||||||
request.addfinalizer(lambda: l.append("fin%s" % arg))
|
testcontext.addfinalizer(lambda: l.append("fin%s" % arg))
|
||||||
l.append("setup%s" % arg)
|
l.append("setup%s" % arg)
|
||||||
|
|
||||||
l = []
|
l = []
|
||||||
|
@ -2297,3 +2294,76 @@ class TestFuncargMarker:
|
||||||
reprec = testdir.inline_run("-v")
|
reprec = testdir.inline_run("-v")
|
||||||
reprec.assertoutcome(passed=6)
|
reprec.assertoutcome(passed=6)
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(("scope", "ok", "error"),[
|
||||||
|
["session", "", "fspath class function module"],
|
||||||
|
["module", "module fspath", "cls function"],
|
||||||
|
["class", "module fspath cls", "function"],
|
||||||
|
["function", "module fspath cls function", ""]
|
||||||
|
])
|
||||||
|
class TestTestContextScopeAccess:
|
||||||
|
def test_setup(self, testdir, scope, ok, error):
|
||||||
|
testdir.makepyfile("""
|
||||||
|
import pytest
|
||||||
|
@pytest.mark.setup(scope=%r)
|
||||||
|
def myscoped(testcontext):
|
||||||
|
for x in %r:
|
||||||
|
assert hasattr(testcontext, x)
|
||||||
|
for x in %r:
|
||||||
|
pytest.raises(AttributeError, lambda:
|
||||||
|
getattr(testcontext, x))
|
||||||
|
assert testcontext.session
|
||||||
|
assert testcontext.config
|
||||||
|
def test_func():
|
||||||
|
pass
|
||||||
|
""" %(scope, ok.split(), error.split()))
|
||||||
|
reprec = testdir.inline_run()
|
||||||
|
reprec.assertoutcome(passed=1)
|
||||||
|
|
||||||
|
def test_resource(self, testdir, scope, ok, error):
|
||||||
|
testdir.makepyfile("""
|
||||||
|
import pytest
|
||||||
|
@pytest.mark.factory(scope=%r)
|
||||||
|
def arg(testcontext):
|
||||||
|
for x in %r:
|
||||||
|
assert hasattr(testcontext, x)
|
||||||
|
for x in %r:
|
||||||
|
pytest.raises(AttributeError, lambda:
|
||||||
|
getattr(testcontext, x))
|
||||||
|
assert testcontext.session
|
||||||
|
assert testcontext.config
|
||||||
|
def test_func(arg):
|
||||||
|
pass
|
||||||
|
""" %(scope, ok.split(), error.split()))
|
||||||
|
reprec = testdir.inline_run()
|
||||||
|
reprec.assertoutcome(passed=1)
|
||||||
|
|
||||||
|
class TestTestContextVarious:
|
||||||
|
def test_newstyle_no_request(self, testdir):
|
||||||
|
testdir.makepyfile("""
|
||||||
|
import pytest
|
||||||
|
@pytest.mark.factory
|
||||||
|
def arg(request):
|
||||||
|
pass
|
||||||
|
def test_1(arg):
|
||||||
|
pass
|
||||||
|
""")
|
||||||
|
result = testdir.runpytest()
|
||||||
|
result.stdout.fnmatch_lines([
|
||||||
|
"*no factory found*request*",
|
||||||
|
])
|
||||||
|
|
||||||
|
def test_setupcontext_no_param(self, testdir):
|
||||||
|
testdir.makepyfile("""
|
||||||
|
import pytest
|
||||||
|
@pytest.mark.factory(params=[1,2])
|
||||||
|
def arg(testcontext):
|
||||||
|
return testcontext.param
|
||||||
|
|
||||||
|
@pytest.mark.setup
|
||||||
|
def mysetup(testcontext, arg):
|
||||||
|
assert not hasattr(testcontext, "param")
|
||||||
|
def test_1(arg):
|
||||||
|
assert arg in (1,2)
|
||||||
|
""")
|
||||||
|
reprec = testdir.inline_run()
|
||||||
|
reprec.assertoutcome(passed=2)
|
||||||
|
|
Loading…
Reference in New Issue