outcomes.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. """Exception classes and constants handling test outcomes as well as
  2. functions creating them."""
  3. import sys
  4. import warnings
  5. from typing import Any
  6. from typing import Callable
  7. from typing import cast
  8. from typing import Optional
  9. from typing import Type
  10. from typing import TypeVar
  11. from _pytest.deprecated import KEYWORD_MSG_ARG
  12. TYPE_CHECKING = False # Avoid circular import through compat.
  13. if TYPE_CHECKING:
  14. from typing import NoReturn
  15. from typing_extensions import Protocol
  16. else:
  17. # typing.Protocol is only available starting from Python 3.8. It is also
  18. # available from typing_extensions, but we don't want a runtime dependency
  19. # on that. So use a dummy runtime implementation.
  20. from typing import Generic
  21. Protocol = Generic
  22. class OutcomeException(BaseException):
  23. """OutcomeException and its subclass instances indicate and contain info
  24. about test and collection outcomes."""
  25. def __init__(self, msg: Optional[str] = None, pytrace: bool = True) -> None:
  26. if msg is not None and not isinstance(msg, str):
  27. error_msg = ( # type: ignore[unreachable]
  28. "{} expected string as 'msg' parameter, got '{}' instead.\n"
  29. "Perhaps you meant to use a mark?"
  30. )
  31. raise TypeError(error_msg.format(type(self).__name__, type(msg).__name__))
  32. super().__init__(msg)
  33. self.msg = msg
  34. self.pytrace = pytrace
  35. def __repr__(self) -> str:
  36. if self.msg is not None:
  37. return self.msg
  38. return f"<{self.__class__.__name__} instance>"
  39. __str__ = __repr__
  40. TEST_OUTCOME = (OutcomeException, Exception)
  41. class Skipped(OutcomeException):
  42. # XXX hackish: on 3k we fake to live in the builtins
  43. # in order to have Skipped exception printing shorter/nicer
  44. __module__ = "builtins"
  45. def __init__(
  46. self,
  47. msg: Optional[str] = None,
  48. pytrace: bool = True,
  49. allow_module_level: bool = False,
  50. *,
  51. _use_item_location: bool = False,
  52. ) -> None:
  53. super().__init__(msg=msg, pytrace=pytrace)
  54. self.allow_module_level = allow_module_level
  55. # If true, the skip location is reported as the item's location,
  56. # instead of the place that raises the exception/calls skip().
  57. self._use_item_location = _use_item_location
  58. class Failed(OutcomeException):
  59. """Raised from an explicit call to pytest.fail()."""
  60. __module__ = "builtins"
  61. class Exit(Exception):
  62. """Raised for immediate program exits (no tracebacks/summaries)."""
  63. def __init__(
  64. self, msg: str = "unknown reason", returncode: Optional[int] = None
  65. ) -> None:
  66. self.msg = msg
  67. self.returncode = returncode
  68. super().__init__(msg)
  69. # Elaborate hack to work around https://github.com/python/mypy/issues/2087.
  70. # Ideally would just be `exit.Exception = Exit` etc.
  71. _F = TypeVar("_F", bound=Callable[..., object])
  72. _ET = TypeVar("_ET", bound=Type[BaseException])
  73. class _WithException(Protocol[_F, _ET]):
  74. Exception: _ET
  75. __call__: _F
  76. def _with_exception(exception_type: _ET) -> Callable[[_F], _WithException[_F, _ET]]:
  77. def decorate(func: _F) -> _WithException[_F, _ET]:
  78. func_with_exception = cast(_WithException[_F, _ET], func)
  79. func_with_exception.Exception = exception_type
  80. return func_with_exception
  81. return decorate
  82. # Exposed helper methods.
  83. @_with_exception(Exit)
  84. def exit(
  85. reason: str = "", returncode: Optional[int] = None, *, msg: Optional[str] = None
  86. ) -> "NoReturn":
  87. """Exit testing process.
  88. :param reason:
  89. The message to show as the reason for exiting pytest. reason has a default value
  90. only because `msg` is deprecated.
  91. :param returncode:
  92. Return code to be used when exiting pytest.
  93. :param msg:
  94. Same as ``reason``, but deprecated. Will be removed in a future version, use ``reason`` instead.
  95. """
  96. __tracebackhide__ = True
  97. from _pytest.config import UsageError
  98. if reason and msg:
  99. raise UsageError(
  100. "cannot pass reason and msg to exit(), `msg` is deprecated, use `reason`."
  101. )
  102. if not reason:
  103. if msg is None:
  104. raise UsageError("exit() requires a reason argument")
  105. warnings.warn(KEYWORD_MSG_ARG.format(func="exit"), stacklevel=2)
  106. reason = msg
  107. raise Exit(reason, returncode)
  108. @_with_exception(Skipped)
  109. def skip(
  110. reason: str = "", *, allow_module_level: bool = False, msg: Optional[str] = None
  111. ) -> "NoReturn":
  112. """Skip an executing test with the given message.
  113. This function should be called only during testing (setup, call or teardown) or
  114. during collection by using the ``allow_module_level`` flag. This function can
  115. be called in doctests as well.
  116. :param reason:
  117. The message to show the user as reason for the skip.
  118. :param allow_module_level:
  119. Allows this function to be called at module level, skipping the rest
  120. of the module. Defaults to False.
  121. :param msg:
  122. Same as ``reason``, but deprecated. Will be removed in a future version, use ``reason`` instead.
  123. .. note::
  124. It is better to use the :ref:`pytest.mark.skipif ref` marker when
  125. possible to declare a test to be skipped under certain conditions
  126. like mismatching platforms or dependencies.
  127. Similarly, use the ``# doctest: +SKIP`` directive (see :py:data:`doctest.SKIP`)
  128. to skip a doctest statically.
  129. """
  130. __tracebackhide__ = True
  131. reason = _resolve_msg_to_reason("skip", reason, msg)
  132. raise Skipped(msg=reason, allow_module_level=allow_module_level)
  133. @_with_exception(Failed)
  134. def fail(
  135. reason: str = "", pytrace: bool = True, msg: Optional[str] = None
  136. ) -> "NoReturn":
  137. """Explicitly fail an executing test with the given message.
  138. :param reason:
  139. The message to show the user as reason for the failure.
  140. :param pytrace:
  141. If False, msg represents the full failure information and no
  142. python traceback will be reported.
  143. :param msg:
  144. Same as ``reason``, but deprecated. Will be removed in a future version, use ``reason`` instead.
  145. """
  146. __tracebackhide__ = True
  147. reason = _resolve_msg_to_reason("fail", reason, msg)
  148. raise Failed(msg=reason, pytrace=pytrace)
  149. def _resolve_msg_to_reason(
  150. func_name: str, reason: str, msg: Optional[str] = None
  151. ) -> str:
  152. """
  153. Handles converting the deprecated msg parameter if provided into
  154. reason, raising a deprecation warning. This function will be removed
  155. when the optional msg argument is removed from here in future.
  156. :param str func_name:
  157. The name of the offending function, this is formatted into the deprecation message.
  158. :param str reason:
  159. The reason= passed into either pytest.fail() or pytest.skip()
  160. :param str msg:
  161. The msg= passed into either pytest.fail() or pytest.skip(). This will
  162. be converted into reason if it is provided to allow pytest.skip(msg=) or
  163. pytest.fail(msg=) to continue working in the interim period.
  164. :returns:
  165. The value to use as reason.
  166. """
  167. __tracebackhide__ = True
  168. if msg is not None:
  169. if reason:
  170. from pytest import UsageError
  171. raise UsageError(
  172. f"Passing both ``reason`` and ``msg`` to pytest.{func_name}(...) is not permitted."
  173. )
  174. warnings.warn(KEYWORD_MSG_ARG.format(func=func_name), stacklevel=3)
  175. reason = msg
  176. return reason
  177. class XFailed(Failed):
  178. """Raised from an explicit call to pytest.xfail()."""
  179. @_with_exception(XFailed)
  180. def xfail(reason: str = "") -> "NoReturn":
  181. """Imperatively xfail an executing test or setup function with the given reason.
  182. This function should be called only during testing (setup, call or teardown).
  183. .. note::
  184. It is better to use the :ref:`pytest.mark.xfail ref` marker when
  185. possible to declare a test to be xfailed under certain conditions
  186. like known bugs or missing features.
  187. """
  188. __tracebackhide__ = True
  189. raise XFailed(reason)
  190. def importorskip(
  191. modname: str, minversion: Optional[str] = None, reason: Optional[str] = None
  192. ) -> Any:
  193. """Import and return the requested module ``modname``, or skip the
  194. current test if the module cannot be imported.
  195. :param str modname:
  196. The name of the module to import.
  197. :param str minversion:
  198. If given, the imported module's ``__version__`` attribute must be at
  199. least this minimal version, otherwise the test is still skipped.
  200. :param str reason:
  201. If given, this reason is shown as the message when the module cannot
  202. be imported.
  203. :returns:
  204. The imported module. This should be assigned to its canonical name.
  205. Example::
  206. docutils = pytest.importorskip("docutils")
  207. """
  208. import warnings
  209. __tracebackhide__ = True
  210. compile(modname, "", "eval") # to catch syntaxerrors
  211. with warnings.catch_warnings():
  212. # Make sure to ignore ImportWarnings that might happen because
  213. # of existing directories with the same name we're trying to
  214. # import but without a __init__.py file.
  215. warnings.simplefilter("ignore")
  216. try:
  217. __import__(modname)
  218. except ImportError as exc:
  219. if reason is None:
  220. reason = f"could not import {modname!r}: {exc}"
  221. raise Skipped(reason, allow_module_level=True) from None
  222. mod = sys.modules[modname]
  223. if minversion is None:
  224. return mod
  225. verattr = getattr(mod, "__version__", None)
  226. if minversion is not None:
  227. # Imported lazily to improve start-up time.
  228. from packaging.version import Version
  229. if verattr is None or Version(verattr) < Version(minversion):
  230. raise Skipped(
  231. "module %r has __version__ %r, required is: %r"
  232. % (modname, verattr, minversion),
  233. allow_module_level=True,
  234. )
  235. return mod