You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

560 lines
18KB

  1. import collections.abc
  2. import inspect
  3. import warnings
  4. from typing import Any
  5. from typing import Callable
  6. from typing import Collection
  7. from typing import Iterable
  8. from typing import Iterator
  9. from typing import List
  10. from typing import Mapping
  11. from typing import MutableMapping
  12. from typing import NamedTuple
  13. from typing import Optional
  14. from typing import overload
  15. from typing import Sequence
  16. from typing import Set
  17. from typing import Tuple
  18. from typing import Type
  19. from typing import TYPE_CHECKING
  20. from typing import TypeVar
  21. from typing import Union
  22. import attr
  23. from .._code import getfslineno
  24. from ..compat import ascii_escaped
  25. from ..compat import final
  26. from ..compat import NOTSET
  27. from ..compat import NotSetType
  28. from _pytest.config import Config
  29. from _pytest.outcomes import fail
  30. from _pytest.warning_types import PytestUnknownMarkWarning
  31. if TYPE_CHECKING:
  32. from ..nodes import Node
  33. EMPTY_PARAMETERSET_OPTION = "empty_parameter_set_mark"
  34. def istestfunc(func) -> bool:
  35. return (
  36. hasattr(func, "__call__")
  37. and getattr(func, "__name__", "<lambda>") != "<lambda>"
  38. )
  39. def get_empty_parameterset_mark(
  40. config: Config, argnames: Sequence[str], func
  41. ) -> "MarkDecorator":
  42. from ..nodes import Collector
  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. requested_mark = config.getini(EMPTY_PARAMETERSET_OPTION)
  51. if requested_mark in ("", None, "skip"):
  52. mark = MARK_GEN.skip(reason=reason)
  53. elif requested_mark == "xfail":
  54. mark = MARK_GEN.xfail(reason=reason, run=False)
  55. elif requested_mark == "fail_at_collect":
  56. f_name = func.__name__
  57. _, lineno = getfslineno(func)
  58. raise Collector.CollectError(
  59. "Empty parameter set in '%s' at line %d" % (f_name, lineno + 1)
  60. )
  61. else:
  62. raise LookupError(requested_mark)
  63. return mark
  64. class ParameterSet(
  65. NamedTuple(
  66. "ParameterSet",
  67. [
  68. ("values", Sequence[Union[object, NotSetType]]),
  69. ("marks", Collection[Union["MarkDecorator", "Mark"]]),
  70. ("id", Optional[str]),
  71. ],
  72. )
  73. ):
  74. @classmethod
  75. def param(
  76. cls,
  77. *values: object,
  78. marks: Union["MarkDecorator", Collection[Union["MarkDecorator", "Mark"]]] = (),
  79. id: Optional[str] = None,
  80. ) -> "ParameterSet":
  81. if isinstance(marks, MarkDecorator):
  82. marks = (marks,)
  83. else:
  84. assert isinstance(marks, collections.abc.Collection)
  85. if id is not None:
  86. if not isinstance(id, str):
  87. raise TypeError(
  88. "Expected id to be a string, got {}: {!r}".format(type(id), id)
  89. )
  90. id = ascii_escaped(id)
  91. return cls(values, marks, id)
  92. @classmethod
  93. def extract_from(
  94. cls,
  95. parameterset: Union["ParameterSet", Sequence[object], object],
  96. force_tuple: bool = False,
  97. ) -> "ParameterSet":
  98. """Extract from an object or objects.
  99. :param parameterset:
  100. A legacy style parameterset that may or may not be a tuple,
  101. and may or may not be wrapped into a mess of mark objects.
  102. :param force_tuple:
  103. Enforce tuple wrapping so single argument tuple values
  104. don't get decomposed and break tests.
  105. """
  106. if isinstance(parameterset, cls):
  107. return parameterset
  108. if force_tuple:
  109. return cls.param(parameterset)
  110. else:
  111. # TODO: Refactor to fix this type-ignore. Currently the following
  112. # passes type-checking but crashes:
  113. #
  114. # @pytest.mark.parametrize(('x', 'y'), [1, 2])
  115. # def test_foo(x, y): pass
  116. return cls(parameterset, marks=[], id=None) # type: ignore[arg-type]
  117. @staticmethod
  118. def _parse_parametrize_args(
  119. argnames: Union[str, List[str], Tuple[str, ...]],
  120. argvalues: Iterable[Union["ParameterSet", Sequence[object], object]],
  121. *args,
  122. **kwargs,
  123. ) -> Tuple[Union[List[str], Tuple[str, ...]], bool]:
  124. if not isinstance(argnames, (tuple, list)):
  125. argnames = [x.strip() for x in argnames.split(",") if x.strip()]
  126. force_tuple = len(argnames) == 1
  127. else:
  128. force_tuple = False
  129. return argnames, force_tuple
  130. @staticmethod
  131. def _parse_parametrize_parameters(
  132. argvalues: Iterable[Union["ParameterSet", Sequence[object], object]],
  133. force_tuple: bool,
  134. ) -> List["ParameterSet"]:
  135. return [
  136. ParameterSet.extract_from(x, force_tuple=force_tuple) for x in argvalues
  137. ]
  138. @classmethod
  139. def _for_parametrize(
  140. cls,
  141. argnames: Union[str, List[str], Tuple[str, ...]],
  142. argvalues: Iterable[Union["ParameterSet", Sequence[object], object]],
  143. func,
  144. config: Config,
  145. nodeid: str,
  146. ) -> Tuple[Union[List[str], Tuple[str, ...]], List["ParameterSet"]]:
  147. argnames, force_tuple = cls._parse_parametrize_args(argnames, argvalues)
  148. parameters = cls._parse_parametrize_parameters(argvalues, force_tuple)
  149. del argvalues
  150. if parameters:
  151. # Check all parameter sets have the correct number of values.
  152. for param in parameters:
  153. if len(param.values) != len(argnames):
  154. msg = (
  155. '{nodeid}: in "parametrize" the number of names ({names_len}):\n'
  156. " {names}\n"
  157. "must be equal to the number of values ({values_len}):\n"
  158. " {values}"
  159. )
  160. fail(
  161. msg.format(
  162. nodeid=nodeid,
  163. values=param.values,
  164. names=argnames,
  165. names_len=len(argnames),
  166. values_len=len(param.values),
  167. ),
  168. pytrace=False,
  169. )
  170. else:
  171. # Empty parameter set (likely computed at runtime): create a single
  172. # parameter set with NOTSET values, with the "empty parameter set" mark applied to it.
  173. mark = get_empty_parameterset_mark(config, argnames, func)
  174. parameters.append(
  175. ParameterSet(values=(NOTSET,) * len(argnames), marks=[mark], id=None)
  176. )
  177. return argnames, parameters
  178. @final
  179. @attr.s(frozen=True)
  180. class Mark:
  181. #: Name of the mark.
  182. name = attr.ib(type=str)
  183. #: Positional arguments of the mark decorator.
  184. args = attr.ib(type=Tuple[Any, ...])
  185. #: Keyword arguments of the mark decorator.
  186. kwargs = attr.ib(type=Mapping[str, Any])
  187. #: Source Mark for ids with parametrize Marks.
  188. _param_ids_from = attr.ib(type=Optional["Mark"], default=None, repr=False)
  189. #: Resolved/generated ids with parametrize Marks.
  190. _param_ids_generated = attr.ib(
  191. type=Optional[Sequence[str]], default=None, repr=False
  192. )
  193. def _has_param_ids(self) -> bool:
  194. return "ids" in self.kwargs or len(self.args) >= 4
  195. def combined_with(self, other: "Mark") -> "Mark":
  196. """Return a new Mark which is a combination of this
  197. Mark and another Mark.
  198. Combines by appending args and merging kwargs.
  199. :param Mark other: The mark to combine with.
  200. :rtype: Mark
  201. """
  202. assert self.name == other.name
  203. # Remember source of ids with parametrize Marks.
  204. param_ids_from: Optional[Mark] = None
  205. if self.name == "parametrize":
  206. if other._has_param_ids():
  207. param_ids_from = other
  208. elif self._has_param_ids():
  209. param_ids_from = self
  210. return Mark(
  211. self.name,
  212. self.args + other.args,
  213. dict(self.kwargs, **other.kwargs),
  214. param_ids_from=param_ids_from,
  215. )
  216. # A generic parameter designating an object to which a Mark may
  217. # be applied -- a test function (callable) or class.
  218. # Note: a lambda is not allowed, but this can't be represented.
  219. _Markable = TypeVar("_Markable", bound=Union[Callable[..., object], type])
  220. @attr.s
  221. class MarkDecorator:
  222. """A decorator for applying a mark on test functions and classes.
  223. MarkDecorators are created with ``pytest.mark``::
  224. mark1 = pytest.mark.NAME # Simple MarkDecorator
  225. mark2 = pytest.mark.NAME(name1=value) # Parametrized MarkDecorator
  226. and can then be applied as decorators to test functions::
  227. @mark2
  228. def test_function():
  229. pass
  230. When a MarkDecorator is called it does the following:
  231. 1. If called with a single class as its only positional argument and no
  232. additional keyword arguments, it attaches the mark to the class so it
  233. gets applied automatically to all test cases found in that class.
  234. 2. If called with a single function as its only positional argument and
  235. no additional keyword arguments, it attaches the mark to the function,
  236. containing all the arguments already stored internally in the
  237. MarkDecorator.
  238. 3. When called in any other case, it returns a new MarkDecorator instance
  239. with the original MarkDecorator's content updated with the arguments
  240. passed to this call.
  241. Note: The rules above prevent MarkDecorators from storing only a single
  242. function or class reference as their positional argument with no
  243. additional keyword or positional arguments. You can work around this by
  244. using `with_args()`.
  245. """
  246. mark = attr.ib(type=Mark, validator=attr.validators.instance_of(Mark))
  247. @property
  248. def name(self) -> str:
  249. """Alias for mark.name."""
  250. return self.mark.name
  251. @property
  252. def args(self) -> Tuple[Any, ...]:
  253. """Alias for mark.args."""
  254. return self.mark.args
  255. @property
  256. def kwargs(self) -> Mapping[str, Any]:
  257. """Alias for mark.kwargs."""
  258. return self.mark.kwargs
  259. @property
  260. def markname(self) -> str:
  261. return self.name # for backward-compat (2.4.1 had this attr)
  262. def __repr__(self) -> str:
  263. return f"<MarkDecorator {self.mark!r}>"
  264. def with_args(self, *args: object, **kwargs: object) -> "MarkDecorator":
  265. """Return a MarkDecorator with extra arguments added.
  266. Unlike calling the MarkDecorator, with_args() can be used even
  267. if the sole argument is a callable/class.
  268. :rtype: MarkDecorator
  269. """
  270. mark = Mark(self.name, args, kwargs)
  271. return self.__class__(self.mark.combined_with(mark))
  272. # Type ignored because the overloads overlap with an incompatible
  273. # return type. Not much we can do about that. Thankfully mypy picks
  274. # the first match so it works out even if we break the rules.
  275. @overload
  276. def __call__(self, arg: _Markable) -> _Markable: # type: ignore[misc]
  277. pass
  278. @overload
  279. def __call__(self, *args: object, **kwargs: object) -> "MarkDecorator":
  280. pass
  281. def __call__(self, *args: object, **kwargs: object):
  282. """Call the MarkDecorator."""
  283. if args and not kwargs:
  284. func = args[0]
  285. is_class = inspect.isclass(func)
  286. if len(args) == 1 and (istestfunc(func) or is_class):
  287. store_mark(func, self.mark)
  288. return func
  289. return self.with_args(*args, **kwargs)
  290. def get_unpacked_marks(obj) -> List[Mark]:
  291. """Obtain the unpacked marks that are stored on an object."""
  292. mark_list = getattr(obj, "pytestmark", [])
  293. if not isinstance(mark_list, list):
  294. mark_list = [mark_list]
  295. return normalize_mark_list(mark_list)
  296. def normalize_mark_list(mark_list: Iterable[Union[Mark, MarkDecorator]]) -> List[Mark]:
  297. """Normalize marker decorating helpers to mark objects.
  298. :type List[Union[Mark, Markdecorator]] mark_list:
  299. :rtype: List[Mark]
  300. """
  301. extracted = [
  302. getattr(mark, "mark", mark) for mark in mark_list
  303. ] # unpack MarkDecorator
  304. for mark in extracted:
  305. if not isinstance(mark, Mark):
  306. raise TypeError(f"got {mark!r} instead of Mark")
  307. return [x for x in extracted if isinstance(x, Mark)]
  308. def store_mark(obj, mark: Mark) -> None:
  309. """Store a Mark on an object.
  310. This is used to implement the Mark declarations/decorators correctly.
  311. """
  312. assert isinstance(mark, Mark), mark
  313. # Always reassign name to avoid updating pytestmark in a reference that
  314. # was only borrowed.
  315. obj.pytestmark = get_unpacked_marks(obj) + [mark]
  316. # Typing for builtin pytest marks. This is cheating; it gives builtin marks
  317. # special privilege, and breaks modularity. But practicality beats purity...
  318. if TYPE_CHECKING:
  319. from _pytest.fixtures import _Scope
  320. class _SkipMarkDecorator(MarkDecorator):
  321. @overload # type: ignore[override,misc]
  322. def __call__(self, arg: _Markable) -> _Markable:
  323. ...
  324. @overload
  325. def __call__(self, reason: str = ...) -> "MarkDecorator":
  326. ...
  327. class _SkipifMarkDecorator(MarkDecorator):
  328. def __call__( # type: ignore[override]
  329. self,
  330. condition: Union[str, bool] = ...,
  331. *conditions: Union[str, bool],
  332. reason: str = ...,
  333. ) -> MarkDecorator:
  334. ...
  335. class _XfailMarkDecorator(MarkDecorator):
  336. @overload # type: ignore[override,misc]
  337. def __call__(self, arg: _Markable) -> _Markable:
  338. ...
  339. @overload
  340. def __call__(
  341. self,
  342. condition: Union[str, bool] = ...,
  343. *conditions: Union[str, bool],
  344. reason: str = ...,
  345. run: bool = ...,
  346. raises: Union[Type[BaseException], Tuple[Type[BaseException], ...]] = ...,
  347. strict: bool = ...,
  348. ) -> MarkDecorator:
  349. ...
  350. class _ParametrizeMarkDecorator(MarkDecorator):
  351. def __call__( # type: ignore[override]
  352. self,
  353. argnames: Union[str, List[str], Tuple[str, ...]],
  354. argvalues: Iterable[Union[ParameterSet, Sequence[object], object]],
  355. *,
  356. indirect: Union[bool, Sequence[str]] = ...,
  357. ids: Optional[
  358. Union[
  359. Iterable[Union[None, str, float, int, bool]],
  360. Callable[[Any], Optional[object]],
  361. ]
  362. ] = ...,
  363. scope: Optional[_Scope] = ...,
  364. ) -> MarkDecorator:
  365. ...
  366. class _UsefixturesMarkDecorator(MarkDecorator):
  367. def __call__( # type: ignore[override]
  368. self, *fixtures: str
  369. ) -> MarkDecorator:
  370. ...
  371. class _FilterwarningsMarkDecorator(MarkDecorator):
  372. def __call__( # type: ignore[override]
  373. self, *filters: str
  374. ) -> MarkDecorator:
  375. ...
  376. @final
  377. class MarkGenerator:
  378. """Factory for :class:`MarkDecorator` objects - exposed as
  379. a ``pytest.mark`` singleton instance.
  380. Example::
  381. import pytest
  382. @pytest.mark.slowtest
  383. def test_function():
  384. pass
  385. applies a 'slowtest' :class:`Mark` on ``test_function``.
  386. """
  387. _config: Optional[Config] = None
  388. _markers: Set[str] = set()
  389. # See TYPE_CHECKING above.
  390. if TYPE_CHECKING:
  391. skip: _SkipMarkDecorator
  392. skipif: _SkipifMarkDecorator
  393. xfail: _XfailMarkDecorator
  394. parametrize: _ParametrizeMarkDecorator
  395. usefixtures: _UsefixturesMarkDecorator
  396. filterwarnings: _FilterwarningsMarkDecorator
  397. def __getattr__(self, name: str) -> MarkDecorator:
  398. if name[0] == "_":
  399. raise AttributeError("Marker name must NOT start with underscore")
  400. if self._config is not None:
  401. # We store a set of markers as a performance optimisation - if a mark
  402. # name is in the set we definitely know it, but a mark may be known and
  403. # not in the set. We therefore start by updating the set!
  404. if name not in self._markers:
  405. for line in self._config.getini("markers"):
  406. # example lines: "skipif(condition): skip the given test if..."
  407. # or "hypothesis: tests which use Hypothesis", so to get the
  408. # marker name we split on both `:` and `(`.
  409. marker = line.split(":")[0].split("(")[0].strip()
  410. self._markers.add(marker)
  411. # If the name is not in the set of known marks after updating,
  412. # then it really is time to issue a warning or an error.
  413. if name not in self._markers:
  414. if self._config.option.strict_markers or self._config.option.strict:
  415. fail(
  416. f"{name!r} not found in `markers` configuration option",
  417. pytrace=False,
  418. )
  419. # Raise a specific error for common misspellings of "parametrize".
  420. if name in ["parameterize", "parametrise", "parameterise"]:
  421. __tracebackhide__ = True
  422. fail(f"Unknown '{name}' mark, did you mean 'parametrize'?")
  423. warnings.warn(
  424. "Unknown pytest.mark.%s - is this a typo? You can register "
  425. "custom marks to avoid this warning - for details, see "
  426. "https://docs.pytest.org/en/stable/mark.html" % name,
  427. PytestUnknownMarkWarning,
  428. 2,
  429. )
  430. return MarkDecorator(Mark(name, (), {}))
  431. MARK_GEN = MarkGenerator()
  432. @final
  433. class NodeKeywords(MutableMapping[str, Any]):
  434. def __init__(self, node: "Node") -> None:
  435. self.node = node
  436. self.parent = node.parent
  437. self._markers = {node.name: True}
  438. def __getitem__(self, key: str) -> Any:
  439. try:
  440. return self._markers[key]
  441. except KeyError:
  442. if self.parent is None:
  443. raise
  444. return self.parent.keywords[key]
  445. def __setitem__(self, key: str, value: Any) -> None:
  446. self._markers[key] = value
  447. def __delitem__(self, key: str) -> None:
  448. raise ValueError("cannot delete key in keywords dict")
  449. def __iter__(self) -> Iterator[str]:
  450. seen = self._seen()
  451. return iter(seen)
  452. def _seen(self) -> Set[str]:
  453. seen = set(self._markers)
  454. if self.parent is not None:
  455. seen.update(self.parent.keywords)
  456. return seen
  457. def __len__(self) -> int:
  458. return len(self._seen())
  459. def __repr__(self) -> str:
  460. return f"<NodeKeywords for node {self.node}>"