2010-11-06 18:38:53 +08:00
|
|
|
""" version info, help messages, tracing configuration. """
|
2009-08-19 21:45:01 +08:00
|
|
|
import py
|
2010-10-28 04:29:01 +08:00
|
|
|
import pytest
|
2014-10-02 21:25:42 +08:00
|
|
|
import os, sys
|
2009-08-19 21:45:01 +08:00
|
|
|
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
group = parser.getgroup('debugconfig')
|
2010-07-27 03:15:15 +08:00
|
|
|
group.addoption('--version', action="store_true",
|
2010-10-31 01:23:50 +08:00
|
|
|
help="display pytest lib version and import information.")
|
2010-10-28 04:29:01 +08:00
|
|
|
group._addoption("-h", "--help", action="store_true", dest="help",
|
|
|
|
help="show help message and configuration info")
|
2010-01-03 19:41:29 +08:00
|
|
|
group._addoption('-p', action="append", dest="plugins", default = [],
|
2010-07-27 03:15:15 +08:00
|
|
|
metavar="name",
|
2014-01-20 05:05:14 +08:00
|
|
|
help="early-load given plugin (multi-allowed). "
|
|
|
|
"To avoid loading of plugins, use the `no:` prefix, e.g. "
|
|
|
|
"`no:doctest`.")
|
2013-08-01 22:49:26 +08:00
|
|
|
group.addoption('--traceconfig', '--trace-config',
|
|
|
|
action="store_true", default=False,
|
2010-01-13 04:43:25 +08:00
|
|
|
help="trace considerations of conftest.py files."),
|
|
|
|
group.addoption('--debug',
|
|
|
|
action="store_true", dest="debug", default=False,
|
2011-07-15 01:11:50 +08:00
|
|
|
help="store internal tracing debug information in 'pytestdebug.log'.")
|
2016-06-25 22:12:42 +08:00
|
|
|
# support for "--overwrite-ini ININAME=INIVALUE" to override values from the ini file
|
|
|
|
# Example '-o xfail_strict=True'.
|
|
|
|
group._addoption('-o', '--override-ini', nargs='*', dest="override_ini", action="append",
|
|
|
|
help="overrides ini values which do not have a separate command-line flag")
|
2011-07-15 01:11:50 +08:00
|
|
|
|
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@pytest.hookimpl(hookwrapper=True)
|
2014-10-09 02:23:40 +08:00
|
|
|
def pytest_cmdline_parse():
|
|
|
|
outcome = yield
|
|
|
|
config = outcome.get_result()
|
2011-07-15 01:11:50 +08:00
|
|
|
if config.option.debug:
|
|
|
|
path = os.path.abspath("pytestdebug.log")
|
2015-04-22 22:33:20 +08:00
|
|
|
debugfile = open(path, 'w')
|
|
|
|
debugfile.write("versions pytest-%s, py-%s, "
|
2014-10-09 02:23:40 +08:00
|
|
|
"python-%s\ncwd=%s\nargs=%s\n\n" %(
|
|
|
|
pytest.__version__, py.__version__,
|
|
|
|
".".join(map(str, sys.version_info)),
|
2011-07-15 01:11:50 +08:00
|
|
|
os.getcwd(), config._origargs))
|
2015-04-26 00:15:39 +08:00
|
|
|
config.trace.root.setwriter(debugfile.write)
|
|
|
|
undo_tracing = config.pluginmanager.enable_tracing()
|
2011-07-15 01:11:50 +08:00
|
|
|
sys.stderr.write("writing pytestdebug information to %s\n" % path)
|
2015-04-22 22:33:20 +08:00
|
|
|
def unset_tracing():
|
|
|
|
debugfile.close()
|
|
|
|
sys.stderr.write("wrote pytestdebug information to %s\n" %
|
|
|
|
debugfile.name)
|
|
|
|
config.trace.root.setwriter(None)
|
2015-04-26 00:15:39 +08:00
|
|
|
undo_tracing()
|
2015-04-22 22:33:20 +08:00
|
|
|
config.add_cleanup(unset_tracing)
|
2010-01-13 04:43:25 +08:00
|
|
|
|
2010-09-26 22:23:43 +08:00
|
|
|
def pytest_cmdline_main(config):
|
2009-08-19 21:45:01 +08:00
|
|
|
if config.option.version:
|
2010-11-13 18:10:45 +08:00
|
|
|
p = py.path.local(pytest.__file__)
|
2014-01-18 19:31:33 +08:00
|
|
|
sys.stderr.write("This is pytest version %s, imported from %s\n" %
|
2010-11-01 01:57:44 +08:00
|
|
|
(pytest.__version__, p))
|
2011-01-13 02:39:36 +08:00
|
|
|
plugininfo = getpluginversioninfo(config)
|
|
|
|
if plugininfo:
|
|
|
|
for line in plugininfo:
|
|
|
|
sys.stderr.write(line + "\n")
|
2010-09-26 22:23:43 +08:00
|
|
|
return 0
|
2010-10-28 04:29:01 +08:00
|
|
|
elif config.option.help:
|
2015-04-22 22:33:20 +08:00
|
|
|
config._do_configure()
|
2010-10-28 04:29:01 +08:00
|
|
|
showhelp(config)
|
2015-04-22 22:33:20 +08:00
|
|
|
config._ensure_unconfigure()
|
2010-09-26 22:23:43 +08:00
|
|
|
return 0
|
|
|
|
|
2010-10-28 04:29:01 +08:00
|
|
|
def showhelp(config):
|
2015-09-01 03:54:12 +08:00
|
|
|
reporter = config.pluginmanager.get_plugin('terminalreporter')
|
|
|
|
tw = reporter._tw
|
2010-10-28 04:29:01 +08:00
|
|
|
tw.write(config._parser.optparser.format_help())
|
|
|
|
tw.line()
|
|
|
|
tw.line()
|
2010-11-01 00:41:58 +08:00
|
|
|
#tw.sep( "=", "config file settings")
|
2010-11-25 19:11:10 +08:00
|
|
|
tw.line("[pytest] ini-options in the next "
|
|
|
|
"pytest.ini|tox.ini|setup.cfg file:")
|
2010-10-28 04:29:01 +08:00
|
|
|
tw.line()
|
|
|
|
|
2010-11-07 23:10:22 +08:00
|
|
|
for name in config._parser._ininames:
|
|
|
|
help, type, default = config._parser._inidict[name]
|
2010-11-05 06:21:26 +08:00
|
|
|
if type is None:
|
|
|
|
type = "string"
|
|
|
|
spec = "%s (%s)" % (name, type)
|
|
|
|
line = " %-24s %s" %(spec, help)
|
2009-08-19 21:45:01 +08:00
|
|
|
tw.line(line[:tw.fullwidth])
|
2010-10-28 04:29:01 +08:00
|
|
|
|
2015-09-29 05:34:16 +08:00
|
|
|
tw.line()
|
|
|
|
tw.line("environment variables:")
|
|
|
|
vars = [
|
|
|
|
("PYTEST_ADDOPTS", "extra command line options"),
|
|
|
|
("PYTEST_PLUGINS", "comma-separated plugins to load during startup"),
|
|
|
|
("PYTEST_DEBUG", "set to enable debug tracing of pytest's internals")
|
|
|
|
]
|
|
|
|
for name, help in vars:
|
|
|
|
tw.line(" %-24s %s" % (name, help))
|
|
|
|
tw.line()
|
|
|
|
tw.line()
|
|
|
|
|
2016-06-21 22:16:57 +08:00
|
|
|
tw.line("to see available markers type: pytest --markers")
|
|
|
|
tw.line("to see available fixtures type: pytest --fixtures")
|
2014-01-22 17:24:22 +08:00
|
|
|
tw.line("(shown according to specified file_or_dir or current dir "
|
|
|
|
"if not specified)")
|
2015-09-29 05:25:20 +08:00
|
|
|
|
2015-09-01 03:54:12 +08:00
|
|
|
for warningreport in reporter.stats.get('warnings', []):
|
|
|
|
tw.line("warning : " + warningreport.message, red=True)
|
2010-11-01 00:41:58 +08:00
|
|
|
return
|
|
|
|
|
2009-08-19 21:45:01 +08:00
|
|
|
|
2010-11-01 00:41:58 +08:00
|
|
|
conftest_options = [
|
2009-08-19 21:45:01 +08:00
|
|
|
('pytest_plugins', 'list of plugin names to load'),
|
2010-11-01 00:41:58 +08:00
|
|
|
]
|
2009-12-29 17:59:01 +08:00
|
|
|
|
2011-01-13 02:39:36 +08:00
|
|
|
def getpluginversioninfo(config):
|
|
|
|
lines = []
|
2015-05-06 03:53:04 +08:00
|
|
|
plugininfo = config.pluginmanager.list_plugin_distinfo()
|
2011-01-13 02:39:36 +08:00
|
|
|
if plugininfo:
|
|
|
|
lines.append("setuptools registered plugins:")
|
2015-05-06 03:53:04 +08:00
|
|
|
for plugin, dist in plugininfo:
|
2011-01-13 02:39:36 +08:00
|
|
|
loc = getattr(plugin, '__file__', repr(plugin))
|
|
|
|
content = "%s-%s at %s" % (dist.project_name, dist.version, loc)
|
|
|
|
lines.append(" " + content)
|
|
|
|
return lines
|
|
|
|
|
2010-01-13 04:43:25 +08:00
|
|
|
def pytest_report_header(config):
|
|
|
|
lines = []
|
|
|
|
if config.option.debug or config.option.traceconfig:
|
2010-11-05 06:21:26 +08:00
|
|
|
lines.append("using: pytest-%s pylib-%s" %
|
2010-10-28 04:29:01 +08:00
|
|
|
(pytest.__version__,py.__version__))
|
2011-01-13 02:39:36 +08:00
|
|
|
|
|
|
|
verinfo = getpluginversioninfo(config)
|
|
|
|
if verinfo:
|
|
|
|
lines.extend(verinfo)
|
2011-11-08 02:08:41 +08:00
|
|
|
|
2010-01-13 04:43:25 +08:00
|
|
|
if config.option.traceconfig:
|
|
|
|
lines.append("active plugins:")
|
2015-05-06 03:53:04 +08:00
|
|
|
items = config.pluginmanager.list_name_plugin()
|
2010-01-13 04:43:25 +08:00
|
|
|
for name, plugin in items:
|
2010-12-06 23:54:42 +08:00
|
|
|
if hasattr(plugin, '__file__'):
|
|
|
|
r = plugin.__file__
|
|
|
|
else:
|
|
|
|
r = repr(plugin)
|
|
|
|
lines.append(" %-20s: %s" %(name, r))
|
2010-01-13 04:43:25 +08:00
|
|
|
return lines
|