2010-11-06 18:38:53 +08:00
|
|
|
""" version info, help messages, tracing configuration. """
|
2017-03-17 09:21:30 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
2009-08-19 21:45:01 +08:00
|
|
|
import py
|
2010-10-28 04:29:01 +08:00
|
|
|
import pytest
|
2017-06-01 04:55:30 +08:00
|
|
|
from _pytest.config import PrintHelp
|
2014-10-02 21:25:42 +08:00
|
|
|
import os, sys
|
2017-06-01 04:55:30 +08:00
|
|
|
from argparse import Action
|
|
|
|
|
|
|
|
|
|
|
|
class HelpAction(Action):
|
2017-06-02 03:24:43 +08:00
|
|
|
"""This is an argparse Action that will raise an exception in
|
|
|
|
order to skip the rest of the argument parsing when --help is passed.
|
|
|
|
This prevents argparse from quitting due to missing required arguments
|
|
|
|
when any are defined, for example by ``pytest_addoption``.
|
|
|
|
This is similar to the way that the builtin argparse --help option is
|
|
|
|
implemented by raising SystemExit.
|
|
|
|
"""
|
|
|
|
|
2017-06-01 04:55:30 +08:00
|
|
|
def __init__(self,
|
|
|
|
option_strings,
|
|
|
|
dest=None,
|
|
|
|
default=False,
|
|
|
|
help=None):
|
|
|
|
super(HelpAction, self).__init__(
|
|
|
|
option_strings=option_strings,
|
|
|
|
dest=dest,
|
|
|
|
const=True,
|
|
|
|
default=default,
|
|
|
|
nargs=0,
|
|
|
|
help=help)
|
|
|
|
|
|
|
|
def __call__(self, parser, namespace, values, option_string=None):
|
|
|
|
setattr(namespace, self.dest, self.const)
|
|
|
|
|
|
|
|
# We should only skip the rest of the parsing after preparse is done
|
|
|
|
if getattr(parser._parser, 'after_preparse', False):
|
|
|
|
raise PrintHelp
|
|
|
|
|
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.")
|
2017-06-01 04:55:30 +08:00
|
|
|
group._addoption("-h", "--help", action=HelpAction, dest="help",
|
2010-10-28 04:29:01 +08:00
|
|
|
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-07-24 00:09:42 +08:00
|
|
|
group._addoption(
|
|
|
|
'-o', '--override-ini', nargs='*', dest="override_ini",
|
|
|
|
action="append",
|
2016-12-01 20:20:42 +08:00
|
|
|
help="override config option with option=value style, e.g. `-o xfail_strict=True`.")
|
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" %(
|
2017-07-17 07:25:07 +08:00
|
|
|
pytest.__version__, py.__version__,
|
|
|
|
".".join(map(str, sys.version_info)),
|
|
|
|
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)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
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()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
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()
|
2016-10-04 21:41:09 +08:00
|
|
|
tw.line("[pytest] ini-options in the first "
|
|
|
|
"pytest.ini|tox.ini|setup.cfg file found:")
|
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
|