2010-11-07 17:19:58 +08:00
|
|
|
""" core implementation of testing process: init, session, runtest loop. """
|
2017-03-17 09:21:30 +08:00
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
2016-11-28 07:55:49 +08:00
|
|
|
import functools
|
2015-11-27 22:43:01 +08:00
|
|
|
import os
|
|
|
|
import sys
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
import _pytest
|
|
|
|
import _pytest._code
|
2008-08-16 23:26:59 +08:00
|
|
|
import py
|
2012-10-18 19:52:32 +08:00
|
|
|
try:
|
|
|
|
from collections import MutableMapping as MappingMixin
|
|
|
|
except ImportError:
|
|
|
|
from UserDict import DictMixin as MappingMixin
|
2012-06-25 23:35:33 +08:00
|
|
|
|
2017-03-16 01:00:59 +08:00
|
|
|
from _pytest.config import directory_arg, UsageError, hookimpl
|
|
|
|
from _pytest.runner import collect_one_node, exit
|
2012-07-24 18:10:04 +08:00
|
|
|
|
2010-11-13 18:10:45 +08:00
|
|
|
tracebackcutdir = py.path.local(_pytest.__file__).dirpath()
|
2010-10-10 19:48:48 +08:00
|
|
|
|
2010-11-01 07:27:12 +08:00
|
|
|
# exitcodes for the command line
|
|
|
|
EXIT_OK = 0
|
|
|
|
EXIT_TESTSFAILED = 1
|
|
|
|
EXIT_INTERRUPTED = 2
|
|
|
|
EXIT_INTERNALERROR = 3
|
2012-05-18 05:11:23 +08:00
|
|
|
EXIT_USAGEERROR = 4
|
2015-07-05 01:42:22 +08:00
|
|
|
EXIT_NOTESTSCOLLECTED = 5
|
2010-11-01 07:27:12 +08:00
|
|
|
|
2017-03-16 01:00:59 +08:00
|
|
|
|
2010-10-10 19:48:48 +08:00
|
|
|
def pytest_addoption(parser):
|
2010-11-05 06:21:26 +08:00
|
|
|
parser.addini("norecursedirs", "directory patterns to avoid for recursion",
|
2017-07-17 07:25:07 +08:00
|
|
|
type="args", default=['.*', 'build', 'dist', 'CVS', '_darcs', '{arch}', '*.egg', 'venv'])
|
2017-07-17 07:25:10 +08:00
|
|
|
parser.addini("testpaths", "directories to search for tests when no files or directories are given in the "
|
|
|
|
"command line.",
|
2017-07-17 07:25:07 +08:00
|
|
|
type="args", default=[])
|
2017-07-17 07:25:09 +08:00
|
|
|
# parser.addini("dirpatterns",
|
2010-11-06 16:58:04 +08:00
|
|
|
# "patterns specifying possible locations of test files",
|
|
|
|
# type="linelist", default=["**/test_*.txt",
|
|
|
|
# "**/test_*.py", "**/*_test.py"]
|
2017-07-17 07:25:09 +08:00
|
|
|
# )
|
2010-10-10 19:48:48 +08:00
|
|
|
group = parser.getgroup("general", "running and selection options")
|
2016-07-20 23:15:29 +08:00
|
|
|
group._addoption('-x', '--exitfirst', action="store_const",
|
2017-07-17 07:25:07 +08:00
|
|
|
dest="maxfail", const=1,
|
|
|
|
help="exit instantly on first error or failed test."),
|
2010-10-10 19:48:48 +08:00
|
|
|
group._addoption('--maxfail', metavar="num",
|
2017-07-17 07:25:07 +08:00
|
|
|
action="store", type=int, dest="maxfail", default=0,
|
|
|
|
help="exit after first num failures or errors.")
|
2011-11-12 06:56:11 +08:00
|
|
|
group._addoption('--strict', action="store_true",
|
2017-07-17 07:25:07 +08:00
|
|
|
help="marks not registered in configuration file raise errors.")
|
2014-06-28 18:03:55 +08:00
|
|
|
group._addoption("-c", metavar="file", type=str, dest="inifilename",
|
2017-07-17 07:25:10 +08:00
|
|
|
help="load configuration from `file` instead of trying to locate one of the implicit "
|
|
|
|
"configuration files.")
|
2016-06-20 21:05:50 +08:00
|
|
|
group._addoption("--continue-on-collection-errors", action="store_true",
|
2017-07-17 07:25:07 +08:00
|
|
|
default=False, dest="continue_on_collection_errors",
|
|
|
|
help="Force test execution even if collection errors occur.")
|
2011-11-12 06:56:11 +08:00
|
|
|
|
2010-10-10 19:48:48 +08:00
|
|
|
group = parser.getgroup("collect", "collection")
|
2013-08-02 15:52:40 +08:00
|
|
|
group.addoption('--collectonly', '--collect-only', action="store_true",
|
2017-07-17 07:25:07 +08:00
|
|
|
help="only collect tests, don't execute them."),
|
2010-11-07 05:17:33 +08:00
|
|
|
group.addoption('--pyargs', action="store_true",
|
2017-07-17 07:25:07 +08:00
|
|
|
help="try to interpret all arguments as python packages.")
|
2010-10-10 19:48:48 +08:00
|
|
|
group.addoption("--ignore", action="append", metavar="path",
|
2017-07-17 07:25:07 +08:00
|
|
|
help="ignore path during collection (multi-allowed).")
|
2013-08-01 22:49:26 +08:00
|
|
|
# when changing this to --conf-cut-dir, config.py Conftest.setinitial
|
|
|
|
# needs upgrading as well
|
2010-10-10 19:48:48 +08:00
|
|
|
group.addoption('--confcutdir', dest="confcutdir", default=None,
|
2017-07-17 07:25:07 +08:00
|
|
|
metavar="dir", type=functools.partial(directory_arg, optname="--confcutdir"),
|
|
|
|
help="only load conftest.py's relative to specified dir.")
|
2015-06-23 13:53:32 +08:00
|
|
|
group.addoption('--noconftest', action="store_true",
|
2017-07-17 07:25:07 +08:00
|
|
|
dest="noconftest", default=False,
|
|
|
|
help="Don't load any conftest.py files.")
|
2016-07-25 18:40:57 +08:00
|
|
|
group.addoption('--keepduplicates', '--keep-duplicates', action="store_true",
|
2017-07-17 07:25:07 +08:00
|
|
|
dest="keepduplicates", default=False,
|
|
|
|
help="Keep duplicate tests.")
|
2017-07-12 00:52:16 +08:00
|
|
|
group.addoption('--collect-in-virtualenv', action='store_true',
|
2017-07-20 04:09:05 +08:00
|
|
|
dest='collect_in_virtualenv', default=False,
|
|
|
|
help="Don't ignore tests in a local virtualenv directory")
|
2010-10-10 19:48:48 +08:00
|
|
|
|
|
|
|
group = parser.getgroup("debugconfig",
|
2017-07-17 07:25:07 +08:00
|
|
|
"test session debugging and configuration")
|
2010-10-10 19:48:48 +08:00
|
|
|
group.addoption('--basetemp', dest="basetemp", default=None, metavar="dir",
|
2017-07-17 07:25:07 +08:00
|
|
|
help="base temporary directory for this test run.")
|
2010-10-10 19:48:48 +08:00
|
|
|
|
2010-11-01 00:41:58 +08:00
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
def pytest_namespace():
|
2017-03-28 16:28:43 +08:00
|
|
|
"""keeping this one works around a deeper startup issue in pytest
|
|
|
|
|
|
|
|
i tried to find it for a while but the amount of time turned unsustainable,
|
|
|
|
so i put a hack in to revisit later
|
|
|
|
"""
|
|
|
|
return {}
|
2011-11-08 02:08:41 +08:00
|
|
|
|
2016-07-20 23:15:29 +08:00
|
|
|
|
2010-10-10 19:48:48 +08:00
|
|
|
def pytest_configure(config):
|
2017-07-17 07:25:09 +08:00
|
|
|
__import__('pytest').config = config # compatibiltiy
|
2016-07-20 23:15:29 +08:00
|
|
|
|
2010-10-10 19:48:48 +08:00
|
|
|
|
2011-05-27 08:09:42 +08:00
|
|
|
def wrap_session(config, doit):
|
|
|
|
"""Skeleton command line program"""
|
2010-11-01 07:27:12 +08:00
|
|
|
session = Session(config)
|
|
|
|
session.exitstatus = EXIT_OK
|
2011-06-01 20:54:34 +08:00
|
|
|
initstate = 0
|
2010-11-01 07:27:12 +08:00
|
|
|
try:
|
2012-05-18 05:11:23 +08:00
|
|
|
try:
|
2015-04-22 22:33:20 +08:00
|
|
|
config._do_configure()
|
2012-05-18 05:11:23 +08:00
|
|
|
initstate = 1
|
|
|
|
config.hook.pytest_sessionstart(session=session)
|
|
|
|
initstate = 2
|
2015-08-28 01:22:22 +08:00
|
|
|
session.exitstatus = doit(config, session) or 0
|
2017-03-16 01:00:59 +08:00
|
|
|
except UsageError:
|
2015-04-28 17:54:45 +08:00
|
|
|
raise
|
2012-05-18 05:11:23 +08:00
|
|
|
except KeyboardInterrupt:
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = _pytest._code.ExceptionInfo()
|
2017-03-16 01:00:59 +08:00
|
|
|
if initstate < 2 and isinstance(excinfo.value, exit.Exception):
|
2016-07-24 20:13:43 +08:00
|
|
|
sys.stderr.write('{0}: {1}\n'.format(
|
2016-07-25 14:16:32 +08:00
|
|
|
excinfo.typename, excinfo.value.msg))
|
2012-05-18 05:11:23 +08:00
|
|
|
config.hook.pytest_keyboard_interrupt(excinfo=excinfo)
|
|
|
|
session.exitstatus = EXIT_INTERRUPTED
|
|
|
|
except:
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = _pytest._code.ExceptionInfo()
|
2013-09-30 19:14:14 +08:00
|
|
|
config.notify_exception(excinfo, config.option)
|
2012-05-18 05:11:23 +08:00
|
|
|
session.exitstatus = EXIT_INTERNALERROR
|
|
|
|
if excinfo.errisinstance(SystemExit):
|
|
|
|
sys.stderr.write("mainloop: caught Spurious SystemExit!\n")
|
2015-08-28 01:22:22 +08:00
|
|
|
|
2012-05-18 05:11:23 +08:00
|
|
|
finally:
|
2014-04-02 21:34:36 +08:00
|
|
|
excinfo = None # Explicitly break reference cycle.
|
2013-06-10 16:09:28 +08:00
|
|
|
session.startdir.chdir()
|
2012-05-18 05:11:23 +08:00
|
|
|
if initstate >= 2:
|
2013-02-14 19:13:04 +08:00
|
|
|
config.hook.pytest_sessionfinish(
|
|
|
|
session=session,
|
|
|
|
exitstatus=session.exitstatus)
|
2015-04-22 22:33:20 +08:00
|
|
|
config._ensure_unconfigure()
|
2010-11-01 07:27:12 +08:00
|
|
|
return session.exitstatus
|
|
|
|
|
2017-03-16 01:00:59 +08:00
|
|
|
|
2011-05-27 08:09:42 +08:00
|
|
|
def pytest_cmdline_main(config):
|
|
|
|
return wrap_session(config, _main)
|
|
|
|
|
2017-03-16 01:00:59 +08:00
|
|
|
|
2011-05-27 08:09:42 +08:00
|
|
|
def _main(config, session):
|
|
|
|
""" default command line protocol for initialization, session,
|
|
|
|
running tests and reporting. """
|
|
|
|
config.hook.pytest_collection(session=session)
|
|
|
|
config.hook.pytest_runtestloop(session=session)
|
|
|
|
|
2015-08-28 01:22:22 +08:00
|
|
|
if session.testsfailed:
|
|
|
|
return EXIT_TESTSFAILED
|
|
|
|
elif session.testscollected == 0:
|
|
|
|
return EXIT_NOTESTSCOLLECTED
|
|
|
|
|
2017-03-16 01:00:59 +08:00
|
|
|
|
2010-11-01 07:27:12 +08:00
|
|
|
def pytest_collection(session):
|
2011-05-27 08:53:47 +08:00
|
|
|
return session.perform_collect()
|
2010-10-10 19:48:48 +08:00
|
|
|
|
2017-03-16 01:00:59 +08:00
|
|
|
|
2010-11-01 07:27:12 +08:00
|
|
|
def pytest_runtestloop(session):
|
2016-06-20 21:05:50 +08:00
|
|
|
if (session.testsfailed and
|
|
|
|
not session.config.option.continue_on_collection_errors):
|
|
|
|
raise session.Interrupted(
|
|
|
|
"%d errors during collection" % session.testsfailed)
|
|
|
|
|
2010-10-10 19:48:48 +08:00
|
|
|
if session.config.option.collectonly:
|
|
|
|
return True
|
2012-09-15 21:20:49 +08:00
|
|
|
|
|
|
|
for i, item in enumerate(session.items):
|
2017-07-17 07:25:08 +08:00
|
|
|
nextitem = session.items[i + 1] if i + 1 < len(session.items) else None
|
2011-12-03 05:00:19 +08:00
|
|
|
item.config.hook.pytest_runtest_protocol(item=item, nextitem=nextitem)
|
2010-10-10 19:48:48 +08:00
|
|
|
if session.shouldstop:
|
|
|
|
raise session.Interrupted(session.shouldstop)
|
|
|
|
return True
|
|
|
|
|
2017-03-16 01:00:59 +08:00
|
|
|
|
2017-07-12 03:32:09 +08:00
|
|
|
def _in_venv(path):
|
|
|
|
"""Attempts to detect if ``path`` is the root of a Virtual Environment by
|
|
|
|
checking for the existence of the appropriate activate script"""
|
|
|
|
bindir = path.join('Scripts' if sys.platform.startswith('win') else 'bin')
|
|
|
|
if not bindir.exists():
|
|
|
|
return False
|
|
|
|
activates = ('activate', 'activate.csh', 'activate.fish',
|
|
|
|
'Activate', 'Activate.bat', 'Activate.ps1')
|
|
|
|
return any([fname.basename in activates for fname in bindir.listdir()])
|
|
|
|
|
|
|
|
|
2010-10-10 19:48:48 +08:00
|
|
|
def pytest_ignore_collect(path, config):
|
2017-06-06 10:21:29 +08:00
|
|
|
ignore_paths = config._getconftest_pathlist("collect_ignore", path=path.dirpath())
|
2010-10-10 19:48:48 +08:00
|
|
|
ignore_paths = ignore_paths or []
|
2014-04-03 04:30:45 +08:00
|
|
|
excludeopt = config.getoption("ignore")
|
2010-10-10 19:48:48 +08:00
|
|
|
if excludeopt:
|
|
|
|
ignore_paths.extend([py.path.local(x) for x in excludeopt])
|
2016-07-25 18:40:57 +08:00
|
|
|
|
2017-06-06 10:21:29 +08:00
|
|
|
if py.path.local(path) in ignore_paths:
|
2016-07-25 18:40:57 +08:00
|
|
|
return True
|
|
|
|
|
2017-07-12 03:32:09 +08:00
|
|
|
allow_in_venv = config.getoption("collect_in_virtualenv")
|
|
|
|
if _in_venv(path) and not allow_in_venv:
|
2017-07-12 00:52:16 +08:00
|
|
|
return True
|
|
|
|
|
2016-07-25 18:40:57 +08:00
|
|
|
# Skip duplicate paths.
|
|
|
|
keepduplicates = config.getoption("keepduplicates")
|
|
|
|
duplicate_paths = config.pluginmanager._duplicatepaths
|
|
|
|
if not keepduplicates:
|
|
|
|
if path in duplicate_paths:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
duplicate_paths.add(path)
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2010-10-10 19:48:48 +08:00
|
|
|
|
2017-05-18 05:16:11 +08:00
|
|
|
class FSHookProxy:
|
2015-04-26 06:10:52 +08:00
|
|
|
def __init__(self, fspath, pm, remove_mods):
|
2010-11-06 16:58:04 +08:00
|
|
|
self.fspath = fspath
|
2015-04-26 06:10:52 +08:00
|
|
|
self.pm = pm
|
|
|
|
self.remove_mods = remove_mods
|
2012-06-21 17:20:29 +08:00
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
def __getattr__(self, name):
|
2015-04-26 06:10:52 +08:00
|
|
|
x = self.pm.subset_hook_caller(name, remove_plugins=self.remove_mods)
|
2014-10-01 18:19:11 +08:00
|
|
|
self.__dict__[name] = x
|
|
|
|
return x
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2016-12-06 16:13:25 +08:00
|
|
|
class _CompatProperty(object):
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def __get__(self, obj, owner):
|
|
|
|
if obj is None:
|
|
|
|
return self
|
|
|
|
|
2017-01-20 18:25:48 +08:00
|
|
|
# TODO: reenable in the features branch
|
|
|
|
# warnings.warn(
|
|
|
|
# "usage of {owner!r}.{name} is deprecated, please use pytest.{name} instead".format(
|
|
|
|
# name=self.name, owner=type(owner).__name__),
|
|
|
|
# PendingDeprecationWarning, stacklevel=2)
|
2017-03-16 01:00:59 +08:00
|
|
|
return getattr(__import__('pytest'), self.name)
|
2016-12-06 16:13:25 +08:00
|
|
|
|
2012-06-21 17:20:29 +08:00
|
|
|
|
2012-10-18 19:52:32 +08:00
|
|
|
class NodeKeywords(MappingMixin):
|
|
|
|
def __init__(self, node):
|
2013-10-03 19:53:22 +08:00
|
|
|
self.node = node
|
|
|
|
self.parent = node.parent
|
|
|
|
self._markers = {node.name: True}
|
2012-10-18 19:52:32 +08:00
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
try:
|
2013-10-03 19:53:22 +08:00
|
|
|
return self._markers[key]
|
|
|
|
except KeyError:
|
|
|
|
if self.parent is None:
|
|
|
|
raise
|
|
|
|
return self.parent.keywords[key]
|
2012-10-18 19:52:32 +08:00
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
2013-10-03 19:53:22 +08:00
|
|
|
self._markers[key] = value
|
2012-10-18 19:52:32 +08:00
|
|
|
|
|
|
|
def __delitem__(self, key):
|
2013-10-03 19:53:22 +08:00
|
|
|
raise ValueError("cannot delete key in keywords dict")
|
2012-10-18 19:52:32 +08:00
|
|
|
|
|
|
|
def __iter__(self):
|
2013-10-03 19:53:22 +08:00
|
|
|
seen = set(self._markers)
|
|
|
|
if self.parent is not None:
|
|
|
|
seen.update(self.parent.keywords)
|
|
|
|
return iter(seen)
|
2012-10-18 19:52:32 +08:00
|
|
|
|
|
|
|
def __len__(self):
|
2013-10-03 19:53:22 +08:00
|
|
|
return len(self.__iter__())
|
2012-10-18 19:52:32 +08:00
|
|
|
|
|
|
|
def keys(self):
|
2013-10-03 19:53:22 +08:00
|
|
|
return list(self)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<NodeKeywords for node %s>" % (self.node, )
|
|
|
|
|
2011-11-08 02:08:41 +08:00
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
class Node(object):
|
2012-06-25 23:35:33 +08:00
|
|
|
""" base class for Collector and Item the test collection tree.
|
2010-10-12 18:19:53 +08:00
|
|
|
Collector subclasses have children, Items are terminal nodes."""
|
|
|
|
|
2010-11-07 17:19:58 +08:00
|
|
|
def __init__(self, name, parent=None, config=None, session=None):
|
2012-06-25 23:35:33 +08:00
|
|
|
#: a unique name within the scope of the parent node
|
2010-10-12 18:19:53 +08:00
|
|
|
self.name = name
|
|
|
|
|
|
|
|
#: the parent collector node.
|
|
|
|
self.parent = parent
|
2011-11-08 02:08:41 +08:00
|
|
|
|
2012-06-25 23:35:33 +08:00
|
|
|
#: the pytest config object
|
2010-10-12 18:19:53 +08:00
|
|
|
self.config = config or parent.config
|
|
|
|
|
2012-06-25 23:35:33 +08:00
|
|
|
#: the session this node is part of
|
2010-11-07 17:19:58 +08:00
|
|
|
self.session = session or parent.session
|
2011-11-08 02:08:41 +08:00
|
|
|
|
2012-06-25 23:35:33 +08:00
|
|
|
#: filesystem path where this node was collected from (can be None)
|
2010-10-12 18:19:53 +08:00
|
|
|
self.fspath = getattr(parent, 'fspath', None)
|
2012-06-25 23:35:33 +08:00
|
|
|
|
2012-10-18 19:52:32 +08:00
|
|
|
#: keywords/markers collected from all scopes
|
|
|
|
self.keywords = NodeKeywords(self)
|
2012-09-17 23:32:23 +08:00
|
|
|
|
2013-05-20 20:37:58 +08:00
|
|
|
#: allow adding of extra keywords to use for matching
|
2013-05-28 00:14:35 +08:00
|
|
|
self.extra_keyword_matches = set()
|
2013-05-20 20:37:58 +08:00
|
|
|
|
2013-12-07 23:37:46 +08:00
|
|
|
# used for storing artificial fixturedefs for direct parametrization
|
|
|
|
self._name2pseudofixturedef = {}
|
2014-03-14 19:49:34 +08:00
|
|
|
|
2012-10-22 22:12:22 +08:00
|
|
|
@property
|
|
|
|
def ihook(self):
|
|
|
|
""" fspath sensitive hook proxy used to call pytest hooks"""
|
|
|
|
return self.session.gethookproxy(self.fspath)
|
|
|
|
|
2016-12-06 16:13:25 +08:00
|
|
|
Module = _CompatProperty("Module")
|
|
|
|
Class = _CompatProperty("Class")
|
|
|
|
Instance = _CompatProperty("Instance")
|
|
|
|
Function = _CompatProperty("Function")
|
|
|
|
File = _CompatProperty("File")
|
|
|
|
Item = _CompatProperty("Item")
|
2010-10-26 05:09:21 +08:00
|
|
|
|
2011-03-05 21:16:27 +08:00
|
|
|
def _getcustomclass(self, name):
|
2016-12-06 16:13:25 +08:00
|
|
|
maybe_compatprop = getattr(type(self), name)
|
|
|
|
if isinstance(maybe_compatprop, _CompatProperty):
|
2017-03-16 01:00:59 +08:00
|
|
|
return getattr(__import__('pytest'), name)
|
2016-12-06 16:13:25 +08:00
|
|
|
else:
|
|
|
|
cls = getattr(self, name)
|
2017-01-20 18:25:48 +08:00
|
|
|
# TODO: reenable in the features branch
|
|
|
|
# warnings.warn("use of node.%s is deprecated, "
|
|
|
|
# "use pytest_pycollect_makeitem(...) to create custom "
|
|
|
|
# "collection nodes" % name, category=DeprecationWarning)
|
2011-03-05 21:16:27 +08:00
|
|
|
return cls
|
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
def __repr__(self):
|
2017-07-17 07:25:08 +08:00
|
|
|
return "<%s %r>" % (self.__class__.__name__,
|
|
|
|
getattr(self, 'name', None))
|
2010-10-12 18:19:53 +08:00
|
|
|
|
2014-03-12 05:10:17 +08:00
|
|
|
def warn(self, code, message):
|
|
|
|
""" generate a warning with the given code and message for this
|
|
|
|
item. """
|
|
|
|
assert isinstance(code, str)
|
|
|
|
fslocation = getattr(self, "location", None)
|
|
|
|
if fslocation is None:
|
|
|
|
fslocation = getattr(self, "fspath", None)
|
2015-09-03 00:49:49 +08:00
|
|
|
self.ihook.pytest_logwarning.call_historic(kwargs=dict(
|
|
|
|
code=code, message=message,
|
|
|
|
nodeid=self.nodeid, fslocation=fslocation))
|
2014-03-12 05:10:17 +08:00
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
# methods for ordering nodes
|
2010-11-06 16:58:04 +08:00
|
|
|
@property
|
|
|
|
def nodeid(self):
|
2012-06-25 23:35:33 +08:00
|
|
|
""" a ::-separated string denoting its collection tree address. """
|
2010-11-06 16:58:04 +08:00
|
|
|
try:
|
|
|
|
return self._nodeid
|
|
|
|
except AttributeError:
|
|
|
|
self._nodeid = x = self._makeid()
|
|
|
|
return x
|
|
|
|
|
|
|
|
def _makeid(self):
|
|
|
|
return self.parent.nodeid + "::" + self.name
|
2010-10-12 18:19:53 +08:00
|
|
|
|
|
|
|
def __hash__(self):
|
2013-12-07 23:39:53 +08:00
|
|
|
return hash(self.nodeid)
|
2010-10-12 18:19:53 +08:00
|
|
|
|
|
|
|
def setup(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def teardown(self):
|
|
|
|
pass
|
|
|
|
|
2012-09-24 17:36:24 +08:00
|
|
|
def _memoizedcall(self, attrname, function):
|
|
|
|
exattrname = "_ex_" + attrname
|
|
|
|
failure = getattr(self, exattrname, None)
|
|
|
|
if failure is not None:
|
|
|
|
py.builtin._reraise(failure[0], failure[1], failure[2])
|
|
|
|
if hasattr(self, attrname):
|
|
|
|
return getattr(self, attrname)
|
|
|
|
try:
|
|
|
|
res = function()
|
|
|
|
except py.builtin._sysex:
|
|
|
|
raise
|
|
|
|
except:
|
2014-08-01 06:13:40 +08:00
|
|
|
failure = sys.exc_info()
|
2012-09-24 17:36:24 +08:00
|
|
|
setattr(self, exattrname, failure)
|
|
|
|
raise
|
|
|
|
setattr(self, attrname, res)
|
|
|
|
return res
|
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
def listchain(self):
|
|
|
|
""" return list of all parent collectors up to self,
|
|
|
|
starting from root of collection tree. """
|
2011-12-02 02:36:44 +08:00
|
|
|
chain = []
|
|
|
|
item = self
|
|
|
|
while item is not None:
|
|
|
|
chain.append(item)
|
|
|
|
item = item.parent
|
|
|
|
chain.reverse()
|
|
|
|
return chain
|
2010-10-12 18:19:53 +08:00
|
|
|
|
2013-10-03 21:43:56 +08:00
|
|
|
def add_marker(self, marker):
|
|
|
|
""" dynamically add a marker object to the node.
|
|
|
|
|
|
|
|
``marker`` can be a string or pytest.mark.* instance.
|
|
|
|
"""
|
2017-03-16 01:00:59 +08:00
|
|
|
from _pytest.mark import MarkDecorator, MARK_GEN
|
2013-10-03 21:43:56 +08:00
|
|
|
if isinstance(marker, py.builtin._basestring):
|
2017-03-16 01:00:59 +08:00
|
|
|
marker = getattr(MARK_GEN, marker)
|
2013-10-03 21:43:56 +08:00
|
|
|
elif not isinstance(marker, MarkDecorator):
|
|
|
|
raise ValueError("is not a string or pytest.mark.* Marker")
|
|
|
|
self.keywords[marker.name] = marker
|
|
|
|
|
|
|
|
def get_marker(self, name):
|
|
|
|
""" get a marker object from this node or None if
|
|
|
|
the node doesn't have a marker with that name. """
|
|
|
|
val = self.keywords.get(name, None)
|
|
|
|
if val is not None:
|
|
|
|
from _pytest.mark import MarkInfo, MarkDecorator
|
|
|
|
if isinstance(val, (MarkDecorator, MarkInfo)):
|
|
|
|
return val
|
|
|
|
|
2013-05-20 20:37:58 +08:00
|
|
|
def listextrakeywords(self):
|
2013-05-28 00:14:35 +08:00
|
|
|
""" Return a set of all extra keywords in self and any parents."""
|
|
|
|
extra_keywords = set()
|
2013-05-20 20:37:58 +08:00
|
|
|
item = self
|
2013-05-28 00:14:35 +08:00
|
|
|
for item in self.listchain():
|
|
|
|
extra_keywords.update(item.extra_keyword_matches)
|
2013-05-20 20:37:58 +08:00
|
|
|
return extra_keywords
|
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
def listnames(self):
|
|
|
|
return [x.name for x in self.listchain()]
|
|
|
|
|
2013-08-02 15:52:40 +08:00
|
|
|
def addfinalizer(self, fin):
|
|
|
|
""" register a function to be called when this node is finalized.
|
|
|
|
|
|
|
|
This method can only be called when this node is active
|
|
|
|
in a setup chain, for example during self.setup().
|
|
|
|
"""
|
|
|
|
self.session._setupstate.addfinalizer(fin, self)
|
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
def getparent(self, cls):
|
2013-12-07 23:37:46 +08:00
|
|
|
""" get the next parent node (including ourself)
|
|
|
|
which is an instance of the given class"""
|
2010-10-12 18:19:53 +08:00
|
|
|
current = self
|
|
|
|
while current and not isinstance(current, cls):
|
|
|
|
current = current.parent
|
|
|
|
return current
|
|
|
|
|
2010-11-06 06:37:31 +08:00
|
|
|
def _prunetraceback(self, excinfo):
|
|
|
|
pass
|
2010-10-12 18:19:53 +08:00
|
|
|
|
|
|
|
def _repr_failure_py(self, excinfo, style=None):
|
2012-10-05 20:24:44 +08:00
|
|
|
fm = self.session._fixturemanager
|
|
|
|
if excinfo.errisinstance(fm.FixtureLookupError):
|
2012-10-05 20:24:45 +08:00
|
|
|
return excinfo.value.formatrepr()
|
2014-06-29 19:32:53 +08:00
|
|
|
tbfilter = True
|
2010-10-12 18:19:53 +08:00
|
|
|
if self.config.option.fulltrace:
|
2017-07-17 07:25:08 +08:00
|
|
|
style = "long"
|
2010-10-12 18:19:53 +08:00
|
|
|
else:
|
2016-06-12 00:04:49 +08:00
|
|
|
tb = _pytest._code.Traceback([excinfo.traceback[-1]])
|
2010-11-06 06:37:31 +08:00
|
|
|
self._prunetraceback(excinfo)
|
2016-06-12 00:04:49 +08:00
|
|
|
if len(excinfo.traceback) == 0:
|
|
|
|
excinfo.traceback = tb
|
2014-06-29 19:32:53 +08:00
|
|
|
tbfilter = False # prunetraceback already does it
|
|
|
|
if style == "auto":
|
|
|
|
style = "long"
|
|
|
|
# XXX should excinfo.getrepr record all data and toterminal() process it?
|
2010-10-12 18:19:53 +08:00
|
|
|
if style is None:
|
|
|
|
if self.config.option.tbstyle == "short":
|
|
|
|
style = "short"
|
|
|
|
else:
|
|
|
|
style = "long"
|
2014-06-29 19:32:53 +08:00
|
|
|
|
2016-06-07 21:43:05 +08:00
|
|
|
try:
|
|
|
|
os.getcwd()
|
|
|
|
abspath = False
|
|
|
|
except OSError:
|
|
|
|
abspath = True
|
|
|
|
|
|
|
|
return excinfo.getrepr(funcargs=True, abspath=abspath,
|
2010-10-12 18:19:53 +08:00
|
|
|
showlocals=self.config.option.showlocals,
|
2014-06-29 19:32:53 +08:00
|
|
|
style=style, tbfilter=tbfilter)
|
2010-10-12 18:19:53 +08:00
|
|
|
|
|
|
|
repr_failure = _repr_failure_py
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
class Collector(Node):
|
|
|
|
""" Collector instances create children through collect()
|
|
|
|
and thus iteratively build a tree.
|
|
|
|
"""
|
2013-03-14 23:10:33 +08:00
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
class CollectError(Exception):
|
|
|
|
""" an error during collection, contains a custom message. """
|
|
|
|
|
|
|
|
def collect(self):
|
|
|
|
""" returns a list of children (items and collectors)
|
|
|
|
for this collection node.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError("abstract")
|
|
|
|
|
|
|
|
def repr_failure(self, excinfo):
|
2010-11-01 01:01:33 +08:00
|
|
|
""" represent a collection failure. """
|
2010-10-12 18:19:53 +08:00
|
|
|
if excinfo.errisinstance(self.CollectError):
|
|
|
|
exc = excinfo.value
|
|
|
|
return str(exc.args[0])
|
|
|
|
return self._repr_failure_py(excinfo, style="short")
|
|
|
|
|
2010-11-06 06:37:31 +08:00
|
|
|
def _prunetraceback(self, excinfo):
|
2010-10-12 18:19:53 +08:00
|
|
|
if hasattr(self, 'fspath'):
|
2010-11-06 06:37:31 +08:00
|
|
|
traceback = excinfo.traceback
|
2010-10-12 18:19:53 +08:00
|
|
|
ntraceback = traceback.cut(path=self.fspath)
|
|
|
|
if ntraceback == traceback:
|
2010-11-06 06:37:31 +08:00
|
|
|
ntraceback = ntraceback.cut(excludepath=tracebackcutdir)
|
|
|
|
excinfo.traceback = ntraceback.filter()
|
2010-10-12 18:19:53 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
class FSCollector(Collector):
|
2010-11-07 17:19:58 +08:00
|
|
|
def __init__(self, fspath, parent=None, config=None, session=None):
|
2017-07-17 07:25:09 +08:00
|
|
|
fspath = py.path.local(fspath) # xxx only for test_resultlog.py?
|
2010-11-07 19:05:32 +08:00
|
|
|
name = fspath.basename
|
|
|
|
if parent is not None:
|
|
|
|
rel = fspath.relto(parent.fspath)
|
|
|
|
if rel:
|
|
|
|
name = rel
|
|
|
|
name = name.replace(os.sep, "/")
|
2010-11-07 17:19:58 +08:00
|
|
|
super(FSCollector, self).__init__(name, parent, config, session)
|
2010-10-12 18:19:53 +08:00
|
|
|
self.fspath = fspath
|
|
|
|
|
2010-11-06 16:58:04 +08:00
|
|
|
def _makeid(self):
|
2015-02-27 04:56:44 +08:00
|
|
|
relpath = self.fspath.relto(self.config.rootdir)
|
2010-11-06 16:58:04 +08:00
|
|
|
if os.sep != "/":
|
2010-11-07 08:13:40 +08:00
|
|
|
relpath = relpath.replace(os.sep, "/")
|
2010-11-06 16:58:04 +08:00
|
|
|
return relpath
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
class File(FSCollector):
|
|
|
|
""" base class for collecting tests from a file. """
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2012-07-16 16:46:44 +08:00
|
|
|
class Item(Node):
|
2010-10-12 18:19:53 +08:00
|
|
|
""" a basic test invocation item. Note that for a single function
|
2010-11-01 01:01:33 +08:00
|
|
|
there might be multiple test invocation items.
|
2010-10-12 18:19:53 +08:00
|
|
|
"""
|
2011-11-19 00:58:21 +08:00
|
|
|
nextitem = None
|
|
|
|
|
2014-03-14 19:49:34 +08:00
|
|
|
def __init__(self, name, parent=None, config=None, session=None):
|
|
|
|
super(Item, self).__init__(name, parent, config, session)
|
|
|
|
self._report_sections = []
|
|
|
|
|
|
|
|
def add_report_section(self, when, key, content):
|
2017-07-12 08:45:27 +08:00
|
|
|
"""
|
|
|
|
Adds a new report section, similar to what's done internally to add stdout and
|
|
|
|
stderr captured output::
|
|
|
|
|
|
|
|
item.add_report_section("call", "stdout", "report section contents")
|
|
|
|
|
|
|
|
:param str when:
|
|
|
|
One of the possible capture states, ``"setup"``, ``"call"``, ``"teardown"``.
|
|
|
|
:param str key:
|
|
|
|
Name of the section, can be customized at will. Pytest uses ``"stdout"`` and
|
|
|
|
``"stderr"`` internally.
|
|
|
|
|
|
|
|
:param str content:
|
|
|
|
The full contents as a string.
|
|
|
|
"""
|
2014-03-14 19:49:34 +08:00
|
|
|
if content:
|
|
|
|
self._report_sections.append((when, key, content))
|
|
|
|
|
2010-10-12 18:19:53 +08:00
|
|
|
def reportinfo(self):
|
|
|
|
return self.fspath, None, ""
|
2010-11-06 16:58:04 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def location(self):
|
|
|
|
try:
|
|
|
|
return self._location
|
|
|
|
except AttributeError:
|
|
|
|
location = self.reportinfo()
|
2011-03-08 01:28:45 +08:00
|
|
|
# bestrelpath is a quite slow function
|
|
|
|
cache = self.config.__dict__.setdefault("_bestrelpathcache", {})
|
|
|
|
try:
|
|
|
|
fspath = cache[location[0]]
|
|
|
|
except KeyError:
|
|
|
|
fspath = self.session.fspath.bestrelpath(location[0])
|
|
|
|
cache[location[0]] = fspath
|
2010-11-14 06:33:38 +08:00
|
|
|
location = (fspath, location[1], str(location[2]))
|
2010-11-06 16:58:04 +08:00
|
|
|
self._location = location
|
|
|
|
return location
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2010-11-07 19:05:32 +08:00
|
|
|
class NoMatch(Exception):
|
|
|
|
""" raised if matching cannot locate a matching names. """
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-07-16 05:28:43 +08:00
|
|
|
class Interrupted(KeyboardInterrupt):
|
|
|
|
""" signals an interrupted test run. """
|
2017-07-17 07:25:09 +08:00
|
|
|
__module__ = 'builtins' # for py3
|
2015-07-16 05:28:43 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2010-11-07 17:19:58 +08:00
|
|
|
class Session(FSCollector):
|
2015-07-16 05:28:43 +08:00
|
|
|
Interrupted = Interrupted
|
2010-11-07 17:19:58 +08:00
|
|
|
|
2010-11-06 16:58:04 +08:00
|
|
|
def __init__(self, config):
|
2015-02-27 04:56:44 +08:00
|
|
|
FSCollector.__init__(self, config.rootdir, parent=None,
|
2012-07-16 16:46:44 +08:00
|
|
|
config=config, session=self)
|
2015-07-07 07:25:16 +08:00
|
|
|
self.testsfailed = 0
|
|
|
|
self.testscollected = 0
|
2010-11-07 17:19:58 +08:00
|
|
|
self.shouldstop = False
|
2010-11-06 16:58:04 +08:00
|
|
|
self.trace = config.trace.root.get("collection")
|
|
|
|
self._norecursepatterns = config.getini("norecursedirs")
|
2013-06-10 16:09:28 +08:00
|
|
|
self.startdir = py.path.local()
|
2015-04-29 22:40:52 +08:00
|
|
|
self.config.pluginmanager.register(self, name="session")
|
2010-11-06 16:58:04 +08:00
|
|
|
|
2015-02-27 04:56:44 +08:00
|
|
|
def _makeid(self):
|
|
|
|
return ""
|
|
|
|
|
2017-03-16 01:00:59 +08:00
|
|
|
@hookimpl(tryfirst=True)
|
2010-11-07 17:19:58 +08:00
|
|
|
def pytest_collectstart(self):
|
|
|
|
if self.shouldstop:
|
|
|
|
raise self.Interrupted(self.shouldstop)
|
|
|
|
|
2017-03-16 01:00:59 +08:00
|
|
|
@hookimpl(tryfirst=True)
|
2010-11-07 17:19:58 +08:00
|
|
|
def pytest_runtest_logreport(self, report):
|
2012-06-23 17:32:32 +08:00
|
|
|
if report.failed and not hasattr(report, 'wasxfail'):
|
2015-07-07 07:25:16 +08:00
|
|
|
self.testsfailed += 1
|
2010-11-07 17:19:58 +08:00
|
|
|
maxfail = self.config.getvalue("maxfail")
|
2015-07-07 07:25:16 +08:00
|
|
|
if maxfail and self.testsfailed >= maxfail:
|
2010-11-07 17:19:58 +08:00
|
|
|
self.shouldstop = "stopping after %d failures" % (
|
2015-07-07 07:25:16 +08:00
|
|
|
self.testsfailed)
|
2010-11-07 17:19:58 +08:00
|
|
|
pytest_collectreport = pytest_runtest_logreport
|
|
|
|
|
2010-11-06 16:58:04 +08:00
|
|
|
def isinitpath(self, path):
|
|
|
|
return path in self._initialpaths
|
|
|
|
|
|
|
|
def gethookproxy(self, fspath):
|
2016-11-21 22:50:21 +08:00
|
|
|
# check if we have the common case of running
|
|
|
|
# hooks with all conftest.py filesall conftest.py
|
|
|
|
pm = self.config.pluginmanager
|
|
|
|
my_conftestmodules = pm._getconftestmodules(fspath)
|
|
|
|
remove_mods = pm._conftest_plugins.difference(my_conftestmodules)
|
|
|
|
if remove_mods:
|
|
|
|
# one or more conftests are not in use at this fspath
|
|
|
|
proxy = FSHookProxy(fspath, pm, remove_mods)
|
|
|
|
else:
|
|
|
|
# all plugis are active for this fspath
|
|
|
|
proxy = self.config.hook
|
|
|
|
return proxy
|
2010-11-06 16:58:04 +08:00
|
|
|
|
|
|
|
def perform_collect(self, args=None, genitems=True):
|
2011-05-27 08:53:47 +08:00
|
|
|
hook = self.config.hook
|
|
|
|
try:
|
|
|
|
items = self._perform_collect(args, genitems)
|
2017-03-08 08:02:52 +08:00
|
|
|
self.config.pluginmanager.check_pending()
|
2011-05-27 08:53:47 +08:00
|
|
|
hook.pytest_collection_modifyitems(session=self,
|
2017-07-17 07:25:07 +08:00
|
|
|
config=self.config, items=items)
|
2011-05-27 08:53:47 +08:00
|
|
|
finally:
|
|
|
|
hook.pytest_collection_finish(session=self)
|
2015-07-07 07:25:16 +08:00
|
|
|
self.testscollected = len(items)
|
2011-05-27 08:53:47 +08:00
|
|
|
return items
|
|
|
|
|
|
|
|
def _perform_collect(self, args, genitems):
|
2010-11-06 16:58:04 +08:00
|
|
|
if args is None:
|
|
|
|
args = self.config.args
|
|
|
|
self.trace("perform_collect", self, args)
|
|
|
|
self.trace.root.indent += 1
|
|
|
|
self._notfound = []
|
|
|
|
self._initialpaths = set()
|
2010-11-07 07:22:16 +08:00
|
|
|
self._initialparts = []
|
2012-02-03 23:33:32 +08:00
|
|
|
self.items = items = []
|
2010-11-06 16:58:04 +08:00
|
|
|
for arg in args:
|
|
|
|
parts = self._parsearg(arg)
|
2010-11-07 07:22:16 +08:00
|
|
|
self._initialparts.append(parts)
|
2010-11-06 16:58:04 +08:00
|
|
|
self._initialpaths.add(parts[0])
|
2013-09-06 17:56:04 +08:00
|
|
|
rep = collect_one_node(self)
|
2010-11-06 16:58:04 +08:00
|
|
|
self.ihook.pytest_collectreport(report=rep)
|
|
|
|
self.trace.root.indent -= 1
|
|
|
|
if self._notfound:
|
2013-09-09 04:26:51 +08:00
|
|
|
errors = []
|
2010-11-06 16:58:04 +08:00
|
|
|
for arg, exc in self._notfound:
|
2010-11-07 07:22:16 +08:00
|
|
|
line = "(no name %r in any of %r)" % (arg, exc.args[0])
|
2013-09-09 04:26:51 +08:00
|
|
|
errors.append("not found: %s\n%s" % (arg, line))
|
2017-03-16 01:00:59 +08:00
|
|
|
# XXX: test this
|
|
|
|
raise UsageError(*errors)
|
2010-11-06 16:58:04 +08:00
|
|
|
if not genitems:
|
|
|
|
return rep.result
|
|
|
|
else:
|
|
|
|
if rep.passed:
|
|
|
|
for node in rep.result:
|
|
|
|
self.items.extend(self.genitems(node))
|
|
|
|
return items
|
|
|
|
|
|
|
|
def collect(self):
|
2010-11-07 07:22:16 +08:00
|
|
|
for parts in self._initialparts:
|
|
|
|
arg = "::".join(map(str, parts))
|
|
|
|
self.trace("processing argument", arg)
|
2010-11-06 16:58:04 +08:00
|
|
|
self.trace.root.indent += 1
|
|
|
|
try:
|
|
|
|
for x in self._collect(arg):
|
|
|
|
yield x
|
|
|
|
except NoMatch:
|
|
|
|
# we are inside a make_report hook so
|
|
|
|
# we cannot directly pass through the exception
|
|
|
|
self._notfound.append((arg, sys.exc_info()[1]))
|
2013-09-09 04:26:51 +08:00
|
|
|
|
2010-11-06 16:58:04 +08:00
|
|
|
self.trace.root.indent -= 1
|
|
|
|
|
|
|
|
def _collect(self, arg):
|
|
|
|
names = self._parsearg(arg)
|
|
|
|
path = names.pop(0)
|
|
|
|
if path.check(dir=1):
|
2017-03-16 01:00:59 +08:00
|
|
|
assert not names, "invalid arg %r" % (arg,)
|
2010-11-18 21:56:16 +08:00
|
|
|
for path in path.visit(fil=lambda x: x.check(file=1),
|
2012-06-21 17:20:29 +08:00
|
|
|
rec=self._recurse, bf=True, sort=True):
|
2010-11-06 16:58:04 +08:00
|
|
|
for x in self._collectfile(path):
|
|
|
|
yield x
|
|
|
|
else:
|
|
|
|
assert path.check(file=1)
|
|
|
|
for x in self.matchnodes(self._collectfile(path), names):
|
|
|
|
yield x
|
|
|
|
|
|
|
|
def _collectfile(self, path):
|
|
|
|
ihook = self.gethookproxy(path)
|
2010-11-07 07:22:16 +08:00
|
|
|
if not self.isinitpath(path):
|
|
|
|
if ihook.pytest_ignore_collect(path=path, config=self.config):
|
2012-06-21 17:20:29 +08:00
|
|
|
return ()
|
2010-11-06 16:58:04 +08:00
|
|
|
return ihook.pytest_collect_file(path=path, parent=self)
|
|
|
|
|
|
|
|
def _recurse(self, path):
|
2010-11-18 21:56:16 +08:00
|
|
|
ihook = self.gethookproxy(path.dirpath())
|
2010-11-06 16:58:04 +08:00
|
|
|
if ihook.pytest_ignore_collect(path=path, config=self.config):
|
2012-06-21 17:20:29 +08:00
|
|
|
return
|
2010-11-06 16:58:04 +08:00
|
|
|
for pat in self._norecursepatterns:
|
|
|
|
if path.check(fnmatch=pat):
|
|
|
|
return False
|
2010-11-18 21:56:16 +08:00
|
|
|
ihook = self.gethookproxy(path)
|
2010-11-06 16:58:04 +08:00
|
|
|
ihook.pytest_collect_directory(path=path, parent=self)
|
|
|
|
return True
|
|
|
|
|
2010-11-07 05:17:33 +08:00
|
|
|
def _tryconvertpyarg(self, x):
|
2016-06-08 21:17:55 +08:00
|
|
|
"""Convert a dotted module name to path.
|
2011-11-08 02:08:41 +08:00
|
|
|
|
2016-06-08 21:17:55 +08:00
|
|
|
"""
|
|
|
|
import pkgutil
|
|
|
|
try:
|
|
|
|
loader = pkgutil.find_loader(x)
|
|
|
|
except ImportError:
|
|
|
|
return x
|
|
|
|
if loader is None:
|
|
|
|
return x
|
2016-06-09 14:22:11 +08:00
|
|
|
# This method is sometimes invoked when AssertionRewritingHook, which
|
|
|
|
# does not define a get_filename method, is already in place:
|
2016-06-08 21:17:55 +08:00
|
|
|
try:
|
2016-08-22 04:07:18 +08:00
|
|
|
path = loader.get_filename(x)
|
2016-06-09 14:22:11 +08:00
|
|
|
except AttributeError:
|
|
|
|
# Retrieve path from AssertionRewritingHook:
|
2016-06-08 21:17:55 +08:00
|
|
|
path = loader.modules[x][0].co_filename
|
|
|
|
if loader.is_package(x):
|
|
|
|
path = os.path.dirname(path)
|
|
|
|
return path
|
2010-11-07 05:17:33 +08:00
|
|
|
|
2010-11-06 16:58:04 +08:00
|
|
|
def _parsearg(self, arg):
|
|
|
|
""" return (fspath, names) tuple after checking the file exists. """
|
|
|
|
parts = str(arg).split("::")
|
2016-06-08 21:17:55 +08:00
|
|
|
if self.config.option.pyargs:
|
|
|
|
parts[0] = self._tryconvertpyarg(parts[0])
|
2010-11-07 19:05:32 +08:00
|
|
|
relpath = parts[0].replace("/", os.sep)
|
2015-02-27 04:56:44 +08:00
|
|
|
path = self.config.invocation_dir.join(relpath, abs=True)
|
2010-11-06 16:58:04 +08:00
|
|
|
if not path.check():
|
2010-11-07 05:17:33 +08:00
|
|
|
if self.config.option.pyargs:
|
2017-03-16 01:00:59 +08:00
|
|
|
raise UsageError(
|
|
|
|
"file or package not found: " + arg +
|
|
|
|
" (missing __init__.py?)")
|
2010-11-07 05:17:33 +08:00
|
|
|
else:
|
2017-03-16 01:00:59 +08:00
|
|
|
raise UsageError("file not found: " + arg)
|
2010-11-06 16:58:04 +08:00
|
|
|
parts[0] = path
|
|
|
|
return parts
|
2011-11-08 02:08:41 +08:00
|
|
|
|
2010-11-06 16:58:04 +08:00
|
|
|
def matchnodes(self, matching, names):
|
|
|
|
self.trace("matchnodes", matching, names)
|
|
|
|
self.trace.root.indent += 1
|
|
|
|
nodes = self._matchnodes(matching, names)
|
|
|
|
num = len(nodes)
|
|
|
|
self.trace("matchnodes finished -> ", num, "nodes")
|
|
|
|
self.trace.root.indent -= 1
|
|
|
|
if num == 0:
|
|
|
|
raise NoMatch(matching, names[:1])
|
|
|
|
return nodes
|
|
|
|
|
|
|
|
def _matchnodes(self, matching, names):
|
|
|
|
if not matching or not names:
|
|
|
|
return matching
|
|
|
|
name = names[0]
|
|
|
|
assert name
|
|
|
|
nextnames = names[1:]
|
|
|
|
resultnodes = []
|
|
|
|
for node in matching:
|
2017-03-16 01:00:59 +08:00
|
|
|
if isinstance(node, Item):
|
2010-11-18 01:24:28 +08:00
|
|
|
if not names:
|
|
|
|
resultnodes.append(node)
|
2010-11-06 16:58:04 +08:00
|
|
|
continue
|
2017-03-16 01:00:59 +08:00
|
|
|
assert isinstance(node, Collector)
|
2013-09-06 17:56:04 +08:00
|
|
|
rep = collect_one_node(node)
|
2010-11-06 16:58:04 +08:00
|
|
|
if rep.passed:
|
2011-02-07 18:09:42 +08:00
|
|
|
has_matched = False
|
2010-11-06 16:58:04 +08:00
|
|
|
for x in rep.result:
|
2016-03-16 05:42:25 +08:00
|
|
|
# TODO: remove parametrized workaround once collection structure contains parametrization
|
2016-03-14 05:37:21 +08:00
|
|
|
if x.name == name or x.name.split("[")[0] == name:
|
2010-11-06 16:58:04 +08:00
|
|
|
resultnodes.extend(self.matchnodes([x], nextnames))
|
2011-02-07 18:09:42 +08:00
|
|
|
has_matched = True
|
|
|
|
# XXX accept IDs that don't have "()" for class instances
|
|
|
|
if not has_matched and len(rep.result) == 1 and x.name == "()":
|
|
|
|
nextnames.insert(0, name)
|
|
|
|
resultnodes.extend(self.matchnodes([x], nextnames))
|
2017-06-04 05:26:34 +08:00
|
|
|
else:
|
|
|
|
# report collection failures here to avoid failing to run some test
|
|
|
|
# specified in the command line because the module could not be
|
|
|
|
# imported (#134)
|
|
|
|
node.ihook.pytest_collectreport(report=rep)
|
2010-11-06 16:58:04 +08:00
|
|
|
return resultnodes
|
|
|
|
|
|
|
|
def genitems(self, node):
|
|
|
|
self.trace("genitems", node)
|
2017-03-16 01:00:59 +08:00
|
|
|
if isinstance(node, Item):
|
2010-11-06 16:58:04 +08:00
|
|
|
node.ihook.pytest_itemcollected(item=node)
|
|
|
|
yield node
|
|
|
|
else:
|
2017-03-16 01:00:59 +08:00
|
|
|
assert isinstance(node, Collector)
|
2013-09-06 17:56:04 +08:00
|
|
|
rep = collect_one_node(node)
|
2010-11-06 16:58:04 +08:00
|
|
|
if rep.passed:
|
|
|
|
for subnode in rep.result:
|
|
|
|
for x in self.genitems(subnode):
|
|
|
|
yield x
|
|
|
|
node.ihook.pytest_collectreport(report=rep)
|