__init__.py 8.3 KB

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