__init__.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. """Generic mechanism for marking and selecting python functions."""
  2. import warnings
  3. from typing import AbstractSet
  4. from typing import Collection
  5. from typing import List
  6. from typing import Optional
  7. from typing import TYPE_CHECKING
  8. from typing import Union
  9. import attr
  10. from .expression import Expression
  11. from .expression import ParseError
  12. from .structures import EMPTY_PARAMETERSET_OPTION
  13. from .structures import get_empty_parameterset_mark
  14. from .structures import Mark
  15. from .structures import MARK_GEN
  16. from .structures import MarkDecorator
  17. from .structures import MarkGenerator
  18. from .structures import ParameterSet
  19. from _pytest.config import Config
  20. from _pytest.config import ExitCode
  21. from _pytest.config import hookimpl
  22. from _pytest.config import UsageError
  23. from _pytest.config.argparsing import Parser
  24. from _pytest.deprecated import MINUS_K_COLON
  25. from _pytest.deprecated import MINUS_K_DASH
  26. from _pytest.stash import StashKey
  27. if TYPE_CHECKING:
  28. from _pytest.nodes import Item
  29. __all__ = [
  30. "MARK_GEN",
  31. "Mark",
  32. "MarkDecorator",
  33. "MarkGenerator",
  34. "ParameterSet",
  35. "get_empty_parameterset_mark",
  36. ]
  37. old_mark_config_key = StashKey[Optional[Config]]()
  38. def param(
  39. *values: object,
  40. marks: Union[MarkDecorator, Collection[Union[MarkDecorator, Mark]]] = (),
  41. id: Optional[str] = None,
  42. ) -> ParameterSet:
  43. """Specify a parameter in `pytest.mark.parametrize`_ calls or
  44. :ref:`parametrized fixtures <fixture-parametrize-marks>`.
  45. .. code-block:: python
  46. @pytest.mark.parametrize(
  47. "test_input,expected",
  48. [
  49. ("3+5", 8),
  50. pytest.param("6*9", 42, marks=pytest.mark.xfail),
  51. ],
  52. )
  53. def test_eval(test_input, expected):
  54. assert eval(test_input) == expected
  55. :param values: Variable args of the values of the parameter set, in order.
  56. :keyword marks: A single mark or a list of marks to be applied to this parameter set.
  57. :keyword str id: The id to attribute to this parameter set.
  58. """
  59. return ParameterSet.param(*values, marks=marks, id=id)
  60. def pytest_addoption(parser: Parser) -> None:
  61. group = parser.getgroup("general")
  62. group._addoption(
  63. "-k",
  64. action="store",
  65. dest="keyword",
  66. default="",
  67. metavar="EXPRESSION",
  68. help="only run tests which match the given substring expression. "
  69. "An expression is a python evaluatable expression "
  70. "where all names are substring-matched against test names "
  71. "and their parent classes. Example: -k 'test_method or test_"
  72. "other' matches all test functions and classes whose name "
  73. "contains 'test_method' or 'test_other', while -k 'not test_method' "
  74. "matches those that don't contain 'test_method' in their names. "
  75. "-k 'not test_method and not test_other' will eliminate the matches. "
  76. "Additionally keywords are matched to classes and functions "
  77. "containing extra names in their 'extra_keyword_matches' set, "
  78. "as well as functions which have names assigned directly to them. "
  79. "The matching is case-insensitive.",
  80. )
  81. group._addoption(
  82. "-m",
  83. action="store",
  84. dest="markexpr",
  85. default="",
  86. metavar="MARKEXPR",
  87. help="only run tests matching given mark expression.\n"
  88. "For example: -m 'mark1 and not mark2'.",
  89. )
  90. group.addoption(
  91. "--markers",
  92. action="store_true",
  93. help="show markers (builtin, plugin and per-project ones).",
  94. )
  95. parser.addini("markers", "markers for test functions", "linelist")
  96. parser.addini(EMPTY_PARAMETERSET_OPTION, "default marker for empty parametersets")
  97. @hookimpl(tryfirst=True)
  98. def pytest_cmdline_main(config: Config) -> Optional[Union[int, ExitCode]]:
  99. import _pytest.config
  100. if config.option.markers:
  101. config._do_configure()
  102. tw = _pytest.config.create_terminal_writer(config)
  103. for line in config.getini("markers"):
  104. parts = line.split(":", 1)
  105. name = parts[0]
  106. rest = parts[1] if len(parts) == 2 else ""
  107. tw.write("@pytest.mark.%s:" % name, bold=True)
  108. tw.line(rest)
  109. tw.line()
  110. config._ensure_unconfigure()
  111. return 0
  112. return None
  113. @attr.s(slots=True, auto_attribs=True)
  114. class KeywordMatcher:
  115. """A matcher for keywords.
  116. Given a list of names, matches any substring of one of these names. The
  117. string inclusion check is case-insensitive.
  118. Will match on the name of colitem, including the names of its parents.
  119. Only matches names of items which are either a :class:`Class` or a
  120. :class:`Function`.
  121. Additionally, matches on names in the 'extra_keyword_matches' set of
  122. any item, as well as names directly assigned to test functions.
  123. """
  124. _names: AbstractSet[str]
  125. @classmethod
  126. def from_item(cls, item: "Item") -> "KeywordMatcher":
  127. mapped_names = set()
  128. # Add the names of the current item and any parent items.
  129. import pytest
  130. for node in item.listchain():
  131. if not isinstance(node, pytest.Session):
  132. mapped_names.add(node.name)
  133. # Add the names added as extra keywords to current or parent items.
  134. mapped_names.update(item.listextrakeywords())
  135. # Add the names attached to the current function through direct assignment.
  136. function_obj = getattr(item, "function", None)
  137. if function_obj:
  138. mapped_names.update(function_obj.__dict__)
  139. # Add the markers to the keywords as we no longer handle them correctly.
  140. mapped_names.update(mark.name for mark in item.iter_markers())
  141. return cls(mapped_names)
  142. def __call__(self, subname: str) -> bool:
  143. subname = subname.lower()
  144. names = (name.lower() for name in self._names)
  145. for name in names:
  146. if subname in name:
  147. return True
  148. return False
  149. def deselect_by_keyword(items: "List[Item]", config: Config) -> None:
  150. keywordexpr = config.option.keyword.lstrip()
  151. if not keywordexpr:
  152. return
  153. if keywordexpr.startswith("-"):
  154. # To be removed in pytest 8.0.0.
  155. warnings.warn(MINUS_K_DASH, stacklevel=2)
  156. keywordexpr = "not " + keywordexpr[1:]
  157. selectuntil = False
  158. if keywordexpr[-1:] == ":":
  159. # To be removed in pytest 8.0.0.
  160. warnings.warn(MINUS_K_COLON, stacklevel=2)
  161. selectuntil = True
  162. keywordexpr = keywordexpr[:-1]
  163. expr = _parse_expression(keywordexpr, "Wrong expression passed to '-k'")
  164. remaining = []
  165. deselected = []
  166. for colitem in items:
  167. if keywordexpr and not expr.evaluate(KeywordMatcher.from_item(colitem)):
  168. deselected.append(colitem)
  169. else:
  170. if selectuntil:
  171. keywordexpr = None
  172. remaining.append(colitem)
  173. if deselected:
  174. config.hook.pytest_deselected(items=deselected)
  175. items[:] = remaining
  176. @attr.s(slots=True, auto_attribs=True)
  177. class MarkMatcher:
  178. """A matcher for markers which are present.
  179. Tries to match on any marker names, attached to the given colitem.
  180. """
  181. own_mark_names: AbstractSet[str]
  182. @classmethod
  183. def from_item(cls, item: "Item") -> "MarkMatcher":
  184. mark_names = {mark.name for mark in item.iter_markers()}
  185. return cls(mark_names)
  186. def __call__(self, name: str) -> bool:
  187. return name in self.own_mark_names
  188. def deselect_by_mark(items: "List[Item]", config: Config) -> None:
  189. matchexpr = config.option.markexpr
  190. if not matchexpr:
  191. return
  192. expr = _parse_expression(matchexpr, "Wrong expression passed to '-m'")
  193. remaining: List[Item] = []
  194. deselected: List[Item] = []
  195. for item in items:
  196. if expr.evaluate(MarkMatcher.from_item(item)):
  197. remaining.append(item)
  198. else:
  199. deselected.append(item)
  200. if deselected:
  201. config.hook.pytest_deselected(items=deselected)
  202. items[:] = remaining
  203. def _parse_expression(expr: str, exc_message: str) -> Expression:
  204. try:
  205. return Expression.compile(expr)
  206. except ParseError as e:
  207. raise UsageError(f"{exc_message}: {expr}: {e}") from None
  208. def pytest_collection_modifyitems(items: "List[Item]", config: Config) -> None:
  209. deselect_by_keyword(items, config)
  210. deselect_by_mark(items, config)
  211. def pytest_configure(config: Config) -> None:
  212. config.stash[old_mark_config_key] = MARK_GEN._config
  213. MARK_GEN._config = config
  214. empty_parameterset = config.getini(EMPTY_PARAMETERSET_OPTION)
  215. if empty_parameterset not in ("skip", "xfail", "fail_at_collect", None, ""):
  216. raise UsageError(
  217. "{!s} must be one of skip, xfail or fail_at_collect"
  218. " but it is {!r}".format(EMPTY_PARAMETERSET_OPTION, empty_parameterset)
  219. )
  220. def pytest_unconfigure(config: Config) -> None:
  221. MARK_GEN._config = config.stash.get(old_mark_config_key, None)