funcargs work mostly according to the documentation
--HG-- branch : trunk
This commit is contained in:
parent
f0a277008a
commit
763d0d72a5
|
@ -5,10 +5,10 @@ rsyncignore = ['c-extension/greenlet/build']
|
|||
|
||||
import py
|
||||
class PylibTestconfigPlugin:
|
||||
def pytest_funcarg__specssh(self, pyfuncitem):
|
||||
return getspecssh(pyfuncitem.config)
|
||||
def pytest_funcarg__specsocket(self, pyfuncitem):
|
||||
return getsocketspec(pyfuncitem.config)
|
||||
def pytest_funcarg__specssh(self, request):
|
||||
return getspecssh(request.config)
|
||||
def pytest_funcarg__specsocket(self, request):
|
||||
return getsocketspec(request.config)
|
||||
|
||||
def pytest_addoption(self, parser):
|
||||
group = parser.addgroup("pylib", "py lib testing options")
|
||||
|
|
|
@ -1,28 +1,25 @@
|
|||
|
||||
""" RSync filter test
|
||||
"""
|
||||
|
||||
import py
|
||||
from py.__.test.dist.nodemanage import NodeManager
|
||||
|
||||
def pytest_funcarg__source(pyfuncitem):
|
||||
return py.test.ensuretemp(pyfuncitem.getmodpath()).mkdir("source")
|
||||
def pytest_funcarg__dest(pyfuncitem):
|
||||
dest = py.test.ensuretemp(pyfuncitem.getmodpath()).mkdir("dest")
|
||||
return dest
|
||||
class pytest_funcarg__mysetup:
|
||||
def __init__(self, request):
|
||||
basetemp = request.config.ensuretemp(request.getfspath().purebasename)
|
||||
basetemp = basetemp.mkdir(request.funcname)
|
||||
self.source = basetemp.mkdir("source")
|
||||
self.dest = basetemp.mkdir("dest")
|
||||
|
||||
class TestNodeManager:
|
||||
@py.test.mark.xfail("consider / forbid implicit rsyncdirs?")
|
||||
def test_rsync_roots_no_roots(self, source, dest):
|
||||
source.ensure("dir1", "file1").write("hello")
|
||||
def test_rsync_roots_no_roots(self, mysetup):
|
||||
mysetup.source.ensure("dir1", "file1").write("hello")
|
||||
config = py.test.config._reparse([source])
|
||||
nodemanager = NodeManager(config, ["popen//chdir=%s" % dest])
|
||||
nodemanager = NodeManager(config, ["popen//chdir=%s" % mysetup.dest])
|
||||
assert nodemanager.config.topdir == source == config.topdir
|
||||
nodemanager.rsync_roots()
|
||||
p, = nodemanager.gwmanager.multi_exec("import os ; channel.send(os.getcwd())").receive_each()
|
||||
p = py.path.local(p)
|
||||
print "remote curdir", p
|
||||
assert p == dest.join(config.topdir.basename)
|
||||
assert p == mysetup.dest.join(config.topdir.basename)
|
||||
assert p.join("dir1").check()
|
||||
assert p.join("dir1", "file1").check()
|
||||
|
||||
|
@ -33,8 +30,9 @@ class TestNodeManager:
|
|||
nodemanager.setup_nodes([].append)
|
||||
nodemanager.wait_nodesready(timeout=2.0)
|
||||
|
||||
def test_popen_rsync_subdir(self, testdir, source, dest):
|
||||
dir1 = source.mkdir("dir1")
|
||||
def test_popen_rsync_subdir(self, testdir, mysetup):
|
||||
source, dest = mysetup.source, mysetup.dest
|
||||
dir1 = mysetup.source.mkdir("dir1")
|
||||
dir2 = dir1.mkdir("dir2")
|
||||
dir2.ensure("hello")
|
||||
for rsyncroot in (dir1, source):
|
||||
|
@ -53,7 +51,8 @@ class TestNodeManager:
|
|||
assert dest.join("dir1", "dir2", 'hello').check()
|
||||
nodemanager.gwmanager.exit()
|
||||
|
||||
def test_init_rsync_roots(self, source, dest):
|
||||
def test_init_rsync_roots(self, mysetup):
|
||||
source, dest = mysetup.source, mysetup.dest
|
||||
dir2 = source.ensure("dir1", "dir2", dir=1)
|
||||
source.ensure("dir1", "somefile", dir=1)
|
||||
dir2.ensure("hello")
|
||||
|
@ -68,7 +67,8 @@ class TestNodeManager:
|
|||
assert not dest.join("dir1").check()
|
||||
assert not dest.join("bogus").check()
|
||||
|
||||
def test_rsyncignore(self, source, dest):
|
||||
def test_rsyncignore(self, mysetup):
|
||||
source, dest = mysetup.source, mysetup.dest
|
||||
dir2 = source.ensure("dir1", "dir2", dir=1)
|
||||
dir5 = source.ensure("dir5", "dir6", "bogus")
|
||||
dirf = source.ensure("dir5", "file")
|
||||
|
@ -86,7 +86,8 @@ class TestNodeManager:
|
|||
assert dest.join("dir5","file").check()
|
||||
assert not dest.join("dir6").check()
|
||||
|
||||
def test_optimise_popen(self, source, dest):
|
||||
def test_optimise_popen(self, mysetup):
|
||||
source, dest = mysetup.source, mysetup.dest
|
||||
specs = ["popen"] * 3
|
||||
source.join("conftest.py").write("rsyncdirs = ['a']")
|
||||
source.ensure('a', dir=1)
|
||||
|
@ -97,7 +98,8 @@ class TestNodeManager:
|
|||
assert gwspec._samefilesystem()
|
||||
assert not gwspec.chdir
|
||||
|
||||
def test_setup_DEBUG(self, source, testdir):
|
||||
def test_setup_DEBUG(self, mysetup, testdir):
|
||||
source = mysetup.source
|
||||
specs = ["popen"] * 2
|
||||
source.join("conftest.py").write("rsyncdirs = ['a']")
|
||||
source.ensure('a', dir=1)
|
||||
|
|
|
@ -31,8 +31,8 @@ class EventQueue:
|
|||
print str(kwargs["excrepr"])
|
||||
|
||||
class MySetup:
|
||||
def __init__(self, pyfuncitem):
|
||||
self.pyfuncitem = pyfuncitem
|
||||
def __init__(self, request):
|
||||
self.request = request
|
||||
|
||||
def geteventargs(self, eventname, timeout=2.0):
|
||||
eq = EventQueue(self.config.pluginmanager, self.queue)
|
||||
|
@ -55,17 +55,11 @@ class MySetup:
|
|||
print "exiting:", gw
|
||||
gw.exit()
|
||||
|
||||
def pytest_funcarg__mysetup(pyfuncitem):
|
||||
mysetup = MySetup(pyfuncitem)
|
||||
def pytest_funcarg__mysetup(request):
|
||||
mysetup = MySetup(request)
|
||||
#pyfuncitem.addfinalizer(mysetup.finalize)
|
||||
return mysetup
|
||||
|
||||
def pytest_funcarg__testdir(__call__, pyfuncitem):
|
||||
# decorate to make us always change to testdir
|
||||
testdir = __call__.execute(firstresult=True)
|
||||
testdir.chdir()
|
||||
return testdir
|
||||
|
||||
def test_node_hash_equality(mysetup):
|
||||
node = mysetup.makenode()
|
||||
node2 = mysetup.makenode()
|
||||
|
|
|
@ -1,19 +1,19 @@
|
|||
import py
|
||||
|
||||
class _pytestPlugin:
|
||||
def pytest_funcarg___pytest(self, pyfuncitem):
|
||||
return PytestArg(pyfuncitem)
|
||||
def pytest_funcarg___pytest(self, request):
|
||||
return PytestArg(request)
|
||||
|
||||
class PytestArg:
|
||||
def __init__(self, pyfuncitem):
|
||||
self.pyfuncitem = pyfuncitem
|
||||
def __init__(self, request):
|
||||
self.request = request
|
||||
|
||||
def getcallrecorder(self, apiclass, comregistry=None):
|
||||
if comregistry is None:
|
||||
comregistry = self.pyfuncitem.config.pluginmanager.comregistry
|
||||
comregistry = self.request.config.pluginmanager.comregistry
|
||||
callrecorder = CallRecorder(comregistry)
|
||||
callrecorder.start_recording(apiclass)
|
||||
self.pyfuncitem.addfinalizer(callrecorder.finalize)
|
||||
self.request.addfinalizer(callrecorder.finalize)
|
||||
return callrecorder
|
||||
|
||||
|
||||
|
|
|
@ -22,7 +22,6 @@ class DefaultPlugin:
|
|||
rep = runner.ItemTestReport(item, excinfo, "execute", outerr)
|
||||
item.config.api.pytest_itemtestreport(rep=rep)
|
||||
|
||||
# XXX make this access pyfuncitem.args or funcargs
|
||||
def pytest_pyfunc_call(self, pyfuncitem, args, kwargs):
|
||||
pyfuncitem.obj(*args, **kwargs)
|
||||
|
||||
|
|
|
@ -2,14 +2,14 @@ import py
|
|||
|
||||
class IocapturePlugin:
|
||||
""" capture sys.stdout/sys.stderr / fd1/fd2. """
|
||||
def pytest_funcarg__stdcapture(self, pyfuncitem):
|
||||
def pytest_funcarg__stdcapture(self, request):
|
||||
capture = Capture(py.io.StdCapture)
|
||||
pyfuncitem.addfinalizer(capture.finalize)
|
||||
request.addfinalizer(capture.finalize)
|
||||
return capture
|
||||
|
||||
def pytest_funcarg__stdcapturefd(self, pyfuncitem):
|
||||
def pytest_funcarg__stdcapturefd(self, request):
|
||||
capture = Capture(py.io.StdCaptureFD)
|
||||
pyfuncitem.addfinalizer(capture.finalize)
|
||||
request.addfinalizer(capture.finalize)
|
||||
return capture
|
||||
|
||||
class Capture:
|
||||
|
|
|
@ -2,9 +2,9 @@ import os
|
|||
|
||||
class MonkeypatchPlugin:
|
||||
""" setattr-monkeypatching with automatical reversal after test. """
|
||||
def pytest_funcarg__monkeypatch(self, pyfuncitem):
|
||||
def pytest_funcarg__monkeypatch(self, request):
|
||||
monkeypatch = MonkeyPatch()
|
||||
pyfuncitem.addfinalizer(monkeypatch.finalize)
|
||||
request.addfinalizer(monkeypatch.finalize)
|
||||
return monkeypatch
|
||||
|
||||
notset = object()
|
||||
|
|
|
@ -6,37 +6,23 @@ from py.__.test.plugin import api
|
|||
|
||||
class PlugintesterPlugin:
|
||||
""" test support code for testing pytest plugins. """
|
||||
def pytest_funcarg__plugintester(self, pyfuncitem):
|
||||
pt = PluginTester(pyfuncitem)
|
||||
pyfuncitem.addfinalizer(pt.finalize)
|
||||
return pt
|
||||
def pytest_funcarg__plugintester(self, request):
|
||||
return PluginTester(request)
|
||||
|
||||
class Support(object):
|
||||
def __init__(self, pyfuncitem):
|
||||
""" instantiated per function that requests it. """
|
||||
self.pyfuncitem = pyfuncitem
|
||||
class PluginTester:
|
||||
def __init__(self, request):
|
||||
self.request = request
|
||||
|
||||
def getmoditem(self):
|
||||
for colitem in self.pyfuncitem.listchain():
|
||||
if isinstance(colitem, colitem.Module):
|
||||
return colitem
|
||||
|
||||
def finalize(self):
|
||||
""" called after test function finished execution"""
|
||||
|
||||
class PluginTester(Support):
|
||||
def testdir(self):
|
||||
# XXX import differently, eg.
|
||||
# FSTester = self.pyfuncitem.config.pluginmanager.getpluginattr("pytester", "FSTester")
|
||||
from pytest_pytester import TmpTestdir
|
||||
crunner = TmpTestdir(self.pyfuncitem)
|
||||
self.pyfuncitem.addfinalizer(crunner.finalize)
|
||||
crunner = TmpTestdir(self.request)
|
||||
self.request.addfinalizer(crunner.finalize)
|
||||
#
|
||||
for colitem in self.pyfuncitem.listchain():
|
||||
if isinstance(colitem, py.test.collect.Module) and \
|
||||
colitem.name.startswith("pytest_"):
|
||||
crunner.plugins.append(colitem.fspath.purebasename)
|
||||
break
|
||||
#for colitem in self.request.listchain():
|
||||
# if isinstance(colitem, py.test.collect.Module) and \
|
||||
# colitem.name.startswith("pytest_"):
|
||||
# crunner.plugins.append(colitem.fspath.purebasename)
|
||||
# break
|
||||
return crunner
|
||||
|
||||
def apicheck(self, pluginclass):
|
||||
|
|
|
@ -11,19 +11,19 @@ import api
|
|||
|
||||
|
||||
class PytesterPlugin:
|
||||
def pytest_funcarg__linecomp(self, pyfuncitem):
|
||||
def pytest_funcarg__linecomp(self, request):
|
||||
return LineComp()
|
||||
|
||||
def pytest_funcarg__LineMatcher(self, pyfuncitem):
|
||||
def pytest_funcarg__LineMatcher(self, request):
|
||||
return LineMatcher
|
||||
|
||||
def pytest_funcarg__testdir(self, pyfuncitem):
|
||||
tmptestdir = TmpTestdir(pyfuncitem)
|
||||
def pytest_funcarg__testdir(self, request):
|
||||
tmptestdir = TmpTestdir(request)
|
||||
return tmptestdir
|
||||
|
||||
def pytest_funcarg__eventrecorder(self, pyfuncitem):
|
||||
def pytest_funcarg__eventrecorder(self, request):
|
||||
evrec = EventRecorder(py._com.comregistry)
|
||||
pyfuncitem.addfinalizer(lambda: evrec.comregistry.unregister(evrec))
|
||||
request.addfinalizer(lambda: evrec.comregistry.unregister(evrec))
|
||||
return evrec
|
||||
|
||||
def test_generic(plugintester):
|
||||
|
@ -38,11 +38,11 @@ class RunResult:
|
|||
self.stderr = LineMatcher(errlines)
|
||||
|
||||
class TmpTestdir:
|
||||
def __init__(self, pyfuncitem):
|
||||
self.pyfuncitem = pyfuncitem
|
||||
def __init__(self, request):
|
||||
self.request = request
|
||||
# XXX remove duplication with tmpdir plugin
|
||||
basetmp = pyfuncitem.config.ensuretemp("testdir")
|
||||
name = pyfuncitem.name
|
||||
basetmp = request.config.ensuretemp("testdir")
|
||||
name = request.funcname
|
||||
for i in range(100):
|
||||
try:
|
||||
tmpdir = basetmp.mkdir(name + str(i))
|
||||
|
@ -57,7 +57,7 @@ class TmpTestdir:
|
|||
self._syspathremove = []
|
||||
self.chdir() # always chdir
|
||||
assert hasattr(self, '_olddir')
|
||||
self.pyfuncitem.addfinalizer(self.finalize)
|
||||
self.request.addfinalizer(self.finalize)
|
||||
|
||||
def __repr__(self):
|
||||
return "<TmpTestdir %r>" % (self.tmpdir,)
|
||||
|
@ -78,7 +78,7 @@ class TmpTestdir:
|
|||
sorter.callrecorder = CallRecorder(registry)
|
||||
sorter.callrecorder.start_recording(api.PluginHooks)
|
||||
sorter.api = sorter.callrecorder.api
|
||||
self.pyfuncitem.addfinalizer(sorter.callrecorder.finalize)
|
||||
self.request.addfinalizer(sorter.callrecorder.finalize)
|
||||
return sorter
|
||||
|
||||
def chdir(self):
|
||||
|
@ -90,7 +90,7 @@ class TmpTestdir:
|
|||
items = kwargs.items()
|
||||
if args:
|
||||
source = "\n".join(map(str, args))
|
||||
basename = self.pyfuncitem.name
|
||||
basename = self.request.funcname
|
||||
items.insert(0, (basename, source))
|
||||
ret = None
|
||||
for name, value in items:
|
||||
|
@ -139,7 +139,7 @@ class TmpTestdir:
|
|||
# used from runner functional tests
|
||||
item = self.getitem(source)
|
||||
# the test class where we are called from wants to provide the runner
|
||||
testclassinstance = self.pyfuncitem.obj.im_self
|
||||
testclassinstance = self.request.function.im_self
|
||||
runner = testclassinstance.getrunner()
|
||||
return runner(item, **runnerargs)
|
||||
|
||||
|
@ -200,7 +200,7 @@ class TmpTestdir:
|
|||
return self.config.getfsnode(path)
|
||||
|
||||
def getmodulecol(self, source, configargs=(), withinit=False):
|
||||
kw = {self.pyfuncitem.name: py.code.Source(source).strip()}
|
||||
kw = {self.request.funcname: py.code.Source(source).strip()}
|
||||
path = self.makepyfile(**kw)
|
||||
if withinit:
|
||||
self.makepyfile(__init__ = "#")
|
||||
|
@ -455,3 +455,10 @@ class LineMatcher:
|
|||
return extralines
|
||||
|
||||
|
||||
|
||||
|
||||
def test_parseconfig(testdir):
|
||||
config1 = testdir.parseconfig()
|
||||
config2 = testdir.parseconfig()
|
||||
assert config2 != config1
|
||||
assert config1 != py.test.config
|
||||
|
|
|
@ -382,10 +382,15 @@ class TestApigenLinkRole:
|
|||
"resolve_linkrole('source', 'py/foo/bar.py')")
|
||||
|
||||
|
||||
def pytest_funcarg__testdir(__call__, pyfuncitem):
|
||||
testdir = __call__.execute(firstresult=True)
|
||||
def pytest_funcarg__testdir(request):
|
||||
testdir = request.call_next_provider()
|
||||
testdir.makepyfile(confrest="from py.__.misc.rest import Project")
|
||||
testdir.plugins.append(RestdocPlugin())
|
||||
count = 0
|
||||
for p in testdir.plugins:
|
||||
if isinstance(p, RestdocPlugin):
|
||||
count += 1
|
||||
assert count < 2
|
||||
return testdir
|
||||
|
||||
class TestDoctest:
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
import uuid
|
||||
import py
|
||||
from pytest_resultlog import ResultLog
|
||||
|
||||
|
@ -65,7 +64,7 @@ class JSONResultArchive(object):
|
|||
self._flush()
|
||||
|
||||
def append_data(self, data):
|
||||
runid = uuid.uuid4()
|
||||
runid = py.std.uuid.uuid4()
|
||||
for item in data:
|
||||
item = item.copy()
|
||||
item['runid'] = str(runid)
|
||||
|
@ -100,7 +99,7 @@ class SQLiteResultArchive(object):
|
|||
|
||||
def append_data(self, data):
|
||||
flat_data = []
|
||||
runid = uuid.uuid4()
|
||||
runid = py.std.uuid.uuid4()
|
||||
for item in data:
|
||||
item = item.copy()
|
||||
item['runid'] = str(runid)
|
||||
|
|
|
@ -157,6 +157,7 @@ class TestWithFunctionIntegration:
|
|||
# ignorant regarding formatting details.
|
||||
def getresultlog(self, testdir, arg):
|
||||
resultlog = testdir.tmpdir.join("resultlog")
|
||||
testdir.plugins.append("resultlog")
|
||||
args = ["--resultlog=%s" % resultlog] + [arg]
|
||||
testdir.runpytest(*args)
|
||||
return filter(None, resultlog.readlines(cr=0))
|
||||
|
@ -166,7 +167,6 @@ class TestWithFunctionIntegration:
|
|||
ok = testdir.makepyfile(test_collection_ok="")
|
||||
skip = testdir.makepyfile(test_collection_skip="import py ; py.test.skip('hello')")
|
||||
fail = testdir.makepyfile(test_collection_fail="XXX")
|
||||
|
||||
lines = self.getresultlog(testdir, ok)
|
||||
assert not lines
|
||||
|
||||
|
@ -226,6 +226,7 @@ class TestWithFunctionIntegration:
|
|||
def test_generic(plugintester, LineMatcher):
|
||||
plugintester.apicheck(ResultlogPlugin)
|
||||
testdir = plugintester.testdir()
|
||||
testdir.plugins.append("resultlog")
|
||||
testdir.makepyfile("""
|
||||
import py
|
||||
def test_pass():
|
||||
|
|
|
@ -13,9 +13,9 @@ class TmpdirPlugin:
|
|||
""" provide temporary directories to test functions and methods.
|
||||
"""
|
||||
|
||||
def pytest_funcarg__tmpdir(self, pyfuncitem):
|
||||
name = pyfuncitem.name
|
||||
return pyfuncitem.config.mktemp(name, numbered=True)
|
||||
def pytest_funcarg__tmpdir(self, request):
|
||||
name = request.funcname
|
||||
return request.config.mktemp(name, numbered=True)
|
||||
|
||||
# ===============================================================================
|
||||
#
|
||||
|
@ -29,7 +29,7 @@ def test_generic(plugintester):
|
|||
def test_funcarg(testdir):
|
||||
item = testdir.getitem("def test_func(tmpdir): pass")
|
||||
plugin = TmpdirPlugin()
|
||||
p = plugin.pytest_funcarg__tmpdir(item)
|
||||
p = plugin.pytest_funcarg__tmpdir(item.getrequest("tmpdir"))
|
||||
assert p.check()
|
||||
bn = p.basename.strip("0123456789-")
|
||||
assert bn.endswith("test_func")
|
||||
|
|
|
@ -18,6 +18,7 @@ class PluginManager(object):
|
|||
|
||||
def register(self, plugin):
|
||||
self.api.pytest_plugin_registered(plugin=plugin)
|
||||
import types
|
||||
self.comregistry.register(plugin)
|
||||
|
||||
def unregister(self, plugin):
|
||||
|
|
|
@ -177,7 +177,7 @@ class Module(py.test.collect.File, PyCollectorMixin):
|
|||
#print "*" * 20, "INVOKE assertion", self
|
||||
py.magic.invoke(assertion=1)
|
||||
mod = self.obj
|
||||
self.config.pluginmanager.register(mod)
|
||||
#self.config.pluginmanager.register(mod)
|
||||
if hasattr(mod, 'setup_module'):
|
||||
self.obj.setup_module(mod)
|
||||
|
||||
|
@ -187,7 +187,7 @@ class Module(py.test.collect.File, PyCollectorMixin):
|
|||
if not self.config.option.nomagic:
|
||||
#print "*" * 20, "revoke assertion", self
|
||||
py.magic.revoke(assertion=1)
|
||||
self.config.pluginmanager.unregister(self.obj)
|
||||
#self.config.pluginmanager.unregister(self.obj)
|
||||
|
||||
class Class(PyCollectorMixin, py.test.collect.Collector):
|
||||
|
||||
|
@ -366,7 +366,8 @@ class Function(FunctionMixin, py.test.collect.Item):
|
|||
if i < startindex:
|
||||
continue
|
||||
try:
|
||||
self.funcargs[argname] = self.lookup_onearg(argname)
|
||||
argvalue = self.getrequest(argname).call_next_provider()
|
||||
self.funcargs[argname] = argvalue
|
||||
except LookupError, e:
|
||||
numdefaults = len(funcobj.func_defaults or ())
|
||||
if i + numdefaults >= len(argnames):
|
||||
|
@ -374,15 +375,6 @@ class Function(FunctionMixin, py.test.collect.Item):
|
|||
else:
|
||||
raise
|
||||
|
||||
def lookup_onearg(self, argname):
|
||||
prefix = "pytest_funcarg__"
|
||||
#makerlist = self.config.pluginmanager.listattr(prefix + argname)
|
||||
value = self.config.pluginmanager.call_firstresult(prefix + argname, pyfuncitem=self)
|
||||
if value is not None:
|
||||
return value
|
||||
else:
|
||||
self._raisefuncargerror(argname, prefix)
|
||||
|
||||
def _raisefuncargerror(self, argname, prefix="pytest_funcarg__"):
|
||||
metainfo = self.repr_metainfo()
|
||||
available = []
|
||||
|
@ -419,9 +411,11 @@ class FuncargRequest:
|
|||
""" error on performing funcarg request. """
|
||||
|
||||
def __init__(self, pyfuncitem, argname):
|
||||
# XXX make pyfuncitem _pyfuncitem
|
||||
self.pyfuncitem = pyfuncitem
|
||||
self.argname = argname
|
||||
self.function = pyfuncitem.obj
|
||||
self.funcname = pyfuncitem.name
|
||||
self.config = pyfuncitem.config
|
||||
extra = []
|
||||
current = pyfuncitem
|
||||
|
@ -434,11 +428,19 @@ class FuncargRequest:
|
|||
extra=extra,
|
||||
)
|
||||
|
||||
def __repr__(self):
|
||||
return "<FuncargRequest %r for %r>" %(self.argname, self.pyfuncitem)
|
||||
|
||||
def call_next_provider(self):
|
||||
if not self._methods:
|
||||
raise self.Error("no provider methods left")
|
||||
nextmethod = self._methods.pop()
|
||||
print "calling", nextmethod
|
||||
return nextmethod(request=self)
|
||||
|
||||
def addfinalizer(self, finalizer):
|
||||
self.pyfuncitem.addfinalizer(finalizer)
|
||||
|
||||
def getfspath(self):
|
||||
return self.pyfuncitem.fspath
|
||||
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
|
||||
pytest_plugins = "pytest_xfail", "pytest_pytester", "pytest_tmpdir"
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ class TestFuncargs:
|
|||
def test_funcarg_lookupfails(self, testdir):
|
||||
testdir.makeconftest("""
|
||||
class ConftestPlugin:
|
||||
def pytest_funcarg__something(self, pyfuncitem):
|
||||
def pytest_funcarg__xyz(self, request):
|
||||
return 42
|
||||
""")
|
||||
item = testdir.getitem("def test_func(some): pass")
|
||||
|
@ -15,8 +15,8 @@ class TestFuncargs:
|
|||
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
|
||||
def pytest_funcarg__some(self, request):
|
||||
return request.funcname
|
||||
item.config.pluginmanager.register(Provider())
|
||||
item.setupargs()
|
||||
assert len(item.funcargs) == 1
|
||||
|
@ -24,8 +24,8 @@ class TestFuncargs:
|
|||
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
|
||||
def pytest_funcarg__other(self, request):
|
||||
return request.funcname
|
||||
item.config.pluginmanager.register(Provider())
|
||||
item.setupargs()
|
||||
assert len(item.funcargs) == 1
|
||||
|
@ -36,9 +36,9 @@ class TestFuncargs:
|
|||
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):
|
||||
def pytest_funcarg__some(self, request):
|
||||
return request.funcname
|
||||
def pytest_funcarg__other(self, request):
|
||||
return 42
|
||||
item.config.pluginmanager.register(Provider())
|
||||
item.setupargs()
|
||||
|
@ -46,26 +46,10 @@ class TestFuncargs:
|
|||
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
|
||||
def pytest_funcarg__something(request):
|
||||
return request.funcname
|
||||
|
||||
class TestClass:
|
||||
def test_method(self, something):
|
||||
|
@ -74,14 +58,10 @@ class TestFuncargs:
|
|||
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_attributes(self, testdir):
|
||||
|
@ -92,7 +72,9 @@ class TestRequest:
|
|||
req = item.getrequest("other")
|
||||
assert req.argname == "other"
|
||||
assert req.function == item.obj
|
||||
assert req.funcname == "test_func"
|
||||
assert req.config == item.config
|
||||
assert repr(req).find(req.funcname) != -1
|
||||
|
||||
def test_request_contains_funcargs_methods(self, testdir):
|
||||
modcol = testdir.getmodulecol("""
|
||||
|
@ -131,3 +113,9 @@ class TestRequest:
|
|||
l = [1]
|
||||
req.addfinalizer(l.pop)
|
||||
item.teardown()
|
||||
|
||||
def test_request_getmodulepath(self, testdir):
|
||||
modcol = testdir.getmodulecol("def test_somefunc(): pass")
|
||||
item, = testdir.genitems([modcol])
|
||||
req = item.getrequest("hello")
|
||||
assert req.getfspath() == modcol.fspath
|
||||
|
|
|
@ -1,31 +1,27 @@
|
|||
import py
|
||||
|
||||
def pytest_funcarg__pickletransport(pyfuncitem):
|
||||
return ImmutablePickleTransport()
|
||||
|
||||
def pytest_pyfunc_call(__call__, pyfuncitem, args, kwargs):
|
||||
# for each function call we patch py._com.comregistry
|
||||
# so that the unpickling of config objects
|
||||
# (which bind to this mechanism) doesn't do harm
|
||||
# usually config objects are no meant to be unpickled in
|
||||
# the same system
|
||||
def setglobals(request):
|
||||
oldconfig = py.test.config
|
||||
oldcom = py._com.comregistry
|
||||
print "setting py.test.config to None"
|
||||
py.test.config = None
|
||||
py._com.comregistry = py._com.Registry()
|
||||
try:
|
||||
return __call__.execute(firstresult=True)
|
||||
finally:
|
||||
def resetglobals():
|
||||
print "setting py.test.config to", oldconfig
|
||||
py.test.config = oldconfig
|
||||
py._com.comregistry = oldcom
|
||||
request.addfinalizer(resetglobals)
|
||||
|
||||
def pytest_funcarg__testdir(request):
|
||||
setglobals(request)
|
||||
return request.call_next_provider()
|
||||
|
||||
class ImmutablePickleTransport:
|
||||
def __init__(self):
|
||||
def __init__(self, request):
|
||||
from py.__.test.dist.mypickle import ImmutablePickler
|
||||
self.p1 = ImmutablePickler(uneven=0)
|
||||
self.p2 = ImmutablePickler(uneven=1)
|
||||
setglobals(request)
|
||||
|
||||
def p1_to_p2(self, obj):
|
||||
return self.p2.loads(self.p1.dumps(obj))
|
||||
|
@ -39,6 +35,8 @@ class ImmutablePickleTransport:
|
|||
return p2config
|
||||
|
||||
class TestImmutablePickling:
|
||||
pytest_funcarg__pickletransport = ImmutablePickleTransport
|
||||
|
||||
def test_pickle_config(self, testdir, pickletransport):
|
||||
config1 = testdir.parseconfig()
|
||||
assert config1.topdir == testdir.tmpdir
|
||||
|
|
|
@ -34,13 +34,6 @@ class TestModule:
|
|||
x = l.pop()
|
||||
assert x is None
|
||||
|
||||
def test_module_participates_as_plugin(self, testdir):
|
||||
modcol = testdir.getmodulecol("")
|
||||
modcol.setup()
|
||||
assert modcol.config.pluginmanager.isregistered(modcol.obj)
|
||||
modcol.teardown()
|
||||
assert not modcol.config.pluginmanager.isregistered(modcol.obj)
|
||||
|
||||
def test_module_considers_pluginmanager_at_import(self, testdir):
|
||||
modcol = testdir.getmodulecol("pytest_plugins='xasdlkj',")
|
||||
py.test.raises(ImportError, "modcol.obj")
|
||||
|
|
|
@ -10,7 +10,7 @@ class TestTracebackCutting:
|
|||
def test_traceback_argsetup(self, testdir):
|
||||
testdir.makeconftest("""
|
||||
class ConftestPlugin:
|
||||
def pytest_funcarg__hello(self, pyfuncitem):
|
||||
def pytest_funcarg__hello(self, request):
|
||||
raise ValueError("xyz")
|
||||
""")
|
||||
p = testdir.makepyfile("def test(hello): pass")
|
||||
|
|
Loading…
Reference in New Issue