2010-10-13 18:26:14 +08:00
|
|
|
""" generic mechanism for marking and selecting python functions. """
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest, py
|
2009-06-28 19:19:43 +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
|
|
|
|
2010-10-12 19:05:29 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
group = parser.getgroup("general")
|
|
|
|
group._addoption('-k',
|
2010-10-26 05:08:56 +08:00
|
|
|
action="store", dest="keyword", default='', metavar="KEYWORDEXPR",
|
|
|
|
help="only run tests which match given keyword expression. "
|
|
|
|
"An expression consists of space-separated terms. "
|
|
|
|
"Each term must match. Precede a term with '-' to negate. "
|
|
|
|
"Terminate expression with ':' to make the first match match "
|
|
|
|
"all subsequent tests (usually file-order). ")
|
2010-10-12 19:05:29 +08:00
|
|
|
|
2011-11-12 07:02:06 +08:00
|
|
|
group._addoption("-m",
|
|
|
|
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'."
|
|
|
|
)
|
2011-11-12 07:02:06 +08:00
|
|
|
|
2011-11-12 06:56:11 +08:00
|
|
|
group.addoption("--markers", action="store_true", help=
|
|
|
|
"show markers (builtin, plugin and per-project ones).")
|
|
|
|
|
|
|
|
parser.addini("markers", "markers for test functions", 'linelist')
|
|
|
|
|
|
|
|
def pytest_cmdline_main(config):
|
|
|
|
if config.option.markers:
|
|
|
|
config.pluginmanager.do_configure(config)
|
|
|
|
tw = py.io.TerminalWriter()
|
|
|
|
for line in config.getini("markers"):
|
|
|
|
name, rest = line.split(":", 1)
|
|
|
|
tw.write("@pytest.mark.%s:" % name, bold=True)
|
|
|
|
tw.line(rest)
|
|
|
|
tw.line()
|
|
|
|
config.pluginmanager.do_unconfigure(config)
|
|
|
|
return 0
|
|
|
|
pytest_cmdline_main.tryfirst = True
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
2011-11-12 07:02:06 +08:00
|
|
|
class BoolDict:
|
|
|
|
def __init__(self, mydict):
|
|
|
|
self._mydict = mydict
|
|
|
|
def __getitem__(self, name):
|
|
|
|
return name in self._mydict
|
|
|
|
|
2012-11-09 19:29:33 +08:00
|
|
|
class SubstringDict:
|
|
|
|
def __init__(self, mydict):
|
|
|
|
self._mydict = mydict
|
|
|
|
def __getitem__(self, name):
|
|
|
|
for key in self._mydict:
|
|
|
|
if name in key:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2011-11-12 07:02:06 +08:00
|
|
|
def matchmark(colitem, matchexpr):
|
2012-11-09 19:07:41 +08:00
|
|
|
return eval(matchexpr, {}, BoolDict(colitem.keywords))
|
2011-11-12 07:02:06 +08:00
|
|
|
|
2012-11-09 19:29:33 +08:00
|
|
|
def matchkeyword(colitem, keywordexpr):
|
|
|
|
keywordexpr = keywordexpr.replace("-", "not ")
|
|
|
|
return eval(keywordexpr, {}, SubstringDict(colitem.keywords))
|
|
|
|
|
2011-11-12 06:56:11 +08:00
|
|
|
def pytest_configure(config):
|
|
|
|
if config.option.strict:
|
|
|
|
pytest.mark._config = config
|
|
|
|
|
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
|
2010-10-14 01:30:00 +08:00
|
|
|
a ``py.test.mark`` singleton instance. Example::
|
|
|
|
|
|
|
|
import py
|
|
|
|
@py.test.mark.slowtest
|
|
|
|
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] == "_":
|
|
|
|
raise AttributeError(name)
|
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,))
|
|
|
|
|
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
|
|
|
|
|
|
|
mark1 = py.test.mark.NAME # simple MarkDecorator
|
|
|
|
mark2 = py.test.mark.NAME(name1=value) # parametrized MarkDecorator
|
|
|
|
|
|
|
|
and can then be applied as decorators to test functions::
|
|
|
|
|
|
|
|
@mark2
|
|
|
|
def test_function():
|
|
|
|
pass
|
|
|
|
"""
|
2010-11-21 03:17:38 +08:00
|
|
|
def __init__(self, name, args=None, kwargs=None):
|
2009-07-28 20:26:32 +08:00
|
|
|
self.markname = 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
|
|
|
|
2009-07-28 20:26:32 +08:00
|
|
|
def __repr__(self):
|
|
|
|
d = self.__dict__.copy()
|
|
|
|
name = d.pop('markname')
|
2009-12-29 23:29:48 +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. """
|
2009-10-15 22:18:57 +08:00
|
|
|
if args:
|
2010-05-22 00:11:47 +08:00
|
|
|
func = args[0]
|
|
|
|
if len(args) == 1 and hasattr(func, '__call__') or \
|
|
|
|
hasattr(func, '__bases__'):
|
|
|
|
if hasattr(func, '__bases__'):
|
2010-05-22 16:12:57 +08:00
|
|
|
if hasattr(func, 'pytestmark'):
|
|
|
|
l = func.pytestmark
|
|
|
|
if not isinstance(l, list):
|
|
|
|
func.pytestmark = [l, self]
|
2010-07-27 03:15:15 +08:00
|
|
|
else:
|
2010-05-22 16:12:57 +08:00
|
|
|
l.append(self)
|
|
|
|
else:
|
|
|
|
func.pytestmark = [self]
|
2009-10-22 21:21:58 +08:00
|
|
|
else:
|
2010-05-22 00:11:47 +08:00
|
|
|
holder = getattr(func, self.markname, None)
|
|
|
|
if holder is None:
|
|
|
|
holder = MarkInfo(self.markname, self.args, self.kwargs)
|
|
|
|
setattr(func, self.markname, holder)
|
|
|
|
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
|
|
|
|
return self.__class__(self.markname, args=args, kwargs=kw)
|
2010-07-27 03:15:15 +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>" % (
|
2011-06-01 14:03:06 +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)
|
|
|
|
|