2009-02-27 18:18:27 +08:00
|
|
|
"""
|
2009-07-09 01:18:26 +08:00
|
|
|
funcargs and support code for testing py.test's own functionality.
|
2009-02-27 18:18:27 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
import py
|
2009-06-19 01:02:59 +08:00
|
|
|
import sys, os
|
2009-12-25 07:24:58 +08:00
|
|
|
import re
|
2009-04-03 18:57:34 +08:00
|
|
|
import inspect
|
2009-12-30 18:37:46 +08:00
|
|
|
import time
|
2010-01-14 00:15:54 +08:00
|
|
|
from py._test.config import Config as pytestConfig
|
2009-08-29 01:16:15 +08:00
|
|
|
from py.builtin import print_
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2010-01-02 03:36:58 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
group = parser.getgroup("pylib")
|
|
|
|
group.addoption('--tools-on-path',
|
|
|
|
action="store_true", dest="toolsonpath", default=False,
|
|
|
|
help=("discover tools on PATH instead of going through py.cmdline.")
|
|
|
|
)
|
|
|
|
|
2009-05-21 21:22:01 +08:00
|
|
|
pytest_plugins = '_pytest'
|
2009-05-20 01:25:21 +08:00
|
|
|
|
2009-05-19 05:26:16 +08:00
|
|
|
def pytest_funcarg__linecomp(request):
|
|
|
|
return LineComp()
|
2009-04-09 07:33:48 +08:00
|
|
|
|
2009-05-19 05:26:16 +08:00
|
|
|
def pytest_funcarg__LineMatcher(request):
|
|
|
|
return LineMatcher
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-05-19 05:26:16 +08:00
|
|
|
def pytest_funcarg__testdir(request):
|
|
|
|
tmptestdir = TmpTestdir(request)
|
|
|
|
return tmptestdir
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-12-25 07:24:58 +08:00
|
|
|
rex_outcome = re.compile("(\d+) (\w+)")
|
2009-02-27 18:18:27 +08:00
|
|
|
class RunResult:
|
2009-12-30 18:37:46 +08:00
|
|
|
def __init__(self, ret, outlines, errlines, duration):
|
2009-02-27 18:18:27 +08:00
|
|
|
self.ret = ret
|
|
|
|
self.outlines = outlines
|
|
|
|
self.errlines = errlines
|
|
|
|
self.stdout = LineMatcher(outlines)
|
|
|
|
self.stderr = LineMatcher(errlines)
|
2009-12-30 18:37:46 +08:00
|
|
|
self.duration = duration
|
|
|
|
|
2009-12-25 07:24:58 +08:00
|
|
|
def parseoutcomes(self):
|
|
|
|
for line in reversed(self.outlines):
|
|
|
|
if 'seconds' in line:
|
|
|
|
outcomes = rex_outcome.findall(line)
|
|
|
|
if outcomes:
|
|
|
|
d = {}
|
|
|
|
for num, cat in outcomes:
|
|
|
|
d[cat] = int(num)
|
|
|
|
return d
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
class TmpTestdir:
|
2009-04-15 00:30:26 +08:00
|
|
|
def __init__(self, request):
|
|
|
|
self.request = request
|
2009-05-21 15:45:43 +08:00
|
|
|
self._pytest = request.getfuncargvalue("_pytest")
|
2009-02-27 18:18:27 +08:00
|
|
|
# XXX remove duplication with tmpdir plugin
|
2009-04-15 00:30:26 +08:00
|
|
|
basetmp = request.config.ensuretemp("testdir")
|
2009-04-15 01:57:00 +08:00
|
|
|
name = request.function.__name__
|
2009-02-27 18:18:27 +08:00
|
|
|
for i in range(100):
|
|
|
|
try:
|
|
|
|
tmpdir = basetmp.mkdir(name + str(i))
|
|
|
|
except py.error.EEXIST:
|
|
|
|
continue
|
|
|
|
break
|
|
|
|
# we need to create another subdir
|
|
|
|
# because Directory.collect() currently loads
|
|
|
|
# conftest.py from sibling directories
|
|
|
|
self.tmpdir = tmpdir.mkdir(name)
|
|
|
|
self.plugins = []
|
|
|
|
self._syspathremove = []
|
2010-01-02 18:57:42 +08:00
|
|
|
self.chdir() # always chdir
|
2009-04-15 00:30:26 +08:00
|
|
|
self.request.addfinalizer(self.finalize)
|
2009-04-01 01:58:02 +08:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<TmpTestdir %r>" % (self.tmpdir,)
|
2009-03-01 21:43:53 +08:00
|
|
|
|
2009-12-29 21:13:12 +08:00
|
|
|
def Config(self, topdir=None):
|
2009-03-01 21:43:53 +08:00
|
|
|
if topdir is None:
|
|
|
|
topdir = self.tmpdir.dirpath()
|
2009-12-29 21:13:12 +08:00
|
|
|
return pytestConfig(topdir=topdir)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def finalize(self):
|
|
|
|
for p in self._syspathremove:
|
|
|
|
py.std.sys.path.remove(p)
|
|
|
|
if hasattr(self, '_olddir'):
|
|
|
|
self._olddir.chdir()
|
2009-10-29 02:51:20 +08:00
|
|
|
# delete modules that have been loaded from tmpdir
|
|
|
|
for name, mod in list(sys.modules.items()):
|
|
|
|
if mod:
|
|
|
|
fn = getattr(mod, '__file__', None)
|
|
|
|
if fn and fn.startswith(str(self.tmpdir)):
|
|
|
|
del sys.modules[name]
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-05-21 15:45:43 +08:00
|
|
|
def getreportrecorder(self, obj):
|
2009-12-29 19:36:17 +08:00
|
|
|
if hasattr(obj, 'config'):
|
|
|
|
obj = obj.config
|
|
|
|
if hasattr(obj, 'hook'):
|
|
|
|
obj = obj.hook
|
|
|
|
assert hasattr(obj, '_hookspecs'), obj
|
|
|
|
reprec = ReportRecorder(obj)
|
|
|
|
reprec.hookrecorder = self._pytest.gethookrecorder(obj)
|
2009-05-21 15:45:43 +08:00
|
|
|
reprec.hook = reprec.hookrecorder.hook
|
|
|
|
return reprec
|
2009-04-05 03:06:20 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def chdir(self):
|
2009-04-01 01:58:02 +08:00
|
|
|
old = self.tmpdir.chdir()
|
2009-02-27 18:18:27 +08:00
|
|
|
if not hasattr(self, '_olddir'):
|
|
|
|
self._olddir = old
|
|
|
|
|
|
|
|
def _makefile(self, ext, args, kwargs):
|
2009-08-29 01:16:15 +08:00
|
|
|
items = list(kwargs.items())
|
2009-02-27 18:18:27 +08:00
|
|
|
if args:
|
|
|
|
source = "\n".join(map(str, args))
|
2009-04-15 01:57:00 +08:00
|
|
|
basename = self.request.function.__name__
|
2009-02-27 18:18:27 +08:00
|
|
|
items.insert(0, (basename, source))
|
|
|
|
ret = None
|
|
|
|
for name, value in items:
|
|
|
|
p = self.tmpdir.join(name).new(ext=ext)
|
|
|
|
source = py.code.Source(value)
|
|
|
|
p.write(str(py.code.Source(value)).lstrip())
|
|
|
|
if ret is None:
|
|
|
|
ret = p
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def makefile(self, ext, *args, **kwargs):
|
|
|
|
return self._makefile(ext, args, kwargs)
|
|
|
|
|
|
|
|
def makeconftest(self, source):
|
|
|
|
return self.makepyfile(conftest=source)
|
|
|
|
|
|
|
|
def makepyfile(self, *args, **kwargs):
|
|
|
|
return self._makefile('.py', args, kwargs)
|
|
|
|
|
|
|
|
def maketxtfile(self, *args, **kwargs):
|
|
|
|
return self._makefile('.txt', args, kwargs)
|
|
|
|
|
|
|
|
def syspathinsert(self, path=None):
|
|
|
|
if path is None:
|
|
|
|
path = self.tmpdir
|
|
|
|
py.std.sys.path.insert(0, str(path))
|
|
|
|
self._syspathremove.append(str(path))
|
|
|
|
|
|
|
|
def mkdir(self, name):
|
|
|
|
return self.tmpdir.mkdir(name)
|
2009-07-30 15:52:12 +08:00
|
|
|
|
|
|
|
def mkpydir(self, name):
|
|
|
|
p = self.mkdir(name)
|
|
|
|
p.ensure("__init__.py")
|
|
|
|
return p
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def genitems(self, colitems):
|
|
|
|
return list(self.session.genitems(colitems))
|
|
|
|
|
|
|
|
def inline_genitems(self, *args):
|
|
|
|
#config = self.parseconfig(*args)
|
|
|
|
config = self.parseconfig(*args)
|
|
|
|
session = config.initsession()
|
2009-05-21 15:45:43 +08:00
|
|
|
rec = self.getreportrecorder(config)
|
2010-01-11 04:29:36 +08:00
|
|
|
colitems = [config.getnode(arg) for arg in config.args]
|
2009-02-27 18:18:27 +08:00
|
|
|
items = list(session.genitems(colitems))
|
|
|
|
return items, rec
|
|
|
|
|
2009-05-23 01:57:21 +08:00
|
|
|
def runitem(self, source):
|
2009-02-27 18:18:27 +08:00
|
|
|
# used from runner functional tests
|
|
|
|
item = self.getitem(source)
|
|
|
|
# the test class where we are called from wants to provide the runner
|
2009-09-01 22:10:21 +08:00
|
|
|
testclassinstance = py.builtin._getimself(self.request.function)
|
2009-02-27 18:18:27 +08:00
|
|
|
runner = testclassinstance.getrunner()
|
2009-05-23 01:57:21 +08:00
|
|
|
return runner(item)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def inline_runsource(self, source, *cmdlineargs):
|
|
|
|
p = self.makepyfile(source)
|
|
|
|
l = list(cmdlineargs) + [p]
|
|
|
|
return self.inline_run(*l)
|
|
|
|
|
2009-05-23 01:57:21 +08:00
|
|
|
def inline_runsource1(self, *args):
|
|
|
|
args = list(args)
|
|
|
|
source = args.pop()
|
|
|
|
p = self.makepyfile(source)
|
|
|
|
l = list(args) + [p]
|
|
|
|
reprec = self.inline_run(*l)
|
2009-06-09 00:31:10 +08:00
|
|
|
reports = reprec.getreports("pytest_runtest_logreport")
|
2009-05-23 01:57:21 +08:00
|
|
|
assert len(reports) == 1, reports
|
|
|
|
return reports[0]
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def inline_run(self, *args):
|
2009-03-06 05:10:18 +08:00
|
|
|
config = self.parseconfig(*args)
|
2009-04-09 22:03:09 +08:00
|
|
|
config.pluginmanager.do_configure(config)
|
2009-03-06 05:10:18 +08:00
|
|
|
session = config.initsession()
|
2009-05-21 20:33:09 +08:00
|
|
|
reprec = self.getreportrecorder(config)
|
2010-01-11 04:29:36 +08:00
|
|
|
colitems = config.getinitialnodes()
|
|
|
|
session.main(colitems)
|
2009-04-09 22:03:09 +08:00
|
|
|
config.pluginmanager.do_unconfigure(config)
|
2009-05-21 20:33:09 +08:00
|
|
|
return reprec
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def config_preparse(self):
|
|
|
|
config = self.Config()
|
|
|
|
for plugin in self.plugins:
|
|
|
|
if isinstance(plugin, str):
|
2009-04-09 22:03:09 +08:00
|
|
|
config.pluginmanager.import_plugin(plugin)
|
2009-05-20 01:25:21 +08:00
|
|
|
else:
|
2009-05-19 05:26:16 +08:00
|
|
|
if isinstance(plugin, dict):
|
|
|
|
plugin = PseudoPlugin(plugin)
|
2009-05-20 01:25:21 +08:00
|
|
|
if not config.pluginmanager.isregistered(plugin):
|
|
|
|
config.pluginmanager.register(plugin)
|
2009-02-27 18:18:27 +08:00
|
|
|
return config
|
|
|
|
|
|
|
|
def parseconfig(self, *args):
|
|
|
|
if not args:
|
|
|
|
args = (self.tmpdir,)
|
|
|
|
config = self.config_preparse()
|
2009-03-17 20:22:14 +08:00
|
|
|
args = list(args) + ["--basetemp=%s" % self.tmpdir.dirpath('basetemp')]
|
|
|
|
config.parse(args)
|
2009-02-27 18:18:27 +08:00
|
|
|
return config
|
|
|
|
|
2009-12-30 01:02:54 +08:00
|
|
|
def reparseconfig(self, args=None):
|
|
|
|
""" this is used from tests that want to re-invoke parse(). """
|
|
|
|
if not args:
|
|
|
|
args = [self.tmpdir]
|
2010-01-14 00:15:54 +08:00
|
|
|
from py._test import config
|
2010-01-18 06:23:02 +08:00
|
|
|
oldconfig = config.config_per_process # py.test.config
|
2009-12-30 01:02:54 +08:00
|
|
|
try:
|
|
|
|
c = config.config_per_process = py.test.config = pytestConfig()
|
|
|
|
c.basetemp = oldconfig.mktemp("reparse", numbered=True)
|
|
|
|
c.parse(args)
|
|
|
|
return c
|
|
|
|
finally:
|
|
|
|
config.config_per_process = py.test.config = oldconfig
|
|
|
|
|
2009-03-23 01:41:36 +08:00
|
|
|
def parseconfigure(self, *args):
|
|
|
|
config = self.parseconfig(*args)
|
2009-04-09 22:03:09 +08:00
|
|
|
config.pluginmanager.do_configure(config)
|
2009-03-23 01:41:36 +08:00
|
|
|
return config
|
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def getitem(self, source, funcname="test_func"):
|
|
|
|
modcol = self.getmodulecol(source)
|
2009-04-05 03:06:20 +08:00
|
|
|
moditems = modcol.collect()
|
|
|
|
for item in modcol.collect():
|
|
|
|
if item.name == funcname:
|
|
|
|
return item
|
|
|
|
else:
|
|
|
|
assert 0, "%r item not found in module:\n%s" %(funcname, source)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def getitems(self, source):
|
|
|
|
modcol = self.getmodulecol(source)
|
2009-03-18 07:48:07 +08:00
|
|
|
return list(modcol.config.initsession().genitems([modcol]))
|
2009-02-27 18:18:27 +08:00
|
|
|
#assert item is not None, "%r item not found in module:\n%s" %(funcname, source)
|
|
|
|
#return item
|
|
|
|
|
|
|
|
def getfscol(self, path, configargs=()):
|
|
|
|
self.config = self.parseconfig(path, *configargs)
|
|
|
|
self.session = self.config.initsession()
|
2010-01-11 04:29:36 +08:00
|
|
|
return self.config.getnode(path)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def getmodulecol(self, source, configargs=(), withinit=False):
|
2009-04-15 01:57:00 +08:00
|
|
|
kw = {self.request.function.__name__: py.code.Source(source).strip()}
|
2009-02-27 18:18:27 +08:00
|
|
|
path = self.makepyfile(**kw)
|
|
|
|
if withinit:
|
|
|
|
self.makepyfile(__init__ = "#")
|
|
|
|
self.config = self.parseconfig(path, *configargs)
|
|
|
|
self.session = self.config.initsession()
|
2009-06-09 00:31:10 +08:00
|
|
|
#self.config.pluginmanager.do_configure(config=self.config)
|
|
|
|
# XXX
|
|
|
|
self.config.pluginmanager.import_plugin("runner")
|
|
|
|
plugin = self.config.pluginmanager.getplugin("runner")
|
|
|
|
plugin.pytest_configure(config=self.config)
|
|
|
|
|
2010-01-11 04:29:36 +08:00
|
|
|
return self.config.getnode(path)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def popen(self, cmdargs, stdout, stderr, **kw):
|
|
|
|
if not hasattr(py.std, 'subprocess'):
|
|
|
|
py.test.skip("no subprocess module")
|
2009-04-29 05:49:03 +08:00
|
|
|
env = os.environ.copy()
|
2009-05-08 03:04:56 +08:00
|
|
|
env['PYTHONPATH'] = ":".join(filter(None, [
|
|
|
|
str(os.getcwd()), env.get('PYTHONPATH', '')]))
|
2009-04-29 05:49:03 +08:00
|
|
|
kw['env'] = env
|
2009-05-08 03:04:56 +08:00
|
|
|
#print "env", env
|
2009-02-27 18:18:27 +08:00
|
|
|
return py.std.subprocess.Popen(cmdargs, stdout=stdout, stderr=stderr, **kw)
|
|
|
|
|
|
|
|
def run(self, *cmdargs):
|
2010-01-02 04:54:27 +08:00
|
|
|
return self._run(*cmdargs)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def _run(self, *cmdargs):
|
2009-08-29 21:51:49 +08:00
|
|
|
cmdargs = [str(x) for x in cmdargs]
|
2010-01-02 06:05:00 +08:00
|
|
|
p1 = self.tmpdir.join("stdout")
|
|
|
|
p2 = self.tmpdir.join("stderr")
|
2009-08-29 01:16:15 +08:00
|
|
|
print_("running", cmdargs, "curdir=", py.path.local())
|
2009-06-09 22:08:34 +08:00
|
|
|
f1 = p1.open("w")
|
|
|
|
f2 = p2.open("w")
|
2009-12-30 18:37:46 +08:00
|
|
|
now = time.time()
|
2009-06-19 01:02:59 +08:00
|
|
|
popen = self.popen(cmdargs, stdout=f1, stderr=f2,
|
|
|
|
close_fds=(sys.platform != "win32"))
|
2009-02-27 18:18:27 +08:00
|
|
|
ret = popen.wait()
|
2009-06-09 22:08:34 +08:00
|
|
|
f1.close()
|
|
|
|
f2.close()
|
2009-02-27 18:18:27 +08:00
|
|
|
out, err = p1.readlines(cr=0), p2.readlines(cr=0)
|
|
|
|
if err:
|
|
|
|
for line in err:
|
2009-08-29 21:51:49 +08:00
|
|
|
py.builtin.print_(line, file=sys.stderr)
|
2009-02-27 18:18:27 +08:00
|
|
|
if out:
|
|
|
|
for line in out:
|
2009-08-29 21:51:49 +08:00
|
|
|
py.builtin.print_(line, file=sys.stdout)
|
2009-12-30 18:37:46 +08:00
|
|
|
return RunResult(ret, out, err, time.time()-now)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def runpybin(self, scriptname, *args):
|
2009-05-06 17:47:48 +08:00
|
|
|
fullargs = self._getpybinargs(scriptname) + args
|
|
|
|
return self.run(*fullargs)
|
|
|
|
|
|
|
|
def _getpybinargs(self, scriptname):
|
2010-01-02 03:36:58 +08:00
|
|
|
if self.request.config.getvalue("toolsonpath"):
|
|
|
|
script = py.path.local.sysfind(scriptname)
|
|
|
|
assert script, "script %r not found" % scriptname
|
|
|
|
return (script,)
|
|
|
|
else:
|
|
|
|
cmdlinename = scriptname.replace(".", "")
|
|
|
|
assert hasattr(py.cmdline, cmdlinename), cmdlinename
|
2010-04-21 18:50:03 +08:00
|
|
|
source = ("import sys;sys.path.insert(0,%r);"
|
|
|
|
"import py;py.cmdline.%s()" %
|
2010-01-11 21:30:50 +08:00
|
|
|
(str(py._pydir.dirpath()), cmdlinename))
|
2010-01-02 03:36:58 +08:00
|
|
|
return (sys.executable, "-c", source,)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-07-31 20:21:02 +08:00
|
|
|
def runpython(self, script):
|
2010-01-11 03:45:37 +08:00
|
|
|
s = self._getsysprepend()
|
|
|
|
if s:
|
|
|
|
script.write(s + "\n" + script.read())
|
|
|
|
return self.run(sys.executable, script)
|
|
|
|
|
|
|
|
def _getsysprepend(self):
|
|
|
|
if not self.request.config.getvalue("toolsonpath"):
|
2010-04-21 18:50:03 +08:00
|
|
|
s = "import sys;sys.path.insert(0,%r);" % str(py._pydir.dirpath())
|
2010-01-11 03:45:37 +08:00
|
|
|
else:
|
|
|
|
s = ""
|
|
|
|
return s
|
|
|
|
|
|
|
|
def runpython_c(self, command):
|
|
|
|
command = self._getsysprepend() + command
|
|
|
|
return self.run(py.std.sys.executable, "-c", command)
|
2009-07-31 20:21:02 +08:00
|
|
|
|
2009-02-27 18:18:27 +08:00
|
|
|
def runpytest(self, *args):
|
2009-03-17 18:29:45 +08:00
|
|
|
p = py.path.local.make_numbered_dir(prefix="runpytest-",
|
|
|
|
keep=None, rootdir=self.tmpdir)
|
|
|
|
args = ('--basetemp=%s' % p, ) + args
|
2009-12-25 07:24:58 +08:00
|
|
|
plugins = [x for x in self.plugins if isinstance(x, str)]
|
|
|
|
if plugins:
|
|
|
|
args = ('-p', plugins[0]) + args
|
2009-02-27 18:18:27 +08:00
|
|
|
return self.runpybin("py.test", *args)
|
|
|
|
|
2009-05-06 17:47:48 +08:00
|
|
|
def spawn_pytest(self, string, expect_timeout=10.0):
|
2009-11-24 22:16:58 +08:00
|
|
|
pexpect = py.test.importorskip("pexpect", "2.4")
|
2010-01-02 04:03:33 +08:00
|
|
|
if not self.request.config.getvalue("toolsonpath"):
|
|
|
|
py.test.skip("need --tools-on-path to run py.test script")
|
2009-05-06 17:47:48 +08:00
|
|
|
basetemp = self.tmpdir.mkdir("pexpect")
|
2010-01-02 04:03:33 +08:00
|
|
|
invoke = self._getpybinargs("py.test")[0]
|
2009-05-06 17:47:48 +08:00
|
|
|
cmd = "%s --basetemp=%s %s" % (invoke, basetemp, string)
|
|
|
|
child = pexpect.spawn(cmd, logfile=basetemp.join("spawn.out").open("w"))
|
|
|
|
child.timeout = expect_timeout
|
|
|
|
return child
|
|
|
|
|
2009-05-19 05:26:16 +08:00
|
|
|
class PseudoPlugin:
|
|
|
|
def __init__(self, vars):
|
|
|
|
self.__dict__.update(vars)
|
|
|
|
|
2009-05-20 01:25:21 +08:00
|
|
|
class ReportRecorder(object):
|
2009-12-29 19:36:17 +08:00
|
|
|
def __init__(self, hook):
|
|
|
|
self.hook = hook
|
|
|
|
self.registry = hook._registry
|
|
|
|
self.registry.register(self)
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-04-07 18:48:57 +08:00
|
|
|
def getcall(self, name):
|
2009-05-20 01:25:21 +08:00
|
|
|
return self.hookrecorder.getcall(name)
|
2009-04-07 18:48:57 +08:00
|
|
|
|
2009-05-23 01:56:05 +08:00
|
|
|
def popcall(self, name):
|
|
|
|
return self.hookrecorder.popcall(name)
|
|
|
|
|
2009-05-20 01:25:21 +08:00
|
|
|
def getcalls(self, names):
|
2009-04-07 17:53:01 +08:00
|
|
|
""" return list of ParsedCall instances matching the given eventname. """
|
2009-05-20 01:25:21 +08:00
|
|
|
return self.hookrecorder.getcalls(names)
|
2009-04-03 18:57:34 +08:00
|
|
|
|
2009-04-07 18:48:57 +08:00
|
|
|
# functionality for test reports
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-06-09 00:31:10 +08:00
|
|
|
def getreports(self, names="pytest_runtest_logreport pytest_collectreport"):
|
2009-08-04 18:00:04 +08:00
|
|
|
return [x.report for x in self.getcalls(names)]
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-06-09 00:31:10 +08:00
|
|
|
def matchreport(self, inamepart="", names="pytest_runtest_logreport pytest_collectreport"):
|
2009-04-07 18:48:57 +08:00
|
|
|
""" return a testreport whose dotted import path matches """
|
|
|
|
l = []
|
|
|
|
for rep in self.getreports(names=names):
|
2009-05-23 01:56:05 +08:00
|
|
|
colitem = rep.getnode()
|
|
|
|
if not inamepart or inamepart in colitem.listnames():
|
2009-04-07 18:48:57 +08:00
|
|
|
l.append(rep)
|
|
|
|
if not l:
|
|
|
|
raise ValueError("could not find test report matching %r: no test reports at all!" %
|
|
|
|
(inamepart,))
|
|
|
|
if len(l) > 1:
|
|
|
|
raise ValueError("found more than one testreport matching %r: %s" %(
|
|
|
|
inamepart, l))
|
|
|
|
return l[0]
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-06-09 00:31:10 +08:00
|
|
|
def getfailures(self, names='pytest_runtest_logreport pytest_collectreport'):
|
2009-05-20 01:25:21 +08:00
|
|
|
return [rep for rep in self.getreports(names) if rep.failed]
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def getfailedcollections(self):
|
2009-05-20 01:25:21 +08:00
|
|
|
return self.getfailures('pytest_collectreport')
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def listoutcomes(self):
|
|
|
|
passed = []
|
|
|
|
skipped = []
|
|
|
|
failed = []
|
2009-06-09 00:31:10 +08:00
|
|
|
for rep in self.getreports("pytest_runtest_logreport"):
|
2009-04-07 18:48:57 +08:00
|
|
|
if rep.passed:
|
2009-06-09 22:08:34 +08:00
|
|
|
if rep.when == "call":
|
|
|
|
passed.append(rep)
|
2009-04-07 18:48:57 +08:00
|
|
|
elif rep.skipped:
|
|
|
|
skipped.append(rep)
|
|
|
|
elif rep.failed:
|
|
|
|
failed.append(rep)
|
2009-02-27 18:18:27 +08:00
|
|
|
return passed, skipped, failed
|
|
|
|
|
|
|
|
def countoutcomes(self):
|
2009-09-05 03:47:49 +08:00
|
|
|
return [len(x) for x in self.listoutcomes()]
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def assertoutcome(self, passed=0, skipped=0, failed=0):
|
|
|
|
realpassed, realskipped, realfailed = self.listoutcomes()
|
|
|
|
assert passed == len(realpassed)
|
|
|
|
assert skipped == len(realskipped)
|
|
|
|
assert failed == len(realfailed)
|
|
|
|
|
|
|
|
def clear(self):
|
2009-05-20 01:25:21 +08:00
|
|
|
self.hookrecorder.calls[:] = []
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def unregister(self):
|
2009-12-29 19:36:17 +08:00
|
|
|
self.registry.unregister(self)
|
2009-05-20 01:25:21 +08:00
|
|
|
self.hookrecorder.finish_recording()
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
class LineComp:
|
|
|
|
def __init__(self):
|
2009-08-21 02:47:39 +08:00
|
|
|
self.stringio = py.io.TextIO()
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
def assert_contains_lines(self, lines2):
|
|
|
|
""" assert that lines2 are contained (linearly) in lines1.
|
|
|
|
return a list of extralines found.
|
|
|
|
"""
|
|
|
|
__tracebackhide__ = True
|
|
|
|
val = self.stringio.getvalue()
|
|
|
|
self.stringio.truncate(0) # remove what we got
|
|
|
|
lines1 = val.split("\n")
|
|
|
|
return LineMatcher(lines1).fnmatch_lines(lines2)
|
|
|
|
|
|
|
|
class LineMatcher:
|
|
|
|
def __init__(self, lines):
|
|
|
|
self.lines = lines
|
|
|
|
|
|
|
|
def str(self):
|
|
|
|
return "\n".join(self.lines)
|
|
|
|
|
|
|
|
def fnmatch_lines(self, lines2):
|
|
|
|
if isinstance(lines2, str):
|
|
|
|
lines2 = py.code.Source(lines2)
|
|
|
|
if isinstance(lines2, py.code.Source):
|
|
|
|
lines2 = lines2.strip().lines
|
|
|
|
|
|
|
|
from fnmatch import fnmatch
|
|
|
|
__tracebackhide__ = True
|
|
|
|
lines1 = self.lines[:]
|
|
|
|
nextline = None
|
|
|
|
extralines = []
|
|
|
|
for line in lines2:
|
|
|
|
nomatchprinted = False
|
|
|
|
while lines1:
|
|
|
|
nextline = lines1.pop(0)
|
|
|
|
if line == nextline:
|
2009-08-29 01:16:15 +08:00
|
|
|
print_("exact match:", repr(line))
|
2009-02-27 18:18:27 +08:00
|
|
|
break
|
|
|
|
elif fnmatch(nextline, line):
|
2009-08-29 01:16:15 +08:00
|
|
|
print_("fnmatch:", repr(line))
|
|
|
|
print_(" with:", repr(nextline))
|
2009-02-27 18:18:27 +08:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
if not nomatchprinted:
|
2009-08-29 01:16:15 +08:00
|
|
|
print_("nomatch:", repr(line))
|
2009-02-27 18:18:27 +08:00
|
|
|
nomatchprinted = True
|
2009-08-29 01:16:15 +08:00
|
|
|
print_(" and:", repr(nextline))
|
2009-02-27 18:18:27 +08:00
|
|
|
extralines.append(nextline)
|
|
|
|
else:
|
|
|
|
if line != nextline:
|
|
|
|
#__tracebackhide__ = True
|
|
|
|
raise AssertionError("expected line not found: %r" % line)
|
|
|
|
extralines.extend(lines1)
|
|
|
|
return extralines
|