2010-10-13 18:26:14 +08:00
|
|
|
""" generic mechanism for marking and selecting python functions. """
|
2013-09-30 19:14:14 +08:00
|
|
|
import py
|
2009-06-28 19:19:43 +08:00
|
|
|
|
2013-05-20 20:37:58 +08:00
|
|
|
|
2009-07-18 00:07:37 +08:00
|
|
|
def pytest_namespace():
|
2009-12-29 23:29:48 +08:00
|
|
|
return {'mark': MarkGenerator()}
|
2009-06-28 19:19:43 +08:00
|
|
|
|
2013-05-20 20:37:58 +08:00
|
|
|
|
2010-10-12 19:05:29 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
group = parser.getgroup("general")
|
2013-05-20 20:37:58 +08:00
|
|
|
group._addoption(
|
|
|
|
'-k',
|
2013-04-16 15:04:05 +08:00
|
|
|
action="store", dest="keyword", default='', metavar="EXPRESSION",
|
|
|
|
help="only run tests which match the given substring expression. "
|
2012-11-20 21:20:39 +08:00
|
|
|
"An expression is a python evaluatable expression "
|
2013-05-22 13:41:46 +08:00
|
|
|
"where all names are substring-matched against test names "
|
2013-05-20 20:37:58 +08:00
|
|
|
"and their parent classes. Example: -k 'test_method or test "
|
|
|
|
"other' matches all test functions and classes whose name "
|
|
|
|
"contains 'test_method' or 'test_other'. "
|
|
|
|
"Additionally keywords are matched to classes and functions "
|
2013-05-28 00:14:35 +08:00
|
|
|
"containing extra names in their 'extra_keyword_matches' set, "
|
2013-05-20 20:37:58 +08:00
|
|
|
"as well as functions which have names assigned directly to them."
|
|
|
|
)
|
2010-10-12 19:05:29 +08:00
|
|
|
|
2013-05-20 20:37:58 +08:00
|
|
|
group._addoption(
|
|
|
|
"-m",
|
2011-11-12 07:02:06 +08:00
|
|
|
action="store", dest="markexpr", default="", metavar="MARKEXPR",
|
2011-11-19 02:32:11 +08:00
|
|
|
help="only run tests matching given mark expression. "
|
|
|
|
"example: -m 'mark1 and not mark2'."
|
2013-05-20 20:37:58 +08:00
|
|
|
)
|
2011-11-12 07:02:06 +08:00
|
|
|
|
2013-05-20 20:37:58 +08:00
|
|
|
group.addoption(
|
|
|
|
"--markers", action="store_true",
|
|
|
|
help="show markers (builtin, plugin and per-project ones)."
|
|
|
|
)
|
2011-11-12 06:56:11 +08:00
|
|
|
|
|
|
|
parser.addini("markers", "markers for test functions", 'linelist')
|
|
|
|
|
2013-05-20 20:37:58 +08:00
|
|
|
|
2011-11-12 06:56:11 +08:00
|
|
|
def pytest_cmdline_main(config):
|
|
|
|
if config.option.markers:
|
2013-09-30 19:14:14 +08:00
|
|
|
config.do_configure()
|
2014-04-01 21:03:17 +08:00
|
|
|
tw = py.io.TerminalWriter()
|
2011-11-12 06:56:11 +08:00
|
|
|
for line in config.getini("markers"):
|
|
|
|
name, rest = line.split(":", 1)
|
2013-05-20 20:37:58 +08:00
|
|
|
tw.write("@pytest.mark.%s:" % name, bold=True)
|
2011-11-12 06:56:11 +08:00
|
|
|
tw.line(rest)
|
|
|
|
tw.line()
|
2013-09-30 19:14:14 +08:00
|
|
|
config.do_unconfigure()
|
2011-11-12 06:56:11 +08:00
|
|
|
return 0
|
|
|
|
pytest_cmdline_main.tryfirst = True
|
|
|
|
|
2013-05-20 20:37:58 +08:00
|
|
|
|
2010-10-12 19:05:29 +08:00
|
|
|
def pytest_collection_modifyitems(items, config):
|
|
|
|
keywordexpr = config.option.keyword
|
2011-11-12 07:02:06 +08:00
|
|
|
matchexpr = config.option.markexpr
|
|
|
|
if not keywordexpr and not matchexpr:
|
2010-10-12 19:05:29 +08:00
|
|
|
return
|
2014-10-06 18:11:48 +08:00
|
|
|
# pytest used to allow "-" for negating
|
|
|
|
# but today we just allow "-" at the beginning, use "not" instead
|
|
|
|
# we probably remove "-" alltogether soon
|
|
|
|
if keywordexpr.startswith("-"):
|
|
|
|
keywordexpr = "not " + keywordexpr[1:]
|
2010-10-12 19:05:29 +08:00
|
|
|
selectuntil = False
|
2011-11-12 07:02:06 +08:00
|
|
|
if keywordexpr[-1:] == ":":
|
2010-10-12 19:05:29 +08:00
|
|
|
selectuntil = True
|
|
|
|
keywordexpr = keywordexpr[:-1]
|
|
|
|
|
|
|
|
remaining = []
|
|
|
|
deselected = []
|
|
|
|
for colitem in items:
|
2012-11-09 19:29:33 +08:00
|
|
|
if keywordexpr and not matchkeyword(colitem, keywordexpr):
|
2010-10-12 19:05:29 +08:00
|
|
|
deselected.append(colitem)
|
|
|
|
else:
|
|
|
|
if selectuntil:
|
|
|
|
keywordexpr = None
|
2011-11-12 07:02:06 +08:00
|
|
|
if matchexpr:
|
|
|
|
if not matchmark(colitem, matchexpr):
|
|
|
|
deselected.append(colitem)
|
|
|
|
continue
|
|
|
|
remaining.append(colitem)
|
2010-10-12 19:05:29 +08:00
|
|
|
|
|
|
|
if deselected:
|
|
|
|
config.hook.pytest_deselected(items=deselected)
|
|
|
|
items[:] = remaining
|
|
|
|
|
2013-05-20 20:37:58 +08:00
|
|
|
|
|
|
|
class MarkMapping:
|
2013-10-03 21:43:56 +08:00
|
|
|
"""Provides a local mapping for markers where item access
|
|
|
|
resolves to True if the marker is present. """
|
2013-05-20 20:37:58 +08:00
|
|
|
def __init__(self, keywords):
|
2013-05-28 00:14:35 +08:00
|
|
|
mymarks = set()
|
2013-05-20 20:37:58 +08:00
|
|
|
for key, value in keywords.items():
|
|
|
|
if isinstance(value, MarkInfo) or isinstance(value, MarkDecorator):
|
2013-05-28 00:14:35 +08:00
|
|
|
mymarks.add(key)
|
2013-05-20 20:37:58 +08:00
|
|
|
self._mymarks = mymarks
|
|
|
|
|
2013-10-03 21:43:56 +08:00
|
|
|
def __getitem__(self, name):
|
|
|
|
return name in self._mymarks
|
2013-05-20 20:37:58 +08:00
|
|
|
|
|
|
|
|
|
|
|
class KeywordMapping:
|
|
|
|
"""Provides a local mapping for keywords.
|
|
|
|
Given a list of names, map any substring of one of these names to True.
|
|
|
|
"""
|
|
|
|
def __init__(self, names):
|
|
|
|
self._names = names
|
|
|
|
|
|
|
|
def __getitem__(self, subname):
|
|
|
|
for name in self._names:
|
|
|
|
if subname in name:
|
2012-11-09 19:29:33 +08:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2013-05-20 20:37:58 +08:00
|
|
|
|
|
|
|
def matchmark(colitem, markexpr):
|
|
|
|
"""Tries to match on any marker names, attached to the given colitem."""
|
|
|
|
return eval(markexpr, {}, MarkMapping(colitem.keywords))
|
|
|
|
|
2011-11-12 07:02:06 +08:00
|
|
|
|
2012-11-09 19:29:33 +08:00
|
|
|
def matchkeyword(colitem, keywordexpr):
|
2013-05-20 20:37:58 +08:00
|
|
|
"""Tries to match given keyword expression to given collector item.
|
|
|
|
|
|
|
|
Will match on the name of colitem, including the names of its parents.
|
|
|
|
Only matches names of items which are either a :class:`Class` or a
|
|
|
|
:class:`Function`.
|
2013-05-28 00:14:35 +08:00
|
|
|
Additionally, matches on names in the 'extra_keyword_matches' set of
|
2013-05-20 20:37:58 +08:00
|
|
|
any item, as well as names directly assigned to test functions.
|
|
|
|
"""
|
2013-05-28 00:14:35 +08:00
|
|
|
mapped_names = set()
|
2013-05-20 20:37:58 +08:00
|
|
|
|
|
|
|
# Add the names of the current item and any parent items
|
2013-09-30 19:14:14 +08:00
|
|
|
import pytest
|
2013-05-20 20:37:58 +08:00
|
|
|
for item in colitem.listchain():
|
2013-05-27 23:58:39 +08:00
|
|
|
if not isinstance(item, pytest.Instance):
|
2013-05-28 00:14:35 +08:00
|
|
|
mapped_names.add(item.name)
|
2013-05-20 20:37:58 +08:00
|
|
|
|
|
|
|
# Add the names added as extra keywords to current or parent items
|
|
|
|
for name in colitem.listextrakeywords():
|
2013-05-28 00:14:35 +08:00
|
|
|
mapped_names.add(name)
|
2013-05-20 20:37:58 +08:00
|
|
|
|
|
|
|
# Add the names attached to the current function through direct assignment
|
2013-10-02 21:55:28 +08:00
|
|
|
if hasattr(colitem, 'function'):
|
|
|
|
for name in colitem.function.__dict__:
|
|
|
|
mapped_names.add(name)
|
2013-05-20 20:37:58 +08:00
|
|
|
|
2013-11-21 22:25:16 +08:00
|
|
|
mapping = KeywordMapping(mapped_names)
|
|
|
|
if " " not in keywordexpr:
|
|
|
|
# special case to allow for simple "-k pass" and "-k 1.3"
|
|
|
|
return mapping[keywordexpr]
|
|
|
|
elif keywordexpr.startswith("not ") and " " not in keywordexpr[4:]:
|
|
|
|
return not mapping[keywordexpr[4:]]
|
|
|
|
return eval(keywordexpr, {}, mapping)
|
2013-05-20 20:37:58 +08:00
|
|
|
|
2012-11-09 19:29:33 +08:00
|
|
|
|
2011-11-12 06:56:11 +08:00
|
|
|
def pytest_configure(config):
|
2013-09-30 19:14:14 +08:00
|
|
|
import pytest
|
2011-11-12 06:56:11 +08:00
|
|
|
if config.option.strict:
|
|
|
|
pytest.mark._config = config
|
|
|
|
|
2013-05-20 20:37:58 +08:00
|
|
|
|
2009-12-29 23:29:48 +08:00
|
|
|
class MarkGenerator:
|
2010-11-06 18:38:53 +08:00
|
|
|
""" Factory for :class:`MarkDecorator` objects - exposed as
|
2014-01-18 19:31:33 +08:00
|
|
|
a ``pytest.mark`` singleton instance. Example::
|
2010-10-14 01:30:00 +08:00
|
|
|
|
|
|
|
import py
|
2014-01-18 19:31:33 +08:00
|
|
|
@pytest.mark.slowtest
|
2010-10-14 01:30:00 +08:00
|
|
|
def test_function():
|
|
|
|
pass
|
2011-11-12 06:56:11 +08:00
|
|
|
|
2010-10-14 01:30:00 +08:00
|
|
|
will set a 'slowtest' :class:`MarkInfo` object
|
|
|
|
on the ``test_function`` object. """
|
|
|
|
|
2009-06-28 19:19:43 +08:00
|
|
|
def __getattr__(self, name):
|
|
|
|
if name[0] == "_":
|
2013-12-19 21:29:57 +08:00
|
|
|
raise AttributeError("Marker name must NOT start with underscore")
|
2011-11-12 06:56:11 +08:00
|
|
|
if hasattr(self, '_config'):
|
|
|
|
self._check(name)
|
2009-12-29 23:29:48 +08:00
|
|
|
return MarkDecorator(name)
|
2009-06-28 19:19:43 +08:00
|
|
|
|
2011-11-12 06:56:11 +08:00
|
|
|
def _check(self, name):
|
|
|
|
try:
|
|
|
|
if name in self._markers:
|
|
|
|
return
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
self._markers = l = set()
|
|
|
|
for line in self._config.getini("markers"):
|
|
|
|
beginning = line.split(":", 1)
|
|
|
|
x = beginning[0].split("(", 1)[0]
|
|
|
|
l.add(x)
|
|
|
|
if name not in self._markers:
|
|
|
|
raise AttributeError("%r not a registered marker" % (name,))
|
|
|
|
|
2013-10-11 20:36:54 +08:00
|
|
|
def istestfunc(func):
|
|
|
|
return hasattr(func, "__call__") and \
|
|
|
|
getattr(func, "__name__", "<lambda>") != "<lambda>"
|
2013-05-20 20:37:58 +08:00
|
|
|
|
2009-12-29 23:29:48 +08:00
|
|
|
class MarkDecorator:
|
2010-10-14 01:30:00 +08:00
|
|
|
""" A decorator for test functions and test classes. When applied
|
2010-11-06 18:38:53 +08:00
|
|
|
it will create :class:`MarkInfo` objects which may be
|
2011-02-09 21:55:21 +08:00
|
|
|
:ref:`retrieved by hooks as item keywords <excontrolskip>`.
|
|
|
|
MarkDecorator instances are often created like this::
|
2010-10-14 01:30:00 +08:00
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
mark1 = pytest.mark.NAME # simple MarkDecorator
|
|
|
|
mark2 = pytest.mark.NAME(name1=value) # parametrized MarkDecorator
|
2010-10-14 01:30:00 +08:00
|
|
|
|
|
|
|
and can then be applied as decorators to test functions::
|
|
|
|
|
|
|
|
@mark2
|
|
|
|
def test_function():
|
|
|
|
pass
|
2014-01-20 08:27:33 +08:00
|
|
|
|
|
|
|
When a MarkDecorator instance is called it does the following:
|
|
|
|
1. If called with a single class as its only positional argument and no
|
|
|
|
additional keyword arguments, it attaches itself to the class so it
|
|
|
|
gets applied automatically to all test cases found in that class.
|
|
|
|
2. If called with a single function as its only positional argument and
|
|
|
|
no additional keyword arguments, it attaches a MarkInfo object to the
|
|
|
|
function, containing all the arguments already stored internally in
|
|
|
|
the MarkDecorator.
|
|
|
|
3. When called in any other case, it performs a 'fake construction' call,
|
|
|
|
i.e. it returns a new MarkDecorator instance with the original
|
|
|
|
MarkDecorator's content updated with the arguments passed to this
|
|
|
|
call.
|
|
|
|
|
|
|
|
Note: The rules above prevent MarkDecorator objects from storing only a
|
|
|
|
single function or class reference as their positional argument with no
|
|
|
|
additional keyword or positional arguments.
|
|
|
|
|
2010-10-14 01:30:00 +08:00
|
|
|
"""
|
2010-11-21 03:17:38 +08:00
|
|
|
def __init__(self, name, args=None, kwargs=None):
|
2013-10-03 21:43:56 +08:00
|
|
|
self.name = name
|
2010-11-21 03:17:38 +08:00
|
|
|
self.args = args or ()
|
|
|
|
self.kwargs = kwargs or {}
|
2009-06-28 19:19:43 +08:00
|
|
|
|
2013-10-03 21:43:56 +08:00
|
|
|
@property
|
|
|
|
def markname(self):
|
|
|
|
return self.name # for backward-compat (2.4.1 had this attr)
|
|
|
|
|
2009-07-28 20:26:32 +08:00
|
|
|
def __repr__(self):
|
|
|
|
d = self.__dict__.copy()
|
2013-10-03 21:43:56 +08:00
|
|
|
name = d.pop('name')
|
2013-05-20 20:37:58 +08:00
|
|
|
return "<MarkDecorator %r %r>" % (name, d)
|
2009-06-28 19:19:43 +08:00
|
|
|
|
2009-07-28 20:26:32 +08:00
|
|
|
def __call__(self, *args, **kwargs):
|
2010-07-27 03:15:15 +08:00
|
|
|
""" if passed a single callable argument: decorate it with mark info.
|
2009-12-29 23:29:48 +08:00
|
|
|
otherwise add *args/**kwargs in-place to mark information. """
|
2014-01-20 08:27:33 +08:00
|
|
|
if args and not kwargs:
|
2010-05-22 00:11:47 +08:00
|
|
|
func = args[0]
|
2013-10-11 20:36:54 +08:00
|
|
|
if len(args) == 1 and (istestfunc(func) or
|
|
|
|
hasattr(func, '__bases__')):
|
2010-05-22 00:11:47 +08:00
|
|
|
if hasattr(func, '__bases__'):
|
2010-05-22 16:12:57 +08:00
|
|
|
if hasattr(func, 'pytestmark'):
|
|
|
|
l = func.pytestmark
|
|
|
|
if not isinstance(l, list):
|
2013-05-20 20:37:58 +08:00
|
|
|
func.pytestmark = [l, self]
|
2010-07-27 03:15:15 +08:00
|
|
|
else:
|
2013-05-20 20:37:58 +08:00
|
|
|
l.append(self)
|
2010-05-22 16:12:57 +08:00
|
|
|
else:
|
2013-05-20 20:37:58 +08:00
|
|
|
func.pytestmark = [self]
|
2009-10-22 21:21:58 +08:00
|
|
|
else:
|
2013-10-03 21:43:56 +08:00
|
|
|
holder = getattr(func, self.name, None)
|
2010-05-22 00:11:47 +08:00
|
|
|
if holder is None:
|
2013-05-20 20:37:58 +08:00
|
|
|
holder = MarkInfo(
|
2013-10-03 21:43:56 +08:00
|
|
|
self.name, self.args, self.kwargs
|
2013-05-20 20:37:58 +08:00
|
|
|
)
|
2013-10-03 21:43:56 +08:00
|
|
|
setattr(func, self.name, holder)
|
2010-05-22 00:11:47 +08:00
|
|
|
else:
|
2011-12-28 23:47:18 +08:00
|
|
|
holder.add(self.args, self.kwargs)
|
2009-10-15 22:18:57 +08:00
|
|
|
return func
|
2010-11-21 03:17:38 +08:00
|
|
|
kw = self.kwargs.copy()
|
|
|
|
kw.update(kwargs)
|
|
|
|
args = self.args + args
|
2013-10-03 21:43:56 +08:00
|
|
|
return self.__class__(self.name, args=args, kwargs=kw)
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2013-05-20 20:37:58 +08:00
|
|
|
|
2009-12-29 23:29:48 +08:00
|
|
|
class MarkInfo:
|
2010-10-14 01:30:00 +08:00
|
|
|
""" Marking object created by :class:`MarkDecorator` instances. """
|
2009-10-22 21:21:58 +08:00
|
|
|
def __init__(self, name, args, kwargs):
|
2010-10-14 01:30:00 +08:00
|
|
|
#: name of attribute
|
|
|
|
self.name = name
|
|
|
|
#: positional argument list, empty if none specified
|
2009-10-23 02:57:21 +08:00
|
|
|
self.args = args
|
2010-10-14 01:30:00 +08:00
|
|
|
#: keyword argument dictionary, empty if nothing specified
|
2009-10-23 02:57:21 +08:00
|
|
|
self.kwargs = kwargs
|
2011-12-28 23:47:18 +08:00
|
|
|
self._arglist = [(args, kwargs.copy())]
|
2009-10-23 02:57:21 +08:00
|
|
|
|
2009-10-22 21:21:58 +08:00
|
|
|
def __repr__(self):
|
2009-12-29 23:29:48 +08:00
|
|
|
return "<MarkInfo %r args=%r kwargs=%r>" % (
|
2013-05-20 20:37:58 +08:00
|
|
|
self.name, self.args, self.kwargs
|
|
|
|
)
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2011-12-28 23:47:18 +08:00
|
|
|
def add(self, args, kwargs):
|
|
|
|
""" add a MarkInfo with the given args and kwargs. """
|
|
|
|
self._arglist.append((args, kwargs))
|
|
|
|
self.args += args
|
|
|
|
self.kwargs.update(kwargs)
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
""" yield MarkInfo objects each relating to a marking-call. """
|
|
|
|
for args, kwargs in self._arglist:
|
|
|
|
yield MarkInfo(self.name, args, kwargs)
|