2009-07-09 01:18:26 +08:00
|
|
|
""" log invocations of extension hooks to a file. """
|
2009-05-21 05:12:37 +08:00
|
|
|
import py
|
|
|
|
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption("--hooklog", dest="hooklog", default=None,
|
|
|
|
help="write hook calls to the given file.")
|
|
|
|
|
|
|
|
def pytest_configure(config):
|
|
|
|
hooklog = config.getvalue("hooklog")
|
|
|
|
if hooklog:
|
2009-08-10 05:51:25 +08:00
|
|
|
config._hooklogfile = open(hooklog, 'w', 0)
|
|
|
|
config._hooklog_oldperformcall = config.hook._performcall
|
|
|
|
config.hook._performcall = (lambda name, multicall:
|
|
|
|
logged_call(name=name, multicall=multicall, config=config))
|
|
|
|
|
|
|
|
def logged_call(name, multicall, config):
|
|
|
|
f = config._hooklogfile
|
|
|
|
f.write("%s(**%s)\n" % (name, multicall.kwargs))
|
|
|
|
try:
|
|
|
|
res = config._hooklog_oldperformcall(name=name, multicall=multicall)
|
|
|
|
except:
|
|
|
|
f.write("-> exception")
|
|
|
|
raise
|
|
|
|
f.write("-> %r" % (res,))
|
|
|
|
return res
|
2009-05-21 05:12:37 +08:00
|
|
|
|
|
|
|
def pytest_unconfigure(config):
|
2009-08-10 05:51:25 +08:00
|
|
|
try:
|
|
|
|
del config.hook.__dict__['_performcall']
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2009-05-21 05:12:37 +08:00
|
|
|
|
|
|
|
# ===============================================================================
|
|
|
|
# plugin tests
|
|
|
|
# ===============================================================================
|
|
|
|
|
|
|
|
def test_functional(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_pass():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
testdir.runpytest("--hooklog=hook.log")
|
|
|
|
s = testdir.tmpdir.join("hook.log").read()
|
2009-05-23 03:53:26 +08:00
|
|
|
assert s.find("pytest_sessionstart") != -1
|
2009-05-21 05:12:37 +08:00
|
|
|
assert s.find("ItemTestReport") != -1
|
2009-05-23 03:53:26 +08:00
|
|
|
assert s.find("sessionfinish") != -1
|