hookspec.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  1. """Hook specifications for pytest plugins which are invoked by pytest itself
  2. and by builtin plugins."""
  3. from pathlib import Path
  4. from typing import Any
  5. from typing import Dict
  6. from typing import List
  7. from typing import Mapping
  8. from typing import Optional
  9. from typing import Sequence
  10. from typing import Tuple
  11. from typing import TYPE_CHECKING
  12. from typing import Union
  13. from pluggy import HookspecMarker
  14. from _pytest.deprecated import WARNING_CAPTURED_HOOK
  15. from _pytest.deprecated import WARNING_CMDLINE_PREPARSE_HOOK
  16. if TYPE_CHECKING:
  17. import pdb
  18. import warnings
  19. from typing_extensions import Literal
  20. from _pytest._code.code import ExceptionRepr
  21. from _pytest.code import ExceptionInfo
  22. from _pytest.config import Config
  23. from _pytest.config import ExitCode
  24. from _pytest.config import PytestPluginManager
  25. from _pytest.config import _PluggyPlugin
  26. from _pytest.config.argparsing import Parser
  27. from _pytest.fixtures import FixtureDef
  28. from _pytest.fixtures import SubRequest
  29. from _pytest.main import Session
  30. from _pytest.nodes import Collector
  31. from _pytest.nodes import Item
  32. from _pytest.outcomes import Exit
  33. from _pytest.python import Function
  34. from _pytest.python import Metafunc
  35. from _pytest.python import Module
  36. from _pytest.python import PyCollector
  37. from _pytest.reports import CollectReport
  38. from _pytest.reports import TestReport
  39. from _pytest.runner import CallInfo
  40. from _pytest.terminal import TerminalReporter
  41. from _pytest.compat import LEGACY_PATH
  42. hookspec = HookspecMarker("pytest")
  43. # -------------------------------------------------------------------------
  44. # Initialization hooks called for every plugin
  45. # -------------------------------------------------------------------------
  46. @hookspec(historic=True)
  47. def pytest_addhooks(pluginmanager: "PytestPluginManager") -> None:
  48. """Called at plugin registration time to allow adding new hooks via a call to
  49. ``pluginmanager.add_hookspecs(module_or_class, prefix)``.
  50. :param pytest.PytestPluginManager pluginmanager: The pytest plugin manager.
  51. .. note::
  52. This hook is incompatible with ``hookwrapper=True``.
  53. """
  54. @hookspec(historic=True)
  55. def pytest_plugin_registered(
  56. plugin: "_PluggyPlugin", manager: "PytestPluginManager"
  57. ) -> None:
  58. """A new pytest plugin got registered.
  59. :param plugin: The plugin module or instance.
  60. :param pytest.PytestPluginManager manager: pytest plugin manager.
  61. .. note::
  62. This hook is incompatible with ``hookwrapper=True``.
  63. """
  64. @hookspec(historic=True)
  65. def pytest_addoption(parser: "Parser", pluginmanager: "PytestPluginManager") -> None:
  66. """Register argparse-style options and ini-style config values,
  67. called once at the beginning of a test run.
  68. .. note::
  69. This function should be implemented only in plugins or ``conftest.py``
  70. files situated at the tests root directory due to how pytest
  71. :ref:`discovers plugins during startup <pluginorder>`.
  72. :param pytest.Parser parser:
  73. To add command line options, call
  74. :py:func:`parser.addoption(...) <pytest.Parser.addoption>`.
  75. To add ini-file values call :py:func:`parser.addini(...)
  76. <pytest.Parser.addini>`.
  77. :param pytest.PytestPluginManager pluginmanager:
  78. The pytest plugin manager, which can be used to install :py:func:`hookspec`'s
  79. or :py:func:`hookimpl`'s and allow one plugin to call another plugin's hooks
  80. to change how command line options are added.
  81. Options can later be accessed through the
  82. :py:class:`config <pytest.Config>` object, respectively:
  83. - :py:func:`config.getoption(name) <pytest.Config.getoption>` to
  84. retrieve the value of a command line option.
  85. - :py:func:`config.getini(name) <pytest.Config.getini>` to retrieve
  86. a value read from an ini-style file.
  87. The config object is passed around on many internal objects via the ``.config``
  88. attribute or can be retrieved as the ``pytestconfig`` fixture.
  89. .. note::
  90. This hook is incompatible with ``hookwrapper=True``.
  91. """
  92. @hookspec(historic=True)
  93. def pytest_configure(config: "Config") -> None:
  94. """Allow plugins and conftest files to perform initial configuration.
  95. This hook is called for every plugin and initial conftest file
  96. after command line options have been parsed.
  97. After that, the hook is called for other conftest files as they are
  98. imported.
  99. .. note::
  100. This hook is incompatible with ``hookwrapper=True``.
  101. :param pytest.Config config: The pytest config object.
  102. """
  103. # -------------------------------------------------------------------------
  104. # Bootstrapping hooks called for plugins registered early enough:
  105. # internal and 3rd party plugins.
  106. # -------------------------------------------------------------------------
  107. @hookspec(firstresult=True)
  108. def pytest_cmdline_parse(
  109. pluginmanager: "PytestPluginManager", args: List[str]
  110. ) -> Optional["Config"]:
  111. """Return an initialized config object, parsing the specified args.
  112. Stops at first non-None result, see :ref:`firstresult`.
  113. .. note::
  114. This hook will only be called for plugin classes passed to the
  115. ``plugins`` arg when using `pytest.main`_ to perform an in-process
  116. test run.
  117. :param pytest.PytestPluginManager pluginmanager: The pytest plugin manager.
  118. :param List[str] args: List of arguments passed on the command line.
  119. """
  120. @hookspec(warn_on_impl=WARNING_CMDLINE_PREPARSE_HOOK)
  121. def pytest_cmdline_preparse(config: "Config", args: List[str]) -> None:
  122. """(**Deprecated**) modify command line arguments before option parsing.
  123. This hook is considered deprecated and will be removed in a future pytest version. Consider
  124. using :hook:`pytest_load_initial_conftests` instead.
  125. .. note::
  126. This hook will not be called for ``conftest.py`` files, only for setuptools plugins.
  127. :param pytest.Config config: The pytest config object.
  128. :param List[str] args: Arguments passed on the command line.
  129. """
  130. @hookspec(firstresult=True)
  131. def pytest_cmdline_main(config: "Config") -> Optional[Union["ExitCode", int]]:
  132. """Called for performing the main command line action. The default
  133. implementation will invoke the configure hooks and runtest_mainloop.
  134. Stops at first non-None result, see :ref:`firstresult`.
  135. :param pytest.Config config: The pytest config object.
  136. """
  137. def pytest_load_initial_conftests(
  138. early_config: "Config", parser: "Parser", args: List[str]
  139. ) -> None:
  140. """Called to implement the loading of initial conftest files ahead
  141. of command line option parsing.
  142. .. note::
  143. This hook will not be called for ``conftest.py`` files, only for setuptools plugins.
  144. :param pytest.Config early_config: The pytest config object.
  145. :param List[str] args: Arguments passed on the command line.
  146. :param pytest.Parser parser: To add command line options.
  147. """
  148. # -------------------------------------------------------------------------
  149. # collection hooks
  150. # -------------------------------------------------------------------------
  151. @hookspec(firstresult=True)
  152. def pytest_collection(session: "Session") -> Optional[object]:
  153. """Perform the collection phase for the given session.
  154. Stops at first non-None result, see :ref:`firstresult`.
  155. The return value is not used, but only stops further processing.
  156. The default collection phase is this (see individual hooks for full details):
  157. 1. Starting from ``session`` as the initial collector:
  158. 1. ``pytest_collectstart(collector)``
  159. 2. ``report = pytest_make_collect_report(collector)``
  160. 3. ``pytest_exception_interact(collector, call, report)`` if an interactive exception occurred
  161. 4. For each collected node:
  162. 1. If an item, ``pytest_itemcollected(item)``
  163. 2. If a collector, recurse into it.
  164. 5. ``pytest_collectreport(report)``
  165. 2. ``pytest_collection_modifyitems(session, config, items)``
  166. 1. ``pytest_deselected(items)`` for any deselected items (may be called multiple times)
  167. 3. ``pytest_collection_finish(session)``
  168. 4. Set ``session.items`` to the list of collected items
  169. 5. Set ``session.testscollected`` to the number of collected items
  170. You can implement this hook to only perform some action before collection,
  171. for example the terminal plugin uses it to start displaying the collection
  172. counter (and returns `None`).
  173. :param pytest.Session session: The pytest session object.
  174. """
  175. def pytest_collection_modifyitems(
  176. session: "Session", config: "Config", items: List["Item"]
  177. ) -> None:
  178. """Called after collection has been performed. May filter or re-order
  179. the items in-place.
  180. :param pytest.Session session: The pytest session object.
  181. :param pytest.Config config: The pytest config object.
  182. :param List[pytest.Item] items: List of item objects.
  183. """
  184. def pytest_collection_finish(session: "Session") -> None:
  185. """Called after collection has been performed and modified.
  186. :param pytest.Session session: The pytest session object.
  187. """
  188. @hookspec(firstresult=True)
  189. def pytest_ignore_collect(
  190. collection_path: Path, path: "LEGACY_PATH", config: "Config"
  191. ) -> Optional[bool]:
  192. """Return True to prevent considering this path for collection.
  193. This hook is consulted for all files and directories prior to calling
  194. more specific hooks.
  195. Stops at first non-None result, see :ref:`firstresult`.
  196. :param pathlib.Path collection_path : The path to analyze.
  197. :param LEGACY_PATH path: The path to analyze (deprecated).
  198. :param pytest.Config config: The pytest config object.
  199. .. versionchanged:: 7.0.0
  200. The ``collection_path`` parameter was added as a :class:`pathlib.Path`
  201. equivalent of the ``path`` parameter. The ``path`` parameter
  202. has been deprecated.
  203. """
  204. def pytest_collect_file(
  205. file_path: Path, path: "LEGACY_PATH", parent: "Collector"
  206. ) -> "Optional[Collector]":
  207. """Create a Collector for the given path, or None if not relevant.
  208. The new node needs to have the specified ``parent`` as a parent.
  209. :param pathlib.Path file_path: The path to analyze.
  210. :param LEGACY_PATH path: The path to collect (deprecated).
  211. .. versionchanged:: 7.0.0
  212. The ``file_path`` parameter was added as a :class:`pathlib.Path`
  213. equivalent of the ``path`` parameter. The ``path`` parameter
  214. has been deprecated.
  215. """
  216. # logging hooks for collection
  217. def pytest_collectstart(collector: "Collector") -> None:
  218. """Collector starts collecting."""
  219. def pytest_itemcollected(item: "Item") -> None:
  220. """We just collected a test item."""
  221. def pytest_collectreport(report: "CollectReport") -> None:
  222. """Collector finished collecting."""
  223. def pytest_deselected(items: Sequence["Item"]) -> None:
  224. """Called for deselected test items, e.g. by keyword.
  225. May be called multiple times.
  226. """
  227. @hookspec(firstresult=True)
  228. def pytest_make_collect_report(collector: "Collector") -> "Optional[CollectReport]":
  229. """Perform :func:`collector.collect() <pytest.Collector.collect>` and return
  230. a :class:`~pytest.CollectReport`.
  231. Stops at first non-None result, see :ref:`firstresult`.
  232. """
  233. # -------------------------------------------------------------------------
  234. # Python test function related hooks
  235. # -------------------------------------------------------------------------
  236. @hookspec(firstresult=True)
  237. def pytest_pycollect_makemodule(
  238. module_path: Path, path: "LEGACY_PATH", parent
  239. ) -> Optional["Module"]:
  240. """Return a Module collector or None for the given path.
  241. This hook will be called for each matching test module path.
  242. The pytest_collect_file hook needs to be used if you want to
  243. create test modules for files that do not match as a test module.
  244. Stops at first non-None result, see :ref:`firstresult`.
  245. :param pathlib.Path module_path: The path of the module to collect.
  246. :param LEGACY_PATH path: The path of the module to collect (deprecated).
  247. .. versionchanged:: 7.0.0
  248. The ``module_path`` parameter was added as a :class:`pathlib.Path`
  249. equivalent of the ``path`` parameter.
  250. The ``path`` parameter has been deprecated in favor of ``fspath``.
  251. """
  252. @hookspec(firstresult=True)
  253. def pytest_pycollect_makeitem(
  254. collector: "PyCollector", name: str, obj: object
  255. ) -> Union[None, "Item", "Collector", List[Union["Item", "Collector"]]]:
  256. """Return a custom item/collector for a Python object in a module, or None.
  257. Stops at first non-None result, see :ref:`firstresult`.
  258. """
  259. @hookspec(firstresult=True)
  260. def pytest_pyfunc_call(pyfuncitem: "Function") -> Optional[object]:
  261. """Call underlying test function.
  262. Stops at first non-None result, see :ref:`firstresult`.
  263. """
  264. def pytest_generate_tests(metafunc: "Metafunc") -> None:
  265. """Generate (multiple) parametrized calls to a test function."""
  266. @hookspec(firstresult=True)
  267. def pytest_make_parametrize_id(
  268. config: "Config", val: object, argname: str
  269. ) -> Optional[str]:
  270. """Return a user-friendly string representation of the given ``val``
  271. that will be used by @pytest.mark.parametrize calls, or None if the hook
  272. doesn't know about ``val``.
  273. The parameter name is available as ``argname``, if required.
  274. Stops at first non-None result, see :ref:`firstresult`.
  275. :param pytest.Config config: The pytest config object.
  276. :param val: The parametrized value.
  277. :param str argname: The automatic parameter name produced by pytest.
  278. """
  279. # -------------------------------------------------------------------------
  280. # runtest related hooks
  281. # -------------------------------------------------------------------------
  282. @hookspec(firstresult=True)
  283. def pytest_runtestloop(session: "Session") -> Optional[object]:
  284. """Perform the main runtest loop (after collection finished).
  285. The default hook implementation performs the runtest protocol for all items
  286. collected in the session (``session.items``), unless the collection failed
  287. or the ``collectonly`` pytest option is set.
  288. If at any point :py:func:`pytest.exit` is called, the loop is
  289. terminated immediately.
  290. If at any point ``session.shouldfail`` or ``session.shouldstop`` are set, the
  291. loop is terminated after the runtest protocol for the current item is finished.
  292. :param pytest.Session session: The pytest session object.
  293. Stops at first non-None result, see :ref:`firstresult`.
  294. The return value is not used, but only stops further processing.
  295. """
  296. @hookspec(firstresult=True)
  297. def pytest_runtest_protocol(
  298. item: "Item", nextitem: "Optional[Item]"
  299. ) -> Optional[object]:
  300. """Perform the runtest protocol for a single test item.
  301. The default runtest protocol is this (see individual hooks for full details):
  302. - ``pytest_runtest_logstart(nodeid, location)``
  303. - Setup phase:
  304. - ``call = pytest_runtest_setup(item)`` (wrapped in ``CallInfo(when="setup")``)
  305. - ``report = pytest_runtest_makereport(item, call)``
  306. - ``pytest_runtest_logreport(report)``
  307. - ``pytest_exception_interact(call, report)`` if an interactive exception occurred
  308. - Call phase, if the the setup passed and the ``setuponly`` pytest option is not set:
  309. - ``call = pytest_runtest_call(item)`` (wrapped in ``CallInfo(when="call")``)
  310. - ``report = pytest_runtest_makereport(item, call)``
  311. - ``pytest_runtest_logreport(report)``
  312. - ``pytest_exception_interact(call, report)`` if an interactive exception occurred
  313. - Teardown phase:
  314. - ``call = pytest_runtest_teardown(item, nextitem)`` (wrapped in ``CallInfo(when="teardown")``)
  315. - ``report = pytest_runtest_makereport(item, call)``
  316. - ``pytest_runtest_logreport(report)``
  317. - ``pytest_exception_interact(call, report)`` if an interactive exception occurred
  318. - ``pytest_runtest_logfinish(nodeid, location)``
  319. :param item: Test item for which the runtest protocol is performed.
  320. :param nextitem: The scheduled-to-be-next test item (or None if this is the end my friend).
  321. Stops at first non-None result, see :ref:`firstresult`.
  322. The return value is not used, but only stops further processing.
  323. """
  324. def pytest_runtest_logstart(
  325. nodeid: str, location: Tuple[str, Optional[int], str]
  326. ) -> None:
  327. """Called at the start of running the runtest protocol for a single item.
  328. See :hook:`pytest_runtest_protocol` for a description of the runtest protocol.
  329. :param str nodeid: Full node ID of the item.
  330. :param location: A tuple of ``(filename, lineno, testname)``.
  331. """
  332. def pytest_runtest_logfinish(
  333. nodeid: str, location: Tuple[str, Optional[int], str]
  334. ) -> None:
  335. """Called at the end of running the runtest protocol for a single item.
  336. See :hook:`pytest_runtest_protocol` for a description of the runtest protocol.
  337. :param str nodeid: Full node ID of the item.
  338. :param location: A tuple of ``(filename, lineno, testname)``.
  339. """
  340. def pytest_runtest_setup(item: "Item") -> None:
  341. """Called to perform the setup phase for a test item.
  342. The default implementation runs ``setup()`` on ``item`` and all of its
  343. parents (which haven't been setup yet). This includes obtaining the
  344. values of fixtures required by the item (which haven't been obtained
  345. yet).
  346. """
  347. def pytest_runtest_call(item: "Item") -> None:
  348. """Called to run the test for test item (the call phase).
  349. The default implementation calls ``item.runtest()``.
  350. """
  351. def pytest_runtest_teardown(item: "Item", nextitem: Optional["Item"]) -> None:
  352. """Called to perform the teardown phase for a test item.
  353. The default implementation runs the finalizers and calls ``teardown()``
  354. on ``item`` and all of its parents (which need to be torn down). This
  355. includes running the teardown phase of fixtures required by the item (if
  356. they go out of scope).
  357. :param nextitem:
  358. The scheduled-to-be-next test item (None if no further test item is
  359. scheduled). This argument is used to perform exact teardowns, i.e.
  360. calling just enough finalizers so that nextitem only needs to call
  361. setup functions.
  362. """
  363. @hookspec(firstresult=True)
  364. def pytest_runtest_makereport(
  365. item: "Item", call: "CallInfo[None]"
  366. ) -> Optional["TestReport"]:
  367. """Called to create a :class:`~pytest.TestReport` for each of
  368. the setup, call and teardown runtest phases of a test item.
  369. See :hook:`pytest_runtest_protocol` for a description of the runtest protocol.
  370. :param call: The :class:`~pytest.CallInfo` for the phase.
  371. Stops at first non-None result, see :ref:`firstresult`.
  372. """
  373. def pytest_runtest_logreport(report: "TestReport") -> None:
  374. """Process the :class:`~pytest.TestReport` produced for each
  375. of the setup, call and teardown runtest phases of an item.
  376. See :hook:`pytest_runtest_protocol` for a description of the runtest protocol.
  377. """
  378. @hookspec(firstresult=True)
  379. def pytest_report_to_serializable(
  380. config: "Config",
  381. report: Union["CollectReport", "TestReport"],
  382. ) -> Optional[Dict[str, Any]]:
  383. """Serialize the given report object into a data structure suitable for
  384. sending over the wire, e.g. converted to JSON."""
  385. @hookspec(firstresult=True)
  386. def pytest_report_from_serializable(
  387. config: "Config",
  388. data: Dict[str, Any],
  389. ) -> Optional[Union["CollectReport", "TestReport"]]:
  390. """Restore a report object previously serialized with
  391. :hook:`pytest_report_to_serializable`."""
  392. # -------------------------------------------------------------------------
  393. # Fixture related hooks
  394. # -------------------------------------------------------------------------
  395. @hookspec(firstresult=True)
  396. def pytest_fixture_setup(
  397. fixturedef: "FixtureDef[Any]", request: "SubRequest"
  398. ) -> Optional[object]:
  399. """Perform fixture setup execution.
  400. :returns: The return value of the call to the fixture function.
  401. Stops at first non-None result, see :ref:`firstresult`.
  402. .. note::
  403. If the fixture function returns None, other implementations of
  404. this hook function will continue to be called, according to the
  405. behavior of the :ref:`firstresult` option.
  406. """
  407. def pytest_fixture_post_finalizer(
  408. fixturedef: "FixtureDef[Any]", request: "SubRequest"
  409. ) -> None:
  410. """Called after fixture teardown, but before the cache is cleared, so
  411. the fixture result ``fixturedef.cached_result`` is still available (not
  412. ``None``)."""
  413. # -------------------------------------------------------------------------
  414. # test session related hooks
  415. # -------------------------------------------------------------------------
  416. def pytest_sessionstart(session: "Session") -> None:
  417. """Called after the ``Session`` object has been created and before performing collection
  418. and entering the run test loop.
  419. :param pytest.Session session: The pytest session object.
  420. """
  421. def pytest_sessionfinish(
  422. session: "Session",
  423. exitstatus: Union[int, "ExitCode"],
  424. ) -> None:
  425. """Called after whole test run finished, right before returning the exit status to the system.
  426. :param pytest.Session session: The pytest session object.
  427. :param int exitstatus: The status which pytest will return to the system.
  428. """
  429. def pytest_unconfigure(config: "Config") -> None:
  430. """Called before test process is exited.
  431. :param pytest.Config config: The pytest config object.
  432. """
  433. # -------------------------------------------------------------------------
  434. # hooks for customizing the assert methods
  435. # -------------------------------------------------------------------------
  436. def pytest_assertrepr_compare(
  437. config: "Config", op: str, left: object, right: object
  438. ) -> Optional[List[str]]:
  439. """Return explanation for comparisons in failing assert expressions.
  440. Return None for no custom explanation, otherwise return a list
  441. of strings. The strings will be joined by newlines but any newlines
  442. *in* a string will be escaped. Note that all but the first line will
  443. be indented slightly, the intention is for the first line to be a summary.
  444. :param pytest.Config config: The pytest config object.
  445. """
  446. def pytest_assertion_pass(item: "Item", lineno: int, orig: str, expl: str) -> None:
  447. """Called whenever an assertion passes.
  448. .. versionadded:: 5.0
  449. Use this hook to do some processing after a passing assertion.
  450. The original assertion information is available in the `orig` string
  451. and the pytest introspected assertion information is available in the
  452. `expl` string.
  453. This hook must be explicitly enabled by the ``enable_assertion_pass_hook``
  454. ini-file option:
  455. .. code-block:: ini
  456. [pytest]
  457. enable_assertion_pass_hook=true
  458. You need to **clean the .pyc** files in your project directory and interpreter libraries
  459. when enabling this option, as assertions will require to be re-written.
  460. :param pytest.Item item: pytest item object of current test.
  461. :param int lineno: Line number of the assert statement.
  462. :param str orig: String with the original assertion.
  463. :param str expl: String with the assert explanation.
  464. """
  465. # -------------------------------------------------------------------------
  466. # Hooks for influencing reporting (invoked from _pytest_terminal).
  467. # -------------------------------------------------------------------------
  468. def pytest_report_header(
  469. config: "Config", start_path: Path, startdir: "LEGACY_PATH"
  470. ) -> Union[str, List[str]]:
  471. """Return a string or list of strings to be displayed as header info for terminal reporting.
  472. :param pytest.Config config: The pytest config object.
  473. :param Path start_path: The starting dir.
  474. :param LEGACY_PATH startdir: The starting dir (deprecated).
  475. .. note::
  476. Lines returned by a plugin are displayed before those of plugins which
  477. ran before it.
  478. If you want to have your line(s) displayed first, use
  479. :ref:`trylast=True <plugin-hookorder>`.
  480. .. note::
  481. This function should be implemented only in plugins or ``conftest.py``
  482. files situated at the tests root directory due to how pytest
  483. :ref:`discovers plugins during startup <pluginorder>`.
  484. .. versionchanged:: 7.0.0
  485. The ``start_path`` parameter was added as a :class:`pathlib.Path`
  486. equivalent of the ``startdir`` parameter. The ``startdir`` parameter
  487. has been deprecated.
  488. """
  489. def pytest_report_collectionfinish(
  490. config: "Config",
  491. start_path: Path,
  492. startdir: "LEGACY_PATH",
  493. items: Sequence["Item"],
  494. ) -> Union[str, List[str]]:
  495. """Return a string or list of strings to be displayed after collection
  496. has finished successfully.
  497. These strings will be displayed after the standard "collected X items" message.
  498. .. versionadded:: 3.2
  499. :param pytest.Config config: The pytest config object.
  500. :param Path start_path: The starting dir.
  501. :param LEGACY_PATH startdir: The starting dir (deprecated).
  502. :param items: List of pytest items that are going to be executed; this list should not be modified.
  503. .. note::
  504. Lines returned by a plugin are displayed before those of plugins which
  505. ran before it.
  506. If you want to have your line(s) displayed first, use
  507. :ref:`trylast=True <plugin-hookorder>`.
  508. .. versionchanged:: 7.0.0
  509. The ``start_path`` parameter was added as a :class:`pathlib.Path`
  510. equivalent of the ``startdir`` parameter. The ``startdir`` parameter
  511. has been deprecated.
  512. """
  513. @hookspec(firstresult=True)
  514. def pytest_report_teststatus(
  515. report: Union["CollectReport", "TestReport"], config: "Config"
  516. ) -> Tuple[str, str, Union[str, Mapping[str, bool]]]:
  517. """Return result-category, shortletter and verbose word for status
  518. reporting.
  519. The result-category is a category in which to count the result, for
  520. example "passed", "skipped", "error" or the empty string.
  521. The shortletter is shown as testing progresses, for example ".", "s",
  522. "E" or the empty string.
  523. The verbose word is shown as testing progresses in verbose mode, for
  524. example "PASSED", "SKIPPED", "ERROR" or the empty string.
  525. pytest may style these implicitly according to the report outcome.
  526. To provide explicit styling, return a tuple for the verbose word,
  527. for example ``"rerun", "R", ("RERUN", {"yellow": True})``.
  528. :param report: The report object whose status is to be returned.
  529. :param config: The pytest config object.
  530. Stops at first non-None result, see :ref:`firstresult`.
  531. """
  532. def pytest_terminal_summary(
  533. terminalreporter: "TerminalReporter",
  534. exitstatus: "ExitCode",
  535. config: "Config",
  536. ) -> None:
  537. """Add a section to terminal summary reporting.
  538. :param _pytest.terminal.TerminalReporter terminalreporter: The internal terminal reporter object.
  539. :param int exitstatus: The exit status that will be reported back to the OS.
  540. :param pytest.Config config: The pytest config object.
  541. .. versionadded:: 4.2
  542. The ``config`` parameter.
  543. """
  544. @hookspec(historic=True, warn_on_impl=WARNING_CAPTURED_HOOK)
  545. def pytest_warning_captured(
  546. warning_message: "warnings.WarningMessage",
  547. when: "Literal['config', 'collect', 'runtest']",
  548. item: Optional["Item"],
  549. location: Optional[Tuple[str, int, str]],
  550. ) -> None:
  551. """(**Deprecated**) Process a warning captured by the internal pytest warnings plugin.
  552. .. deprecated:: 6.0
  553. This hook is considered deprecated and will be removed in a future pytest version.
  554. Use :func:`pytest_warning_recorded` instead.
  555. :param warnings.WarningMessage warning_message:
  556. The captured warning. This is the same object produced by :py:func:`warnings.catch_warnings`, and contains
  557. the same attributes as the parameters of :py:func:`warnings.showwarning`.
  558. :param str when:
  559. Indicates when the warning was captured. Possible values:
  560. * ``"config"``: during pytest configuration/initialization stage.
  561. * ``"collect"``: during test collection.
  562. * ``"runtest"``: during test execution.
  563. :param pytest.Item|None item:
  564. The item being executed if ``when`` is ``"runtest"``, otherwise ``None``.
  565. :param tuple location:
  566. When available, holds information about the execution context of the captured
  567. warning (filename, linenumber, function). ``function`` evaluates to <module>
  568. when the execution context is at the module level.
  569. """
  570. @hookspec(historic=True)
  571. def pytest_warning_recorded(
  572. warning_message: "warnings.WarningMessage",
  573. when: "Literal['config', 'collect', 'runtest']",
  574. nodeid: str,
  575. location: Optional[Tuple[str, int, str]],
  576. ) -> None:
  577. """Process a warning captured by the internal pytest warnings plugin.
  578. :param warnings.WarningMessage warning_message:
  579. The captured warning. This is the same object produced by :py:func:`warnings.catch_warnings`, and contains
  580. the same attributes as the parameters of :py:func:`warnings.showwarning`.
  581. :param str when:
  582. Indicates when the warning was captured. Possible values:
  583. * ``"config"``: during pytest configuration/initialization stage.
  584. * ``"collect"``: during test collection.
  585. * ``"runtest"``: during test execution.
  586. :param str nodeid:
  587. Full id of the item.
  588. :param tuple|None location:
  589. When available, holds information about the execution context of the captured
  590. warning (filename, linenumber, function). ``function`` evaluates to <module>
  591. when the execution context is at the module level.
  592. .. versionadded:: 6.0
  593. """
  594. # -------------------------------------------------------------------------
  595. # Hooks for influencing skipping
  596. # -------------------------------------------------------------------------
  597. def pytest_markeval_namespace(config: "Config") -> Dict[str, Any]:
  598. """Called when constructing the globals dictionary used for
  599. evaluating string conditions in xfail/skipif markers.
  600. This is useful when the condition for a marker requires
  601. objects that are expensive or impossible to obtain during
  602. collection time, which is required by normal boolean
  603. conditions.
  604. .. versionadded:: 6.2
  605. :param pytest.Config config: The pytest config object.
  606. :returns: A dictionary of additional globals to add.
  607. """
  608. # -------------------------------------------------------------------------
  609. # error handling and internal debugging hooks
  610. # -------------------------------------------------------------------------
  611. def pytest_internalerror(
  612. excrepr: "ExceptionRepr",
  613. excinfo: "ExceptionInfo[BaseException]",
  614. ) -> Optional[bool]:
  615. """Called for internal errors.
  616. Return True to suppress the fallback handling of printing an
  617. INTERNALERROR message directly to sys.stderr.
  618. """
  619. def pytest_keyboard_interrupt(
  620. excinfo: "ExceptionInfo[Union[KeyboardInterrupt, Exit]]",
  621. ) -> None:
  622. """Called for keyboard interrupt."""
  623. def pytest_exception_interact(
  624. node: Union["Item", "Collector"],
  625. call: "CallInfo[Any]",
  626. report: Union["CollectReport", "TestReport"],
  627. ) -> None:
  628. """Called when an exception was raised which can potentially be
  629. interactively handled.
  630. May be called during collection (see :hook:`pytest_make_collect_report`),
  631. in which case ``report`` is a :class:`CollectReport`.
  632. May be called during runtest of an item (see :hook:`pytest_runtest_protocol`),
  633. in which case ``report`` is a :class:`TestReport`.
  634. This hook is not called if the exception that was raised is an internal
  635. exception like ``skip.Exception``.
  636. """
  637. def pytest_enter_pdb(config: "Config", pdb: "pdb.Pdb") -> None:
  638. """Called upon pdb.set_trace().
  639. Can be used by plugins to take special action just before the python
  640. debugger enters interactive mode.
  641. :param pytest.Config config: The pytest config object.
  642. :param pdb.Pdb pdb: The Pdb instance.
  643. """
  644. def pytest_leave_pdb(config: "Config", pdb: "pdb.Pdb") -> None:
  645. """Called when leaving pdb (e.g. with continue after pdb.set_trace()).
  646. Can be used by plugins to take special action just after the python
  647. debugger leaves interactive mode.
  648. :param pytest.Config config: The pytest config object.
  649. :param pdb.Pdb pdb: The Pdb instance.
  650. """