structures.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. # -*- coding: utf-8 -*-
  2. import inspect
  3. import warnings
  4. from collections import namedtuple
  5. from operator import attrgetter
  6. import attr
  7. import six
  8. from ..compat import ascii_escaped
  9. from ..compat import ATTRS_EQ_FIELD
  10. from ..compat import getfslineno
  11. from ..compat import MappingMixin
  12. from ..compat import NOTSET
  13. from _pytest.deprecated import PYTEST_PARAM_UNKNOWN_KWARGS
  14. from _pytest.outcomes import fail
  15. from _pytest.warning_types import PytestUnknownMarkWarning
  16. EMPTY_PARAMETERSET_OPTION = "empty_parameter_set_mark"
  17. def alias(name, warning=None):
  18. getter = attrgetter(name)
  19. def warned(self):
  20. warnings.warn(warning, stacklevel=2)
  21. return getter(self)
  22. return property(getter if warning is None else warned, doc="alias for " + name)
  23. def istestfunc(func):
  24. return (
  25. hasattr(func, "__call__")
  26. and getattr(func, "__name__", "<lambda>") != "<lambda>"
  27. )
  28. def get_empty_parameterset_mark(config, argnames, func):
  29. from ..nodes import Collector
  30. requested_mark = config.getini(EMPTY_PARAMETERSET_OPTION)
  31. if requested_mark in ("", None, "skip"):
  32. mark = MARK_GEN.skip
  33. elif requested_mark == "xfail":
  34. mark = MARK_GEN.xfail(run=False)
  35. elif requested_mark == "fail_at_collect":
  36. f_name = func.__name__
  37. _, lineno = getfslineno(func)
  38. raise Collector.CollectError(
  39. "Empty parameter set in '%s' at line %d" % (f_name, lineno + 1)
  40. )
  41. else:
  42. raise LookupError(requested_mark)
  43. fs, lineno = getfslineno(func)
  44. reason = "got empty parameter set %r, function %s at %s:%d" % (
  45. argnames,
  46. func.__name__,
  47. fs,
  48. lineno,
  49. )
  50. return mark(reason=reason)
  51. class ParameterSet(namedtuple("ParameterSet", "values, marks, id")):
  52. @classmethod
  53. def param(cls, *values, **kwargs):
  54. marks = kwargs.pop("marks", ())
  55. if isinstance(marks, MarkDecorator):
  56. marks = (marks,)
  57. else:
  58. assert isinstance(marks, (tuple, list, set))
  59. id_ = kwargs.pop("id", None)
  60. if id_ is not None:
  61. if not isinstance(id_, six.string_types):
  62. raise TypeError(
  63. "Expected id to be a string, got {}: {!r}".format(type(id_), id_)
  64. )
  65. id_ = ascii_escaped(id_)
  66. if kwargs:
  67. warnings.warn(
  68. PYTEST_PARAM_UNKNOWN_KWARGS.format(args=sorted(kwargs)), stacklevel=3
  69. )
  70. return cls(values, marks, id_)
  71. @classmethod
  72. def extract_from(cls, parameterset, force_tuple=False):
  73. """
  74. :param parameterset:
  75. a legacy style parameterset that may or may not be a tuple,
  76. and may or may not be wrapped into a mess of mark objects
  77. :param force_tuple:
  78. enforce tuple wrapping so single argument tuple values
  79. don't get decomposed and break tests
  80. """
  81. if isinstance(parameterset, cls):
  82. return parameterset
  83. if force_tuple:
  84. return cls.param(parameterset)
  85. else:
  86. return cls(parameterset, marks=[], id=None)
  87. @staticmethod
  88. def _parse_parametrize_args(argnames, argvalues, *args, **kwargs):
  89. if not isinstance(argnames, (tuple, list)):
  90. argnames = [x.strip() for x in argnames.split(",") if x.strip()]
  91. force_tuple = len(argnames) == 1
  92. else:
  93. force_tuple = False
  94. return argnames, force_tuple
  95. @staticmethod
  96. def _parse_parametrize_parameters(argvalues, force_tuple):
  97. return [
  98. ParameterSet.extract_from(x, force_tuple=force_tuple) for x in argvalues
  99. ]
  100. @classmethod
  101. def _for_parametrize(cls, argnames, argvalues, func, config, function_definition):
  102. argnames, force_tuple = cls._parse_parametrize_args(argnames, argvalues)
  103. parameters = cls._parse_parametrize_parameters(argvalues, force_tuple)
  104. del argvalues
  105. if parameters:
  106. # check all parameter sets have the correct number of values
  107. for param in parameters:
  108. if len(param.values) != len(argnames):
  109. msg = (
  110. '{nodeid}: in "parametrize" the number of names ({names_len}):\n'
  111. " {names}\n"
  112. "must be equal to the number of values ({values_len}):\n"
  113. " {values}"
  114. )
  115. fail(
  116. msg.format(
  117. nodeid=function_definition.nodeid,
  118. values=param.values,
  119. names=argnames,
  120. names_len=len(argnames),
  121. values_len=len(param.values),
  122. ),
  123. pytrace=False,
  124. )
  125. else:
  126. # empty parameter set (likely computed at runtime): create a single
  127. # parameter set with NOTSET values, with the "empty parameter set" mark applied to it
  128. mark = get_empty_parameterset_mark(config, argnames, func)
  129. parameters.append(
  130. ParameterSet(values=(NOTSET,) * len(argnames), marks=[mark], id=None)
  131. )
  132. return argnames, parameters
  133. @attr.s(frozen=True)
  134. class Mark(object):
  135. #: name of the mark
  136. name = attr.ib(type=str)
  137. #: positional arguments of the mark decorator
  138. args = attr.ib() # List[object]
  139. #: keyword arguments of the mark decorator
  140. kwargs = attr.ib() # Dict[str, object]
  141. def combined_with(self, other):
  142. """
  143. :param other: the mark to combine with
  144. :type other: Mark
  145. :rtype: Mark
  146. combines by appending args and merging the mappings
  147. """
  148. assert self.name == other.name
  149. return Mark(
  150. self.name, self.args + other.args, dict(self.kwargs, **other.kwargs)
  151. )
  152. @attr.s
  153. class MarkDecorator(object):
  154. """ A decorator for test functions and test classes. When applied
  155. it will create :class:`MarkInfo` objects which may be
  156. :ref:`retrieved by hooks as item keywords <excontrolskip>`.
  157. MarkDecorator instances are often created like this::
  158. mark1 = pytest.mark.NAME # simple MarkDecorator
  159. mark2 = pytest.mark.NAME(name1=value) # parametrized MarkDecorator
  160. and can then be applied as decorators to test functions::
  161. @mark2
  162. def test_function():
  163. pass
  164. When a MarkDecorator instance is called it does the following:
  165. 1. If called with a single class as its only positional argument and no
  166. additional keyword arguments, it attaches itself to the class so it
  167. gets applied automatically to all test cases found in that class.
  168. 2. If called with a single function as its only positional argument and
  169. no additional keyword arguments, it attaches a MarkInfo object to the
  170. function, containing all the arguments already stored internally in
  171. the MarkDecorator.
  172. 3. When called in any other case, it performs a 'fake construction' call,
  173. i.e. it returns a new MarkDecorator instance with the original
  174. MarkDecorator's content updated with the arguments passed to this
  175. call.
  176. Note: The rules above prevent MarkDecorator objects from storing only a
  177. single function or class reference as their positional argument with no
  178. additional keyword or positional arguments.
  179. """
  180. mark = attr.ib(validator=attr.validators.instance_of(Mark))
  181. name = alias("mark.name")
  182. args = alias("mark.args")
  183. kwargs = alias("mark.kwargs")
  184. @property
  185. def markname(self):
  186. return self.name # for backward-compat (2.4.1 had this attr)
  187. def __eq__(self, other):
  188. return self.mark == other.mark if isinstance(other, MarkDecorator) else False
  189. def __repr__(self):
  190. return "<MarkDecorator %r>" % (self.mark,)
  191. def with_args(self, *args, **kwargs):
  192. """ return a MarkDecorator with extra arguments added
  193. unlike call this can be used even if the sole argument is a callable/class
  194. :return: MarkDecorator
  195. """
  196. mark = Mark(self.name, args, kwargs)
  197. return self.__class__(self.mark.combined_with(mark))
  198. def __call__(self, *args, **kwargs):
  199. """ if passed a single callable argument: decorate it with mark info.
  200. otherwise add *args/**kwargs in-place to mark information. """
  201. if args and not kwargs:
  202. func = args[0]
  203. is_class = inspect.isclass(func)
  204. if len(args) == 1 and (istestfunc(func) or is_class):
  205. store_mark(func, self.mark)
  206. return func
  207. return self.with_args(*args, **kwargs)
  208. def get_unpacked_marks(obj):
  209. """
  210. obtain the unpacked marks that are stored on an object
  211. """
  212. mark_list = getattr(obj, "pytestmark", [])
  213. if not isinstance(mark_list, list):
  214. mark_list = [mark_list]
  215. return normalize_mark_list(mark_list)
  216. def normalize_mark_list(mark_list):
  217. """
  218. normalizes marker decorating helpers to mark objects
  219. :type mark_list: List[Union[Mark, Markdecorator]]
  220. :rtype: List[Mark]
  221. """
  222. extracted = [
  223. getattr(mark, "mark", mark) for mark in mark_list
  224. ] # unpack MarkDecorator
  225. for mark in extracted:
  226. if not isinstance(mark, Mark):
  227. raise TypeError("got {!r} instead of Mark".format(mark))
  228. return [x for x in extracted if isinstance(x, Mark)]
  229. def store_mark(obj, mark):
  230. """store a Mark on an object
  231. this is used to implement the Mark declarations/decorators correctly
  232. """
  233. assert isinstance(mark, Mark), mark
  234. # always reassign name to avoid updating pytestmark
  235. # in a reference that was only borrowed
  236. obj.pytestmark = get_unpacked_marks(obj) + [mark]
  237. class MarkGenerator(object):
  238. """ Factory for :class:`MarkDecorator` objects - exposed as
  239. a ``pytest.mark`` singleton instance. Example::
  240. import pytest
  241. @pytest.mark.slowtest
  242. def test_function():
  243. pass
  244. will set a 'slowtest' :class:`MarkInfo` object
  245. on the ``test_function`` object. """
  246. _config = None
  247. _markers = set()
  248. def __getattr__(self, name):
  249. if name[0] == "_":
  250. raise AttributeError("Marker name must NOT start with underscore")
  251. if self._config is not None:
  252. # We store a set of markers as a performance optimisation - if a mark
  253. # name is in the set we definitely know it, but a mark may be known and
  254. # not in the set. We therefore start by updating the set!
  255. if name not in self._markers:
  256. for line in self._config.getini("markers"):
  257. # example lines: "skipif(condition): skip the given test if..."
  258. # or "hypothesis: tests which use Hypothesis", so to get the
  259. # marker name we split on both `:` and `(`.
  260. if line == "ya:external":
  261. marker = line
  262. else:
  263. marker = line.split(":")[0].split("(")[0].strip()
  264. self._markers.add(marker)
  265. # If the name is not in the set of known marks after updating,
  266. # then it really is time to issue a warning or an error.
  267. if name not in self._markers:
  268. if self._config.option.strict_markers:
  269. fail(
  270. "{!r} not found in `markers` configuration option".format(name),
  271. pytrace=False,
  272. )
  273. else:
  274. warnings.warn(
  275. "Unknown pytest.mark.%s - is this a typo? You can register "
  276. "custom marks to avoid this warning - for details, see "
  277. "https://docs.pytest.org/en/latest/mark.html" % name,
  278. PytestUnknownMarkWarning,
  279. )
  280. return MarkDecorator(Mark(name, (), {}))
  281. MARK_GEN = MarkGenerator()
  282. class NodeKeywords(MappingMixin):
  283. def __init__(self, node):
  284. self.node = node
  285. self.parent = node.parent
  286. self._markers = {node.name: True}
  287. def __getitem__(self, key):
  288. try:
  289. return self._markers[key]
  290. except KeyError:
  291. if self.parent is None:
  292. raise
  293. return self.parent.keywords[key]
  294. def __setitem__(self, key, value):
  295. self._markers[key] = value
  296. def __delitem__(self, key):
  297. raise ValueError("cannot delete key in keywords dict")
  298. def __iter__(self):
  299. seen = self._seen()
  300. return iter(seen)
  301. def _seen(self):
  302. seen = set(self._markers)
  303. if self.parent is not None:
  304. seen.update(self.parent.keywords)
  305. return seen
  306. def __len__(self):
  307. return len(self._seen())
  308. def __repr__(self):
  309. return "<NodeKeywords for node %s>" % (self.node,)
  310. # mypy cannot find this overload, remove when on attrs>=19.2
  311. @attr.s(hash=False, **{ATTRS_EQ_FIELD: False}) # type: ignore
  312. class NodeMarkers(object):
  313. """
  314. internal structure for storing marks belonging to a node
  315. ..warning::
  316. unstable api
  317. """
  318. own_markers = attr.ib(default=attr.Factory(list))
  319. def update(self, add_markers):
  320. """update the own markers
  321. """
  322. self.own_markers.extend(add_markers)
  323. def find(self, name):
  324. """
  325. find markers in own nodes or parent nodes
  326. needs a better place
  327. """
  328. for mark in self.own_markers:
  329. if mark.name == name:
  330. yield mark
  331. def __iter__(self):
  332. return iter(self.own_markers)