hookspec.py 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  1. # mypy: allow-untyped-defs
  2. # ruff: noqa: T100
  3. """Hook specifications for pytest plugins which are invoked by pytest itself
  4. and by builtin plugins."""
  5. from __future__ import annotations
  6. from collections.abc import Mapping
  7. from collections.abc import Sequence
  8. from pathlib import Path
  9. from typing import Any
  10. from typing import TYPE_CHECKING
  11. from pluggy import HookspecMarker
  12. from .deprecated import HOOK_LEGACY_PATH_ARG
  13. if TYPE_CHECKING:
  14. import pdb
  15. from typing import Literal
  16. import warnings
  17. from _pytest._code.code import ExceptionInfo
  18. from _pytest._code.code import ExceptionRepr
  19. from _pytest.compat import LEGACY_PATH
  20. from _pytest.config import _PluggyPlugin
  21. from _pytest.config import Config
  22. from _pytest.config import ExitCode
  23. from _pytest.config import PytestPluginManager
  24. from _pytest.config.argparsing import Parser
  25. from _pytest.fixtures import FixtureDef
  26. from _pytest.fixtures import SubRequest
  27. from _pytest.main import Session
  28. from _pytest.nodes import Collector
  29. from _pytest.nodes import Item
  30. from _pytest.outcomes import Exit
  31. from _pytest.python import Class
  32. from _pytest.python import Function
  33. from _pytest.python import Metafunc
  34. from _pytest.python import Module
  35. from _pytest.reports import CollectReport
  36. from _pytest.reports import TestReport
  37. from _pytest.runner import CallInfo
  38. from _pytest.terminal import TerminalReporter
  39. from _pytest.terminal import TestShortLogReport
  40. hookspec = HookspecMarker("pytest")
  41. # -------------------------------------------------------------------------
  42. # Initialization hooks called for every plugin
  43. # -------------------------------------------------------------------------
  44. @hookspec(historic=True)
  45. def pytest_addhooks(pluginmanager: PytestPluginManager) -> None:
  46. """Called at plugin registration time to allow adding new hooks via a call to
  47. :func:`pluginmanager.add_hookspecs(module_or_class, prefix) <pytest.PytestPluginManager.add_hookspecs>`.
  48. :param pluginmanager: The pytest plugin manager.
  49. .. note::
  50. This hook is incompatible with hook wrappers.
  51. Use in conftest plugins
  52. =======================
  53. If a conftest plugin implements this hook, it will be called immediately
  54. when the conftest is registered.
  55. """
  56. @hookspec(historic=True)
  57. def pytest_plugin_registered(
  58. plugin: _PluggyPlugin,
  59. plugin_name: str,
  60. manager: PytestPluginManager,
  61. ) -> None:
  62. """A new pytest plugin got registered.
  63. :param plugin: The plugin module or instance.
  64. :param plugin_name: The name by which the plugin is registered.
  65. :param manager: The pytest plugin manager.
  66. .. note::
  67. This hook is incompatible with hook wrappers.
  68. Use in conftest plugins
  69. =======================
  70. If a conftest plugin implements this hook, it will be called immediately
  71. when the conftest is registered, once for each plugin registered thus far
  72. (including itself!), and for all plugins thereafter when they are
  73. registered.
  74. """
  75. @hookspec(historic=True)
  76. def pytest_addoption(parser: Parser, pluginmanager: PytestPluginManager) -> None:
  77. """Register argparse-style options and config-style config values,
  78. called once at the beginning of a test run.
  79. :param parser:
  80. To add command line options, call
  81. :py:func:`parser.addoption(...) <pytest.Parser.addoption>`.
  82. To add config-file values call :py:func:`parser.addini(...)
  83. <pytest.Parser.addini>`.
  84. :param pluginmanager:
  85. The pytest plugin manager, which can be used to install :py:func:`~pytest.hookspec`'s
  86. or :py:func:`~pytest.hookimpl`'s and allow one plugin to call another plugin's hooks
  87. to change how command line options are added.
  88. Options can later be accessed through the
  89. :py:class:`config <pytest.Config>` object, respectively:
  90. - :py:func:`config.getoption(name) <pytest.Config.getoption>` to
  91. retrieve the value of a command line option.
  92. - :py:func:`config.getini(name) <pytest.Config.getini>` to retrieve
  93. a value read from a configuration file.
  94. The config object is passed around on many internal objects via the ``.config``
  95. attribute or can be retrieved as the ``pytestconfig`` fixture.
  96. .. note::
  97. This hook is incompatible with hook wrappers.
  98. Use in conftest plugins
  99. =======================
  100. If a conftest plugin implements this hook, it will be called immediately
  101. when the conftest is registered.
  102. This hook is only called for :ref:`initial conftests <pluginorder>`.
  103. """
  104. @hookspec(historic=True)
  105. def pytest_configure(config: Config) -> None:
  106. """Allow plugins and conftest files to perform initial configuration.
  107. .. note::
  108. This hook is incompatible with hook wrappers.
  109. :param config: The pytest config object.
  110. Use in conftest plugins
  111. =======================
  112. This hook is called for every :ref:`initial conftest <pluginorder>` file
  113. after command line options have been parsed. After that, the hook is called
  114. for other conftest files as they are registered.
  115. """
  116. # -------------------------------------------------------------------------
  117. # Bootstrapping hooks called for plugins registered early enough:
  118. # internal and 3rd party plugins.
  119. # -------------------------------------------------------------------------
  120. @hookspec(firstresult=True)
  121. def pytest_cmdline_parse(
  122. pluginmanager: PytestPluginManager, args: list[str]
  123. ) -> Config | None:
  124. """Return an initialized :class:`~pytest.Config`, parsing the specified args.
  125. Stops at first non-None result, see :ref:`firstresult`.
  126. .. note::
  127. This hook is only called for plugin classes passed to the
  128. ``plugins`` arg when using `pytest.main`_ to perform an in-process
  129. test run.
  130. :param pluginmanager: The pytest plugin manager.
  131. :param args: List of arguments passed on the command line.
  132. :returns: A pytest config object.
  133. Use in conftest plugins
  134. =======================
  135. This hook is not called for conftest files.
  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 :ref:`initial conftest files
  141. <pluginorder>` ahead of command line option parsing.
  142. :param early_config: The pytest config object.
  143. :param args: Arguments passed on the command line.
  144. :param parser: To add command line options.
  145. Use in conftest plugins
  146. =======================
  147. This hook is not called for conftest files.
  148. """
  149. @hookspec(firstresult=True)
  150. def pytest_cmdline_main(config: Config) -> ExitCode | int | None:
  151. """Called for performing the main command line action.
  152. The default implementation will invoke the configure hooks and
  153. :hook:`pytest_runtestloop`.
  154. Stops at first non-None result, see :ref:`firstresult`.
  155. :param config: The pytest config object.
  156. :returns: The exit code.
  157. Use in conftest plugins
  158. =======================
  159. This hook is only called for :ref:`initial conftests <pluginorder>`.
  160. """
  161. # -------------------------------------------------------------------------
  162. # collection hooks
  163. # -------------------------------------------------------------------------
  164. @hookspec(firstresult=True)
  165. def pytest_collection(session: Session) -> object | None:
  166. """Perform the collection phase for the given session.
  167. Stops at first non-None result, see :ref:`firstresult`.
  168. The return value is not used, but only stops further processing.
  169. The default collection phase is this (see individual hooks for full details):
  170. 1. Starting from ``session`` as the initial collector:
  171. 1. ``pytest_collectstart(collector)``
  172. 2. ``report = pytest_make_collect_report(collector)``
  173. 3. ``pytest_exception_interact(collector, call, report)`` if an interactive exception occurred
  174. 4. For each collected node:
  175. 1. If an item, ``pytest_itemcollected(item)``
  176. 2. If a collector, recurse into it.
  177. 5. ``pytest_collectreport(report)``
  178. 2. ``pytest_collection_modifyitems(session, config, items)``
  179. 1. ``pytest_deselected(items)`` for any deselected items (may be called multiple times)
  180. 3. ``pytest_collection_finish(session)``
  181. 4. Set ``session.items`` to the list of collected items
  182. 5. Set ``session.testscollected`` to the number of collected items
  183. You can implement this hook to only perform some action before collection,
  184. for example the terminal plugin uses it to start displaying the collection
  185. counter (and returns `None`).
  186. :param session: The pytest session object.
  187. Use in conftest plugins
  188. =======================
  189. This hook is only called for :ref:`initial conftests <pluginorder>`.
  190. """
  191. def pytest_collection_modifyitems(
  192. session: Session, config: Config, items: list[Item]
  193. ) -> None:
  194. """Called after collection has been performed. May filter or re-order
  195. the items in-place.
  196. When items are deselected (filtered out from ``items``),
  197. the hook :hook:`pytest_deselected` must be called explicitly
  198. with the deselected items to properly notify other plugins,
  199. e.g. with ``config.hook.pytest_deselected(items=deselected_items)``.
  200. :param session: The pytest session object.
  201. :param config: The pytest config object.
  202. :param items: List of item objects.
  203. Use in conftest plugins
  204. =======================
  205. Any conftest plugin can implement this hook.
  206. """
  207. def pytest_collection_finish(session: Session) -> None:
  208. """Called after collection has been performed and modified.
  209. :param session: The pytest session object.
  210. Use in conftest plugins
  211. =======================
  212. Any conftest plugin can implement this hook.
  213. """
  214. @hookspec(
  215. firstresult=True,
  216. warn_on_impl_args={
  217. "path": HOOK_LEGACY_PATH_ARG.format(
  218. pylib_path_arg="path", pathlib_path_arg="collection_path"
  219. ),
  220. },
  221. )
  222. def pytest_ignore_collect(
  223. collection_path: Path, path: LEGACY_PATH, config: Config
  224. ) -> bool | None:
  225. """Return ``True`` to ignore this path for collection.
  226. Return ``None`` to let other plugins ignore the path for collection.
  227. Returning ``False`` will forcefully *not* ignore this path for collection,
  228. without giving a chance for other plugins to ignore this path.
  229. This hook is consulted for all files and directories prior to calling
  230. more specific hooks.
  231. Stops at first non-None result, see :ref:`firstresult`.
  232. :param collection_path: The path to analyze.
  233. :type collection_path: pathlib.Path
  234. :param path: The path to analyze (deprecated).
  235. :param config: The pytest config object.
  236. .. versionchanged:: 7.0.0
  237. The ``collection_path`` parameter was added as a :class:`pathlib.Path`
  238. equivalent of the ``path`` parameter. The ``path`` parameter
  239. has been deprecated.
  240. Use in conftest plugins
  241. =======================
  242. Any conftest file can implement this hook. For a given collection path, only
  243. conftest files in parent directories of the collection path are consulted
  244. (if the path is a directory, its own conftest file is *not* consulted - a
  245. directory cannot ignore itself!).
  246. """
  247. @hookspec(firstresult=True)
  248. def pytest_collect_directory(path: Path, parent: Collector) -> Collector | None:
  249. """Create a :class:`~pytest.Collector` for the given directory, or None if
  250. not relevant.
  251. .. versionadded:: 8.0
  252. For best results, the returned collector should be a subclass of
  253. :class:`~pytest.Directory`, but this is not required.
  254. The new node needs to have the specified ``parent`` as a parent.
  255. Stops at first non-None result, see :ref:`firstresult`.
  256. :param path: The path to analyze.
  257. :type path: pathlib.Path
  258. See :ref:`custom directory collectors` for a simple example of use of this
  259. hook.
  260. Use in conftest plugins
  261. =======================
  262. Any conftest file can implement this hook. For a given collection path, only
  263. conftest files in parent directories of the collection path are consulted
  264. (if the path is a directory, its own conftest file is *not* consulted - a
  265. directory cannot collect itself!).
  266. """
  267. @hookspec(
  268. warn_on_impl_args={
  269. "path": HOOK_LEGACY_PATH_ARG.format(
  270. pylib_path_arg="path", pathlib_path_arg="file_path"
  271. ),
  272. },
  273. )
  274. def pytest_collect_file(
  275. file_path: Path, path: LEGACY_PATH, parent: Collector
  276. ) -> Collector | None:
  277. """Create a :class:`~pytest.Collector` for the given path, or None if not relevant.
  278. For best results, the returned collector should be a subclass of
  279. :class:`~pytest.File`, but this is not required.
  280. The new node needs to have the specified ``parent`` as a parent.
  281. :param file_path: The path to analyze.
  282. :type file_path: pathlib.Path
  283. :param path: The path to collect (deprecated).
  284. .. versionchanged:: 7.0.0
  285. The ``file_path`` parameter was added as a :class:`pathlib.Path`
  286. equivalent of the ``path`` parameter. The ``path`` parameter
  287. has been deprecated.
  288. Use in conftest plugins
  289. =======================
  290. Any conftest file can implement this hook. For a given file path, only
  291. conftest files in parent directories of the file path are consulted.
  292. """
  293. # logging hooks for collection
  294. def pytest_collectstart(collector: Collector) -> None:
  295. """Collector starts collecting.
  296. :param collector:
  297. The collector.
  298. Use in conftest plugins
  299. =======================
  300. Any conftest file can implement this hook. For a given collector, only
  301. conftest files in the collector's directory and its parent directories are
  302. consulted.
  303. """
  304. def pytest_itemcollected(item: Item) -> None:
  305. """We just collected a test item.
  306. :param item:
  307. The item.
  308. Use in conftest plugins
  309. =======================
  310. Any conftest file can implement this hook. For a given item, only conftest
  311. files in the item's directory and its parent directories are consulted.
  312. """
  313. def pytest_collectreport(report: CollectReport) -> None:
  314. """Collector finished collecting.
  315. :param report:
  316. The collect report.
  317. Use in conftest plugins
  318. =======================
  319. Any conftest file can implement this hook. For a given collector, only
  320. conftest files in the collector's directory and its parent directories are
  321. consulted.
  322. """
  323. def pytest_deselected(items: Sequence[Item]) -> None:
  324. """Called for deselected test items, e.g. by keyword.
  325. Note that this hook has two integration aspects for plugins:
  326. - it can be *implemented* to be notified of deselected items
  327. - it must be *called* from :hook:`pytest_collection_modifyitems`
  328. implementations when items are deselected (to properly notify other plugins).
  329. May be called multiple times.
  330. :param items:
  331. The items.
  332. Use in conftest plugins
  333. =======================
  334. Any conftest file can implement this hook.
  335. """
  336. @hookspec(firstresult=True)
  337. def pytest_make_collect_report(collector: Collector) -> CollectReport | None:
  338. """Perform :func:`collector.collect() <pytest.Collector.collect>` and return
  339. a :class:`~pytest.CollectReport`.
  340. Stops at first non-None result, see :ref:`firstresult`.
  341. :param collector:
  342. The collector.
  343. Use in conftest plugins
  344. =======================
  345. Any conftest file can implement this hook. For a given collector, only
  346. conftest files in the collector's directory and its parent directories are
  347. consulted.
  348. """
  349. # -------------------------------------------------------------------------
  350. # Python test function related hooks
  351. # -------------------------------------------------------------------------
  352. @hookspec(
  353. firstresult=True,
  354. warn_on_impl_args={
  355. "path": HOOK_LEGACY_PATH_ARG.format(
  356. pylib_path_arg="path", pathlib_path_arg="module_path"
  357. ),
  358. },
  359. )
  360. def pytest_pycollect_makemodule(
  361. module_path: Path, path: LEGACY_PATH, parent
  362. ) -> Module | None:
  363. """Return a :class:`pytest.Module` collector or None for the given path.
  364. This hook will be called for each matching test module path.
  365. The :hook:`pytest_collect_file` hook needs to be used if you want to
  366. create test modules for files that do not match as a test module.
  367. Stops at first non-None result, see :ref:`firstresult`.
  368. :param module_path: The path of the module to collect.
  369. :type module_path: pathlib.Path
  370. :param path: The path of the module to collect (deprecated).
  371. .. versionchanged:: 7.0.0
  372. The ``module_path`` parameter was added as a :class:`pathlib.Path`
  373. equivalent of the ``path`` parameter.
  374. The ``path`` parameter has been deprecated in favor of ``fspath``.
  375. Use in conftest plugins
  376. =======================
  377. Any conftest file can implement this hook. For a given parent collector,
  378. only conftest files in the collector's directory and its parent directories
  379. are consulted.
  380. """
  381. @hookspec(firstresult=True)
  382. def pytest_pycollect_makeitem(
  383. collector: Module | Class, name: str, obj: object
  384. ) -> None | Item | Collector | list[Item | Collector]:
  385. """Return a custom item/collector for a Python object in a module, or None.
  386. Stops at first non-None result, see :ref:`firstresult`.
  387. :param collector:
  388. The module/class collector.
  389. :param name:
  390. The name of the object in the module/class.
  391. :param obj:
  392. The object.
  393. :returns:
  394. The created items/collectors.
  395. Use in conftest plugins
  396. =======================
  397. Any conftest file can implement this hook. For a given collector, only
  398. conftest files in the collector's directory and its parent directories
  399. are consulted.
  400. """
  401. @hookspec(firstresult=True)
  402. def pytest_pyfunc_call(pyfuncitem: Function) -> object | None:
  403. """Call underlying test function.
  404. Stops at first non-None result, see :ref:`firstresult`.
  405. :param pyfuncitem:
  406. The function item.
  407. Use in conftest plugins
  408. =======================
  409. Any conftest file can implement this hook. For a given item, only
  410. conftest files in the item's directory and its parent directories
  411. are consulted.
  412. """
  413. def pytest_generate_tests(metafunc: Metafunc) -> None:
  414. """Generate (multiple) parametrized calls to a test function.
  415. :param metafunc:
  416. The :class:`~pytest.Metafunc` helper for the test function.
  417. Use in conftest plugins
  418. =======================
  419. Any conftest file can implement this hook. For a given function definition,
  420. only conftest files in the functions's directory and its parent directories
  421. are consulted.
  422. """
  423. @hookspec(firstresult=True)
  424. def pytest_make_parametrize_id(config: Config, val: object, argname: str) -> str | None:
  425. """Return a user-friendly string representation of the given ``val``
  426. that will be used by @pytest.mark.parametrize calls, or None if the hook
  427. doesn't know about ``val``.
  428. The parameter name is available as ``argname``, if required.
  429. Stops at first non-None result, see :ref:`firstresult`.
  430. :param config: The pytest config object.
  431. :param val: The parametrized value.
  432. :param argname: The automatic parameter name produced by pytest.
  433. Use in conftest plugins
  434. =======================
  435. Any conftest file can implement this hook.
  436. """
  437. # -------------------------------------------------------------------------
  438. # runtest related hooks
  439. # -------------------------------------------------------------------------
  440. @hookspec(firstresult=True)
  441. def pytest_runtestloop(session: Session) -> object | None:
  442. """Perform the main runtest loop (after collection finished).
  443. The default hook implementation performs the runtest protocol for all items
  444. collected in the session (``session.items``), unless the collection failed
  445. or the ``collectonly`` pytest option is set.
  446. If at any point :py:func:`pytest.exit` is called, the loop is
  447. terminated immediately.
  448. If at any point ``session.shouldfail`` or ``session.shouldstop`` are set, the
  449. loop is terminated after the runtest protocol for the current item is finished.
  450. :param session: The pytest session object.
  451. Stops at first non-None result, see :ref:`firstresult`.
  452. The return value is not used, but only stops further processing.
  453. Use in conftest plugins
  454. =======================
  455. Any conftest file can implement this hook.
  456. """
  457. @hookspec(firstresult=True)
  458. def pytest_runtest_protocol(item: Item, nextitem: Item | None) -> object | None:
  459. """Perform the runtest protocol for a single test item.
  460. The default runtest protocol is this (see individual hooks for full details):
  461. - ``pytest_runtest_logstart(nodeid, location)``
  462. - Setup phase:
  463. - ``call = pytest_runtest_setup(item)`` (wrapped in ``CallInfo(when="setup")``)
  464. - ``report = pytest_runtest_makereport(item, call)``
  465. - ``pytest_runtest_logreport(report)``
  466. - ``pytest_exception_interact(call, report)`` if an interactive exception occurred
  467. - Call phase, if the setup passed and the ``setuponly`` pytest option is not set:
  468. - ``call = pytest_runtest_call(item)`` (wrapped in ``CallInfo(when="call")``)
  469. - ``report = pytest_runtest_makereport(item, call)``
  470. - ``pytest_runtest_logreport(report)``
  471. - ``pytest_exception_interact(call, report)`` if an interactive exception occurred
  472. - Teardown phase:
  473. - ``call = pytest_runtest_teardown(item, nextitem)`` (wrapped in ``CallInfo(when="teardown")``)
  474. - ``report = pytest_runtest_makereport(item, call)``
  475. - ``pytest_runtest_logreport(report)``
  476. - ``pytest_exception_interact(call, report)`` if an interactive exception occurred
  477. - ``pytest_runtest_logfinish(nodeid, location)``
  478. :param item: Test item for which the runtest protocol is performed.
  479. :param nextitem: The scheduled-to-be-next test item (or None if this is the end my friend).
  480. Stops at first non-None result, see :ref:`firstresult`.
  481. The return value is not used, but only stops further processing.
  482. Use in conftest plugins
  483. =======================
  484. Any conftest file can implement this hook.
  485. """
  486. def pytest_runtest_logstart(nodeid: str, location: tuple[str, int | None, str]) -> None:
  487. """Called at the start of running the runtest protocol for a single item.
  488. See :hook:`pytest_runtest_protocol` for a description of the runtest protocol.
  489. :param nodeid: Full node ID of the item.
  490. :param location: A tuple of ``(filename, lineno, testname)``
  491. where ``filename`` is a file path relative to ``config.rootpath``
  492. and ``lineno`` is 0-based.
  493. Use in conftest plugins
  494. =======================
  495. Any conftest file can implement this hook. For a given item, only conftest
  496. files in the item's directory and its parent directories are consulted.
  497. """
  498. def pytest_runtest_logfinish(
  499. nodeid: str, location: tuple[str, int | None, str]
  500. ) -> None:
  501. """Called at the end of running the runtest protocol for a single item.
  502. See :hook:`pytest_runtest_protocol` for a description of the runtest protocol.
  503. :param nodeid: Full node ID of the item.
  504. :param location: A tuple of ``(filename, lineno, testname)``
  505. where ``filename`` is a file path relative to ``config.rootpath``
  506. and ``lineno`` is 0-based.
  507. Use in conftest plugins
  508. =======================
  509. Any conftest file can implement this hook. For a given item, only conftest
  510. files in the item's directory and its parent directories are consulted.
  511. """
  512. def pytest_runtest_setup(item: Item) -> None:
  513. """Called to perform the setup phase for a test item.
  514. The default implementation runs ``setup()`` on ``item`` and all of its
  515. parents (which haven't been setup yet). This includes obtaining the
  516. values of fixtures required by the item (which haven't been obtained
  517. yet).
  518. :param item:
  519. The item.
  520. Use in conftest plugins
  521. =======================
  522. Any conftest file can implement this hook. For a given item, only conftest
  523. files in the item's directory and its parent directories are consulted.
  524. """
  525. def pytest_runtest_call(item: Item) -> None:
  526. """Called to run the test for test item (the call phase).
  527. The default implementation calls ``item.runtest()``.
  528. :param item:
  529. The item.
  530. Use in conftest plugins
  531. =======================
  532. Any conftest file can implement this hook. For a given item, only conftest
  533. files in the item's directory and its parent directories are consulted.
  534. """
  535. def pytest_runtest_teardown(item: Item, nextitem: Item | None) -> None:
  536. """Called to perform the teardown phase for a test item.
  537. The default implementation runs the finalizers and calls ``teardown()``
  538. on ``item`` and all of its parents (which need to be torn down). This
  539. includes running the teardown phase of fixtures required by the item (if
  540. they go out of scope).
  541. :param item:
  542. The item.
  543. :param nextitem:
  544. The scheduled-to-be-next test item (None if no further test item is
  545. scheduled). This argument is used to perform exact teardowns, i.e.
  546. calling just enough finalizers so that nextitem only needs to call
  547. setup functions.
  548. Use in conftest plugins
  549. =======================
  550. Any conftest file can implement this hook. For a given item, only conftest
  551. files in the item's directory and its parent directories are consulted.
  552. """
  553. @hookspec(firstresult=True)
  554. def pytest_runtest_makereport(item: Item, call: CallInfo[None]) -> TestReport | None:
  555. """Called to create a :class:`~pytest.TestReport` for each of
  556. the setup, call and teardown runtest phases of a test item.
  557. See :hook:`pytest_runtest_protocol` for a description of the runtest protocol.
  558. :param item: The item.
  559. :param call: The :class:`~pytest.CallInfo` for the phase.
  560. Stops at first non-None result, see :ref:`firstresult`.
  561. Use in conftest plugins
  562. =======================
  563. Any conftest file can implement this hook. For a given item, only conftest
  564. files in the item's directory and its parent directories are consulted.
  565. """
  566. def pytest_runtest_logreport(report: TestReport) -> None:
  567. """Process the :class:`~pytest.TestReport` produced for each
  568. of the setup, call and teardown runtest phases of an item.
  569. See :hook:`pytest_runtest_protocol` for a description of the runtest protocol.
  570. Use in conftest plugins
  571. =======================
  572. Any conftest file can implement this hook. For a given item, only conftest
  573. files in the item's directory and its parent directories are consulted.
  574. """
  575. @hookspec(firstresult=True)
  576. def pytest_report_to_serializable(
  577. config: Config,
  578. report: CollectReport | TestReport,
  579. ) -> dict[str, Any] | None:
  580. """Serialize the given report object into a data structure suitable for
  581. sending over the wire, e.g. converted to JSON.
  582. :param config: The pytest config object.
  583. :param report: The report.
  584. Use in conftest plugins
  585. =======================
  586. Any conftest file can implement this hook. The exact details may depend
  587. on the plugin which calls the hook.
  588. """
  589. @hookspec(firstresult=True)
  590. def pytest_report_from_serializable(
  591. config: Config,
  592. data: dict[str, Any],
  593. ) -> CollectReport | TestReport | None:
  594. """Restore a report object previously serialized with
  595. :hook:`pytest_report_to_serializable`.
  596. :param config: The pytest config object.
  597. Use in conftest plugins
  598. =======================
  599. Any conftest file can implement this hook. The exact details may depend
  600. on the plugin which calls the hook.
  601. """
  602. # -------------------------------------------------------------------------
  603. # Fixture related hooks
  604. # -------------------------------------------------------------------------
  605. @hookspec(firstresult=True)
  606. def pytest_fixture_setup(
  607. fixturedef: FixtureDef[Any], request: SubRequest
  608. ) -> object | None:
  609. """Perform fixture setup execution.
  610. :param fixturedef:
  611. The fixture definition object.
  612. :param request:
  613. The fixture request object.
  614. :returns:
  615. The return value of the call to the fixture function.
  616. Stops at first non-None result, see :ref:`firstresult`.
  617. .. note::
  618. If the fixture function returns None, other implementations of
  619. this hook function will continue to be called, according to the
  620. behavior of the :ref:`firstresult` option.
  621. Use in conftest plugins
  622. =======================
  623. Any conftest file can implement this hook. For a given fixture, only
  624. conftest files in the fixture scope's directory and its parent directories
  625. are consulted.
  626. """
  627. def pytest_fixture_post_finalizer(
  628. fixturedef: FixtureDef[Any], request: SubRequest
  629. ) -> None:
  630. """Called after fixture teardown, but before the cache is cleared, so
  631. the fixture result ``fixturedef.cached_result`` is still available (not
  632. ``None``).
  633. :param fixturedef:
  634. The fixture definition object.
  635. :param request:
  636. The fixture request object.
  637. Use in conftest plugins
  638. =======================
  639. Any conftest file can implement this hook. For a given fixture, only
  640. conftest files in the fixture scope's directory and its parent directories
  641. are consulted.
  642. """
  643. # -------------------------------------------------------------------------
  644. # test session related hooks
  645. # -------------------------------------------------------------------------
  646. def pytest_sessionstart(session: Session) -> None:
  647. """Called after the ``Session`` object has been created and before performing collection
  648. and entering the run test loop.
  649. :param session: The pytest session object.
  650. Use in conftest plugins
  651. =======================
  652. This hook is only called for :ref:`initial conftests <pluginorder>`.
  653. """
  654. def pytest_sessionfinish(
  655. session: Session,
  656. exitstatus: int | ExitCode,
  657. ) -> None:
  658. """Called after whole test run finished, right before returning the exit status to the system.
  659. :param session: The pytest session object.
  660. :param exitstatus: The status which pytest will return to the system.
  661. Use in conftest plugins
  662. =======================
  663. Any conftest file can implement this hook.
  664. """
  665. def pytest_unconfigure(config: Config) -> None:
  666. """Called before test process is exited.
  667. :param config: The pytest config object.
  668. Use in conftest plugins
  669. =======================
  670. Any conftest file can implement this hook.
  671. """
  672. # -------------------------------------------------------------------------
  673. # hooks for customizing the assert methods
  674. # -------------------------------------------------------------------------
  675. def pytest_assertrepr_compare(
  676. config: Config, op: str, left: object, right: object
  677. ) -> list[str] | None:
  678. """Return explanation for comparisons in failing assert expressions.
  679. Return None for no custom explanation, otherwise return a list
  680. of strings. The strings will be joined by newlines but any newlines
  681. *in* a string will be escaped. Note that all but the first line will
  682. be indented slightly, the intention is for the first line to be a summary.
  683. :param config: The pytest config object.
  684. :param op: The operator, e.g. `"=="`, `"!="`, `"not in"`.
  685. :param left: The left operand.
  686. :param right: The right operand.
  687. Use in conftest plugins
  688. =======================
  689. Any conftest file can implement this hook. For a given item, only conftest
  690. files in the item's directory and its parent directories are consulted.
  691. """
  692. def pytest_assertion_pass(item: Item, lineno: int, orig: str, expl: str) -> None:
  693. """Called whenever an assertion passes.
  694. .. versionadded:: 5.0
  695. Use this hook to do some processing after a passing assertion.
  696. The original assertion information is available in the `orig` string
  697. and the pytest introspected assertion information is available in the
  698. `expl` string.
  699. This hook must be explicitly enabled by the :confval:`enable_assertion_pass_hook`
  700. configuration option:
  701. .. tab:: toml
  702. .. code-block:: toml
  703. [pytest]
  704. enable_assertion_pass_hook = true
  705. .. tab:: ini
  706. .. code-block:: ini
  707. [pytest]
  708. enable_assertion_pass_hook = true
  709. You need to **clean the .pyc** files in your project directory and interpreter libraries
  710. when enabling this option, as assertions will require to be re-written.
  711. :param item: pytest item object of current test.
  712. :param lineno: Line number of the assert statement.
  713. :param orig: String with the original assertion.
  714. :param expl: String with the assert explanation.
  715. Use in conftest plugins
  716. =======================
  717. Any conftest file can implement this hook. For a given item, only conftest
  718. files in the item's directory and its parent directories are consulted.
  719. """
  720. # -------------------------------------------------------------------------
  721. # Hooks for influencing reporting (invoked from _pytest_terminal).
  722. # -------------------------------------------------------------------------
  723. @hookspec(
  724. warn_on_impl_args={
  725. "startdir": HOOK_LEGACY_PATH_ARG.format(
  726. pylib_path_arg="startdir", pathlib_path_arg="start_path"
  727. ),
  728. },
  729. )
  730. def pytest_report_header( # type:ignore[empty-body]
  731. config: Config, start_path: Path, startdir: LEGACY_PATH
  732. ) -> str | list[str]:
  733. """Return a string or list of strings to be displayed as header info for terminal reporting.
  734. :param config: The pytest config object.
  735. :param start_path: The starting dir.
  736. :type start_path: pathlib.Path
  737. :param startdir: The starting dir (deprecated).
  738. .. note::
  739. Lines returned by a plugin are displayed before those of plugins which
  740. ran before it.
  741. If you want to have your line(s) displayed first, use
  742. :ref:`trylast=True <plugin-hookorder>`.
  743. .. versionchanged:: 7.0.0
  744. The ``start_path`` parameter was added as a :class:`pathlib.Path`
  745. equivalent of the ``startdir`` parameter. The ``startdir`` parameter
  746. has been deprecated.
  747. Use in conftest plugins
  748. =======================
  749. This hook is only called for :ref:`initial conftests <pluginorder>`.
  750. """
  751. @hookspec(
  752. warn_on_impl_args={
  753. "startdir": HOOK_LEGACY_PATH_ARG.format(
  754. pylib_path_arg="startdir", pathlib_path_arg="start_path"
  755. ),
  756. },
  757. )
  758. def pytest_report_collectionfinish( # type:ignore[empty-body]
  759. config: Config,
  760. start_path: Path,
  761. startdir: LEGACY_PATH,
  762. items: Sequence[Item],
  763. ) -> str | list[str]:
  764. """Return a string or list of strings to be displayed after collection
  765. has finished successfully.
  766. These strings will be displayed after the standard "collected X items" message.
  767. .. versionadded:: 3.2
  768. :param config: The pytest config object.
  769. :param start_path: The starting dir.
  770. :type start_path: pathlib.Path
  771. :param startdir: The starting dir (deprecated).
  772. :param items: List of pytest items that are going to be executed; this list should not be modified.
  773. .. note::
  774. Lines returned by a plugin are displayed before those of plugins which
  775. ran before it.
  776. If you want to have your line(s) displayed first, use
  777. :ref:`trylast=True <plugin-hookorder>`.
  778. .. versionchanged:: 7.0.0
  779. The ``start_path`` parameter was added as a :class:`pathlib.Path`
  780. equivalent of the ``startdir`` parameter. The ``startdir`` parameter
  781. has been deprecated.
  782. Use in conftest plugins
  783. =======================
  784. Any conftest plugin can implement this hook.
  785. """
  786. @hookspec(firstresult=True)
  787. def pytest_report_teststatus( # type:ignore[empty-body]
  788. report: CollectReport | TestReport, config: Config
  789. ) -> TestShortLogReport | tuple[str, str, str | tuple[str, Mapping[str, bool]]]:
  790. """Return result-category, shortletter and verbose word for status
  791. reporting.
  792. The result-category is a category in which to count the result, for
  793. example "passed", "skipped", "error" or the empty string.
  794. The shortletter is shown as testing progresses, for example ".", "s",
  795. "E" or the empty string.
  796. The verbose word is shown as testing progresses in verbose mode, for
  797. example "PASSED", "SKIPPED", "ERROR" or the empty string.
  798. pytest may style these implicitly according to the report outcome.
  799. To provide explicit styling, return a tuple for the verbose word,
  800. for example ``"rerun", "R", ("RERUN", {"yellow": True})``.
  801. :param report: The report object whose status is to be returned.
  802. :param config: The pytest config object.
  803. :returns: The test status.
  804. Stops at first non-None result, see :ref:`firstresult`.
  805. Use in conftest plugins
  806. =======================
  807. Any conftest plugin can implement this hook.
  808. """
  809. def pytest_terminal_summary(
  810. terminalreporter: TerminalReporter,
  811. exitstatus: ExitCode,
  812. config: Config,
  813. ) -> None:
  814. """Add a section to terminal summary reporting.
  815. :param terminalreporter: The internal terminal reporter object.
  816. :param exitstatus: The exit status that will be reported back to the OS.
  817. :param config: The pytest config object.
  818. .. versionadded:: 4.2
  819. The ``config`` parameter.
  820. Use in conftest plugins
  821. =======================
  822. Any conftest plugin can implement this hook.
  823. """
  824. @hookspec(historic=True)
  825. def pytest_warning_recorded(
  826. warning_message: warnings.WarningMessage,
  827. when: Literal["config", "collect", "runtest"],
  828. nodeid: str,
  829. location: tuple[str, int, str] | None,
  830. ) -> None:
  831. """Process a warning captured by the internal pytest warnings plugin.
  832. :param warning_message:
  833. The captured warning. This is the same object produced by :class:`warnings.catch_warnings`,
  834. and contains the same attributes as the parameters of :py:func:`warnings.showwarning`.
  835. :param when:
  836. Indicates when the warning was captured. Possible values:
  837. * ``"config"``: during pytest configuration/initialization stage.
  838. * ``"collect"``: during test collection.
  839. * ``"runtest"``: during test execution.
  840. :param nodeid:
  841. Full id of the item. Empty string for warnings that are not specific to
  842. a particular node.
  843. :param location:
  844. When available, holds information about the execution context of the captured
  845. warning (filename, linenumber, function). ``function`` evaluates to <module>
  846. when the execution context is at the module level.
  847. .. versionadded:: 6.0
  848. Use in conftest plugins
  849. =======================
  850. Any conftest file can implement this hook. If the warning is specific to a
  851. particular node, only conftest files in parent directories of the node are
  852. consulted.
  853. """
  854. # -------------------------------------------------------------------------
  855. # Hooks for influencing skipping
  856. # -------------------------------------------------------------------------
  857. def pytest_markeval_namespace( # type:ignore[empty-body]
  858. config: Config,
  859. ) -> dict[str, Any]:
  860. """Called when constructing the globals dictionary used for
  861. evaluating string conditions in xfail/skipif markers.
  862. This is useful when the condition for a marker requires
  863. objects that are expensive or impossible to obtain during
  864. collection time, which is required by normal boolean
  865. conditions.
  866. .. versionadded:: 6.2
  867. :param config: The pytest config object.
  868. :returns: A dictionary of additional globals to add.
  869. Use in conftest plugins
  870. =======================
  871. Any conftest file can implement this hook. For a given item, only conftest
  872. files in parent directories of the item are consulted.
  873. """
  874. # -------------------------------------------------------------------------
  875. # error handling and internal debugging hooks
  876. # -------------------------------------------------------------------------
  877. def pytest_internalerror(
  878. excrepr: ExceptionRepr,
  879. excinfo: ExceptionInfo[BaseException],
  880. ) -> bool | None:
  881. """Called for internal errors.
  882. Return True to suppress the fallback handling of printing an
  883. INTERNALERROR message directly to sys.stderr.
  884. :param excrepr: The exception repr object.
  885. :param excinfo: The exception info.
  886. Use in conftest plugins
  887. =======================
  888. Any conftest plugin can implement this hook.
  889. """
  890. def pytest_keyboard_interrupt(
  891. excinfo: ExceptionInfo[KeyboardInterrupt | Exit],
  892. ) -> None:
  893. """Called for keyboard interrupt.
  894. :param excinfo: The exception info.
  895. Use in conftest plugins
  896. =======================
  897. Any conftest plugin can implement this hook.
  898. """
  899. def pytest_exception_interact(
  900. node: Item | Collector,
  901. call: CallInfo[Any],
  902. report: CollectReport | TestReport,
  903. ) -> None:
  904. """Called when an exception was raised which can potentially be
  905. interactively handled.
  906. May be called during collection (see :hook:`pytest_make_collect_report`),
  907. in which case ``report`` is a :class:`~pytest.CollectReport`.
  908. May be called during runtest of an item (see :hook:`pytest_runtest_protocol`),
  909. in which case ``report`` is a :class:`~pytest.TestReport`.
  910. This hook is not called if the exception that was raised is an internal
  911. exception like ``skip.Exception``.
  912. :param node:
  913. The item or collector.
  914. :param call:
  915. The call information. Contains the exception.
  916. :param report:
  917. The collection or test report.
  918. Use in conftest plugins
  919. =======================
  920. Any conftest file can implement this hook. For a given node, only conftest
  921. files in parent directories of the node are consulted.
  922. """
  923. def pytest_enter_pdb(config: Config, pdb: pdb.Pdb) -> None:
  924. """Called upon pdb.set_trace().
  925. Can be used by plugins to take special action just before the python
  926. debugger enters interactive mode.
  927. :param config: The pytest config object.
  928. :param pdb: The Pdb instance.
  929. Use in conftest plugins
  930. =======================
  931. Any conftest plugin can implement this hook.
  932. """
  933. def pytest_leave_pdb(config: Config, pdb: pdb.Pdb) -> None:
  934. """Called when leaving pdb (e.g. with continue after pdb.set_trace()).
  935. Can be used by plugins to take special action just after the python
  936. debugger leaves interactive mode.
  937. :param config: The pytest config object.
  938. :param pdb: The Pdb instance.
  939. Use in conftest plugins
  940. =======================
  941. Any conftest plugin can implement this hook.
  942. """