mark.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. """ generic mechanism for marking and selecting python functions. """
  2. import inspect
  3. class MarkerError(Exception):
  4. """Error in use of a pytest marker/attribute."""
  5. def pytest_namespace():
  6. return {'mark': MarkGenerator()}
  7. def pytest_addoption(parser):
  8. group = parser.getgroup("general")
  9. group._addoption(
  10. '-k',
  11. action="store", dest="keyword", default='', metavar="EXPRESSION",
  12. help="only run tests which match the given substring expression. "
  13. "An expression is a python evaluatable expression "
  14. "where all names are substring-matched against test names "
  15. "and their parent classes. Example: -k 'test_method or test "
  16. "other' matches all test functions and classes whose name "
  17. "contains 'test_method' or 'test_other'. "
  18. "Additionally keywords are matched to classes and functions "
  19. "containing extra names in their 'extra_keyword_matches' set, "
  20. "as well as functions which have names assigned directly to them."
  21. )
  22. group._addoption(
  23. "-m",
  24. action="store", dest="markexpr", default="", metavar="MARKEXPR",
  25. help="only run tests matching given mark expression. "
  26. "example: -m 'mark1 and not mark2'."
  27. )
  28. group.addoption(
  29. "--markers", action="store_true",
  30. help="show markers (builtin, plugin and per-project ones)."
  31. )
  32. parser.addini("markers", "markers for test functions", 'linelist')
  33. def pytest_cmdline_main(config):
  34. import _pytest.config
  35. if config.option.markers:
  36. config._do_configure()
  37. tw = _pytest.config.create_terminal_writer(config)
  38. for line in config.getini("markers"):
  39. name, rest = line.split(":", 1)
  40. tw.write("@pytest.mark.%s:" % name, bold=True)
  41. tw.line(rest)
  42. tw.line()
  43. config._ensure_unconfigure()
  44. return 0
  45. pytest_cmdline_main.tryfirst = True
  46. def pytest_collection_modifyitems(items, config):
  47. keywordexpr = config.option.keyword.lstrip()
  48. matchexpr = config.option.markexpr
  49. if not keywordexpr and not matchexpr:
  50. return
  51. # pytest used to allow "-" for negating
  52. # but today we just allow "-" at the beginning, use "not" instead
  53. # we probably remove "-" alltogether soon
  54. if keywordexpr.startswith("-"):
  55. keywordexpr = "not " + keywordexpr[1:]
  56. selectuntil = False
  57. if keywordexpr[-1:] == ":":
  58. selectuntil = True
  59. keywordexpr = keywordexpr[:-1]
  60. remaining = []
  61. deselected = []
  62. for colitem in items:
  63. if keywordexpr and not matchkeyword(colitem, keywordexpr):
  64. deselected.append(colitem)
  65. else:
  66. if selectuntil:
  67. keywordexpr = None
  68. if matchexpr:
  69. if not matchmark(colitem, matchexpr):
  70. deselected.append(colitem)
  71. continue
  72. remaining.append(colitem)
  73. if deselected:
  74. config.hook.pytest_deselected(items=deselected)
  75. items[:] = remaining
  76. class MarkMapping:
  77. """Provides a local mapping for markers where item access
  78. resolves to True if the marker is present. """
  79. def __init__(self, keywords):
  80. mymarks = set()
  81. for key, value in keywords.items():
  82. if isinstance(value, MarkInfo) or isinstance(value, MarkDecorator):
  83. mymarks.add(key)
  84. self._mymarks = mymarks
  85. def __getitem__(self, name):
  86. return name in self._mymarks
  87. class KeywordMapping:
  88. """Provides a local mapping for keywords.
  89. Given a list of names, map any substring of one of these names to True.
  90. """
  91. def __init__(self, names):
  92. self._names = names
  93. def __getitem__(self, subname):
  94. for name in self._names:
  95. if subname in name:
  96. return True
  97. return False
  98. def matchmark(colitem, markexpr):
  99. """Tries to match on any marker names, attached to the given colitem."""
  100. return eval(markexpr, {}, MarkMapping(colitem.keywords))
  101. def matchkeyword(colitem, keywordexpr):
  102. """Tries to match given keyword expression to given collector item.
  103. Will match on the name of colitem, including the names of its parents.
  104. Only matches names of items which are either a :class:`Class` or a
  105. :class:`Function`.
  106. Additionally, matches on names in the 'extra_keyword_matches' set of
  107. any item, as well as names directly assigned to test functions.
  108. """
  109. mapped_names = set()
  110. # Add the names of the current item and any parent items
  111. import pytest
  112. for item in colitem.listchain():
  113. if not isinstance(item, pytest.Instance):
  114. mapped_names.add(item.name)
  115. # Add the names added as extra keywords to current or parent items
  116. for name in colitem.listextrakeywords():
  117. mapped_names.add(name)
  118. # Add the names attached to the current function through direct assignment
  119. if hasattr(colitem, 'function'):
  120. for name in colitem.function.__dict__:
  121. mapped_names.add(name)
  122. mapping = KeywordMapping(mapped_names)
  123. if " " not in keywordexpr:
  124. # special case to allow for simple "-k pass" and "-k 1.3"
  125. return mapping[keywordexpr]
  126. elif keywordexpr.startswith("not ") and " " not in keywordexpr[4:]:
  127. return not mapping[keywordexpr[4:]]
  128. return eval(keywordexpr, {}, mapping)
  129. def pytest_configure(config):
  130. import pytest
  131. if config.option.strict:
  132. pytest.mark._config = config
  133. class MarkGenerator:
  134. """ Factory for :class:`MarkDecorator` objects - exposed as
  135. a ``pytest.mark`` singleton instance. Example::
  136. import pytest
  137. @pytest.mark.slowtest
  138. def test_function():
  139. pass
  140. will set a 'slowtest' :class:`MarkInfo` object
  141. on the ``test_function`` object. """
  142. def __getattr__(self, name):
  143. if name[0] == "_":
  144. raise AttributeError("Marker name must NOT start with underscore")
  145. if hasattr(self, '_config'):
  146. self._check(name)
  147. return MarkDecorator(name)
  148. def _check(self, name):
  149. try:
  150. if name in self._markers:
  151. return
  152. except AttributeError:
  153. pass
  154. self._markers = l = set()
  155. for line in self._config.getini("markers"):
  156. beginning = line.split(":", 1)
  157. x = beginning[0].split("(", 1)[0]
  158. l.add(x)
  159. if name not in self._markers:
  160. raise AttributeError("%r not a registered marker" % (name,))
  161. def istestfunc(func):
  162. return hasattr(func, "__call__") and \
  163. getattr(func, "__name__", "<lambda>") != "<lambda>"
  164. class MarkDecorator:
  165. """ A decorator for test functions and test classes. When applied
  166. it will create :class:`MarkInfo` objects which may be
  167. :ref:`retrieved by hooks as item keywords <excontrolskip>`.
  168. MarkDecorator instances are often created like this::
  169. mark1 = pytest.mark.NAME # simple MarkDecorator
  170. mark2 = pytest.mark.NAME(name1=value) # parametrized MarkDecorator
  171. and can then be applied as decorators to test functions::
  172. @mark2
  173. def test_function():
  174. pass
  175. When a MarkDecorator instance is called it does the following:
  176. 1. If called with a single class as its only positional argument and no
  177. additional keyword arguments, it attaches itself to the class so it
  178. gets applied automatically to all test cases found in that class.
  179. 2. If called with a single function as its only positional argument and
  180. no additional keyword arguments, it attaches a MarkInfo object to the
  181. function, containing all the arguments already stored internally in
  182. the MarkDecorator.
  183. 3. When called in any other case, it performs a 'fake construction' call,
  184. i.e. it returns a new MarkDecorator instance with the original
  185. MarkDecorator's content updated with the arguments passed to this
  186. call.
  187. Note: The rules above prevent MarkDecorator objects from storing only a
  188. single function or class reference as their positional argument with no
  189. additional keyword or positional arguments.
  190. """
  191. def __init__(self, name, args=None, kwargs=None):
  192. self.name = name
  193. self.args = args or ()
  194. self.kwargs = kwargs or {}
  195. @property
  196. def markname(self):
  197. return self.name # for backward-compat (2.4.1 had this attr)
  198. def __repr__(self):
  199. d = self.__dict__.copy()
  200. name = d.pop('name')
  201. return "<MarkDecorator %r %r>" % (name, d)
  202. def __call__(self, *args, **kwargs):
  203. """ if passed a single callable argument: decorate it with mark info.
  204. otherwise add *args/**kwargs in-place to mark information. """
  205. if args and not kwargs:
  206. func = args[0]
  207. is_class = inspect.isclass(func)
  208. if len(args) == 1 and (istestfunc(func) or is_class):
  209. if is_class:
  210. if hasattr(func, 'pytestmark'):
  211. mark_list = func.pytestmark
  212. if not isinstance(mark_list, list):
  213. mark_list = [mark_list]
  214. # always work on a copy to avoid updating pytestmark
  215. # from a superclass by accident
  216. mark_list = mark_list + [self]
  217. func.pytestmark = mark_list
  218. else:
  219. func.pytestmark = [self]
  220. else:
  221. holder = getattr(func, self.name, None)
  222. if holder is None:
  223. holder = MarkInfo(
  224. self.name, self.args, self.kwargs
  225. )
  226. setattr(func, self.name, holder)
  227. else:
  228. holder.add(self.args, self.kwargs)
  229. return func
  230. kw = self.kwargs.copy()
  231. kw.update(kwargs)
  232. args = self.args + args
  233. return self.__class__(self.name, args=args, kwargs=kw)
  234. class MarkInfo:
  235. """ Marking object created by :class:`MarkDecorator` instances. """
  236. def __init__(self, name, args, kwargs):
  237. #: name of attribute
  238. self.name = name
  239. #: positional argument list, empty if none specified
  240. self.args = args
  241. #: keyword argument dictionary, empty if nothing specified
  242. self.kwargs = kwargs.copy()
  243. self._arglist = [(args, kwargs.copy())]
  244. def __repr__(self):
  245. return "<MarkInfo %r args=%r kwargs=%r>" % (
  246. self.name, self.args, self.kwargs
  247. )
  248. def add(self, args, kwargs):
  249. """ add a MarkInfo with the given args and kwargs. """
  250. self._arglist.append((args, kwargs))
  251. self.args += args
  252. self.kwargs.update(kwargs)
  253. def __iter__(self):
  254. """ yield MarkInfo objects each relating to a marking-call. """
  255. for args, kwargs in self._arglist:
  256. yield MarkInfo(self.name, args, kwargs)