hookspec.py 32 KB

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