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.

283 lines
8.7KB

  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.store import StoreKey
  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 = StoreKey[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. [("3+5", 8), pytest.param("6*9", 42, marks=pytest.mark.xfail),],
  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. :keyword marks: A single mark or a list of marks to be applied to this parameter set.
  54. :keyword str 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.\n"
  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. @attr.s(slots=True)
  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. _names = attr.ib(type=AbstractSet[str])
  122. @classmethod
  123. def from_item(cls, item: "Item") -> "KeywordMatcher":
  124. mapped_names = set()
  125. # Add the names of the current item and any parent items.
  126. import pytest
  127. for node in item.listchain():
  128. if not isinstance(node, (pytest.Instance, pytest.Session)):
  129. mapped_names.add(node.name)
  130. # Add the names added as extra keywords to current or parent items.
  131. mapped_names.update(item.listextrakeywords())
  132. # Add the names attached to the current function through direct assignment.
  133. function_obj = getattr(item, "function", None)
  134. if function_obj:
  135. mapped_names.update(function_obj.__dict__)
  136. # Add the markers to the keywords as we no longer handle them correctly.
  137. mapped_names.update(mark.name for mark in item.iter_markers())
  138. return cls(mapped_names)
  139. def __call__(self, subname: str) -> bool:
  140. subname = subname.lower()
  141. names = (name.lower() for name in self._names)
  142. for name in names:
  143. if subname in name:
  144. return True
  145. return False
  146. def deselect_by_keyword(items: "List[Item]", config: Config) -> None:
  147. keywordexpr = config.option.keyword.lstrip()
  148. if not keywordexpr:
  149. return
  150. if keywordexpr.startswith("-"):
  151. # To be removed in pytest 7.0.0.
  152. warnings.warn(MINUS_K_DASH, stacklevel=2)
  153. keywordexpr = "not " + keywordexpr[1:]
  154. selectuntil = False
  155. if keywordexpr[-1:] == ":":
  156. # To be removed in pytest 7.0.0.
  157. warnings.warn(MINUS_K_COLON, stacklevel=2)
  158. selectuntil = True
  159. keywordexpr = keywordexpr[:-1]
  160. try:
  161. expression = Expression.compile(keywordexpr)
  162. except ParseError as e:
  163. raise UsageError(
  164. f"Wrong expression passed to '-k': {keywordexpr}: {e}"
  165. ) from None
  166. remaining = []
  167. deselected = []
  168. for colitem in items:
  169. if keywordexpr and not expression.evaluate(KeywordMatcher.from_item(colitem)):
  170. deselected.append(colitem)
  171. else:
  172. if selectuntil:
  173. keywordexpr = None
  174. remaining.append(colitem)
  175. if deselected:
  176. config.hook.pytest_deselected(items=deselected)
  177. items[:] = remaining
  178. @attr.s(slots=True)
  179. class MarkMatcher:
  180. """A matcher for markers which are present.
  181. Tries to match on any marker names, attached to the given colitem.
  182. """
  183. own_mark_names = attr.ib()
  184. @classmethod
  185. def from_item(cls, item) -> "MarkMatcher":
  186. mark_names = {mark.name for mark in item.iter_markers()}
  187. return cls(mark_names)
  188. def __call__(self, name: str) -> bool:
  189. return name in self.own_mark_names
  190. def deselect_by_mark(items: "List[Item]", config: Config) -> None:
  191. matchexpr = config.option.markexpr
  192. if not matchexpr:
  193. return
  194. try:
  195. expression = Expression.compile(matchexpr)
  196. except ParseError as e:
  197. raise UsageError(f"Wrong expression passed to '-m': {matchexpr}: {e}") from None
  198. remaining = []
  199. deselected = []
  200. for item in items:
  201. if expression.evaluate(MarkMatcher.from_item(item)):
  202. remaining.append(item)
  203. else:
  204. deselected.append(item)
  205. if deselected:
  206. config.hook.pytest_deselected(items=deselected)
  207. items[:] = remaining
  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._store[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._store.get(old_mark_config_key, None)