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
|
2017-07-17 07:25:09 +08:00
|
|
|
import os
|
|
|
|
import 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.
|
|
|
|
"""
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
def __init__(self, option_strings, dest=None, default=False, help=None):
|
2017-06-01 04:55:30 +08:00
|
|
|
super(HelpAction, self).__init__(
|
|
|
|
option_strings=option_strings,
|
|
|
|
dest=dest,
|
|
|
|
const=True,
|
|
|
|
default=default,
|
|
|
|
nargs=0,
|
2018-05-23 22:48:46 +08:00
|
|
|
help=help,
|
|
|
|
)
|
2017-06-01 04:55:30 +08:00
|
|
|
|
|
|
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
if getattr(parser._parser, "after_preparse", False):
|
2017-06-01 04:55:30 +08:00
|
|
|
raise PrintHelp
|
|
|
|
|
2009-08-19 21:45:01 +08:00
|
|
|
|
|
|
|
def pytest_addoption(parser):
|
2018-05-23 22:48:46 +08:00
|
|
|
group = parser.getgroup("debugconfig")
|
|
|
|
group.addoption(
|
|
|
|
"--version",
|
|
|
|
action="store_true",
|
|
|
|
help="display pytest lib version and import information.",
|
|
|
|
)
|
2016-07-24 00:09:42 +08:00
|
|
|
group._addoption(
|
2018-05-23 22:48:46 +08:00
|
|
|
"-h",
|
|
|
|
"--help",
|
|
|
|
action=HelpAction,
|
|
|
|
dest="help",
|
|
|
|
help="show help message and configuration info",
|
|
|
|
)
|
|
|
|
group._addoption(
|
|
|
|
"-p",
|
|
|
|
action="append",
|
|
|
|
dest="plugins",
|
|
|
|
default=[],
|
|
|
|
metavar="name",
|
|
|
|
help="early-load given plugin (multi-allowed). "
|
|
|
|
"To avoid loading of plugins, use the `no:` prefix, e.g. "
|
|
|
|
"`no:doctest`.",
|
|
|
|
)
|
|
|
|
group.addoption(
|
|
|
|
"--traceconfig",
|
|
|
|
"--trace-config",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="trace considerations of conftest.py files.",
|
|
|
|
),
|
|
|
|
group.addoption(
|
|
|
|
"--debug",
|
|
|
|
action="store_true",
|
|
|
|
dest="debug",
|
|
|
|
default=False,
|
|
|
|
help="store internal tracing debug information in 'pytestdebug.log'.",
|
|
|
|
)
|
|
|
|
group._addoption(
|
|
|
|
"-o",
|
|
|
|
"--override-ini",
|
|
|
|
dest="override_ini",
|
2016-07-24 00:09:42 +08:00
|
|
|
action="append",
|
2018-05-23 22:48:46 +08:00
|
|
|
help='override ini option with "option=value" style, e.g. `-o xfail_strict=True -o cache_dir=cache`.',
|
|
|
|
)
|
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")
|
2018-05-23 22:48:46 +08:00
|
|
|
debugfile = open(path, "w")
|
|
|
|
debugfile.write(
|
|
|
|
"versions pytest-%s, py-%s, "
|
|
|
|
"python-%s\ncwd=%s\nargs=%s\n\n"
|
|
|
|
% (
|
|
|
|
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()
|
2018-05-23 22:48:46 +08:00
|
|
|
sys.stderr.write("wrote pytestdebug information to %s\n" % debugfile.name)
|
2015-04-22 22:33:20 +08:00
|
|
|
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
|
|
|
|
2017-07-17 07:25:09 +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__)
|
2018-05-23 22:48:46 +08:00
|
|
|
sys.stderr.write(
|
|
|
|
"This is pytest version %s, imported from %s\n" % (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
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2010-10-28 04:29:01 +08:00
|
|
|
def showhelp(config):
|
2018-05-23 22:48:46 +08:00
|
|
|
reporter = config.pluginmanager.get_plugin("terminalreporter")
|
2015-09-01 03:54:12 +08:00
|
|
|
tw = reporter._tw
|
2010-10-28 04:29:01 +08:00
|
|
|
tw.write(config._parser.optparser.format_help())
|
|
|
|
tw.line()
|
|
|
|
tw.line()
|
2018-05-23 22:48:46 +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)
|
2017-07-17 07:25:08 +08:00
|
|
|
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"),
|
2018-05-23 22:48:46 +08:00
|
|
|
("PYTEST_DEBUG", "set to enable debug tracing of pytest's internals"),
|
2015-09-29 05:34:16 +08:00
|
|
|
]
|
|
|
|
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")
|
2018-05-23 22:48:46 +08:00
|
|
|
tw.line(
|
|
|
|
"(shown according to specified file_or_dir or current dir "
|
|
|
|
"if not specified; fixtures with leading '_' are only shown "
|
|
|
|
"with the '-v' option"
|
|
|
|
)
|
2015-09-29 05:25:20 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
for warningreport in reporter.stats.get("warnings", []):
|
2015-09-01 03:54:12 +08:00
|
|
|
tw.line("warning : " + warningreport.message, red=True)
|
2010-11-01 00:41:58 +08:00
|
|
|
return
|
|
|
|
|
2009-08-19 21:45:01 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
conftest_options = [("pytest_plugins", "list of plugin names to load")]
|
2009-12-29 17:59:01 +08:00
|
|
|
|
2017-07-17 07:25:09 +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:
|
2018-05-23 22:48:46 +08:00
|
|
|
loc = getattr(plugin, "__file__", repr(plugin))
|
2011-01-13 02:39:36 +08:00
|
|
|
content = "%s-%s at %s" % (dist.project_name, dist.version, loc)
|
|
|
|
lines.append(" " + content)
|
|
|
|
return lines
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2010-01-13 04:43:25 +08:00
|
|
|
def pytest_report_header(config):
|
|
|
|
lines = []
|
|
|
|
if config.option.debug or config.option.traceconfig:
|
2018-05-23 22:48:46 +08:00
|
|
|
lines.append("using: pytest-%s pylib-%s" % (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:
|
2018-05-23 22:48:46 +08:00
|
|
|
if hasattr(plugin, "__file__"):
|
2010-12-06 23:54:42 +08:00
|
|
|
r = plugin.__file__
|
|
|
|
else:
|
|
|
|
r = repr(plugin)
|
2017-07-17 07:25:08 +08:00
|
|
|
lines.append(" %-20s: %s" % (name, r))
|
2010-01-13 04:43:25 +08:00
|
|
|
return lines
|