hookspec.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. # -*- coding: utf-8 -*-
  2. """ hook specifications for pytest plugins, invoked from main.py and builtin plugins. """
  3. from pluggy import HookspecMarker
  4. from _pytest.deprecated import PYTEST_LOGWARNING
  5. hookspec = HookspecMarker("pytest")
  6. # -------------------------------------------------------------------------
  7. # Initialization hooks called for every plugin
  8. # -------------------------------------------------------------------------
  9. @hookspec(historic=True)
  10. def pytest_addhooks(pluginmanager):
  11. """called at plugin registration time to allow adding new hooks via a call to
  12. ``pluginmanager.add_hookspecs(module_or_class, prefix)``.
  13. :param _pytest.config.PytestPluginManager pluginmanager: pytest plugin manager
  14. .. note::
  15. This hook is incompatible with ``hookwrapper=True``.
  16. """
  17. @hookspec(historic=True)
  18. def pytest_plugin_registered(plugin, manager):
  19. """ a new pytest plugin got registered.
  20. :param plugin: the plugin module or instance
  21. :param _pytest.config.PytestPluginManager manager: pytest plugin manager
  22. .. note::
  23. This hook is incompatible with ``hookwrapper=True``.
  24. """
  25. @hookspec(historic=True)
  26. def pytest_addoption(parser):
  27. """register argparse-style options and ini-style config values,
  28. called once at the beginning of a test run.
  29. .. note::
  30. This function should be implemented only in plugins or ``conftest.py``
  31. files situated at the tests root directory due to how pytest
  32. :ref:`discovers plugins during startup <pluginorder>`.
  33. :arg _pytest.config.Parser parser: To add command line options, call
  34. :py:func:`parser.addoption(...) <_pytest.config.Parser.addoption>`.
  35. To add ini-file values call :py:func:`parser.addini(...)
  36. <_pytest.config.Parser.addini>`.
  37. Options can later be accessed through the
  38. :py:class:`config <_pytest.config.Config>` object, respectively:
  39. - :py:func:`config.getoption(name) <_pytest.config.Config.getoption>` to
  40. retrieve the value of a command line option.
  41. - :py:func:`config.getini(name) <_pytest.config.Config.getini>` to retrieve
  42. a value read from an ini-style file.
  43. The config object is passed around on many internal objects via the ``.config``
  44. attribute or can be retrieved as the ``pytestconfig`` fixture.
  45. .. note::
  46. This hook is incompatible with ``hookwrapper=True``.
  47. """
  48. @hookspec(historic=True)
  49. def pytest_configure(config):
  50. """
  51. Allows plugins and conftest files to perform initial configuration.
  52. This hook is called for every plugin and initial conftest file
  53. after command line options have been parsed.
  54. After that, the hook is called for other conftest files as they are
  55. imported.
  56. .. note::
  57. This hook is incompatible with ``hookwrapper=True``.
  58. :arg _pytest.config.Config config: pytest config object
  59. """
  60. # -------------------------------------------------------------------------
  61. # Bootstrapping hooks called for plugins registered early enough:
  62. # internal and 3rd party plugins.
  63. # -------------------------------------------------------------------------
  64. @hookspec(firstresult=True)
  65. def pytest_cmdline_parse(pluginmanager, args):
  66. """return initialized config object, parsing the specified args.
  67. Stops at first non-None result, see :ref:`firstresult`
  68. .. note::
  69. This hook will only be called for plugin classes passed to the ``plugins`` arg when using `pytest.main`_ to
  70. perform an in-process test run.
  71. :param _pytest.config.PytestPluginManager pluginmanager: pytest plugin manager
  72. :param list[str] args: list of arguments passed on the command line
  73. """
  74. def pytest_cmdline_preparse(config, args):
  75. """(**Deprecated**) modify command line arguments before option parsing.
  76. This hook is considered deprecated and will be removed in a future pytest version. Consider
  77. using :func:`pytest_load_initial_conftests` instead.
  78. .. note::
  79. This hook will not be called for ``conftest.py`` files, only for setuptools plugins.
  80. :param _pytest.config.Config config: pytest config object
  81. :param list[str] args: list of arguments passed on the command line
  82. """
  83. @hookspec(firstresult=True)
  84. def pytest_cmdline_main(config):
  85. """ called for performing the main command line action. The default
  86. implementation will invoke the configure hooks and runtest_mainloop.
  87. .. note::
  88. This hook will not be called for ``conftest.py`` files, only for setuptools plugins.
  89. Stops at first non-None result, see :ref:`firstresult`
  90. :param _pytest.config.Config config: pytest config object
  91. """
  92. def pytest_load_initial_conftests(early_config, parser, args):
  93. """ implements the loading of initial conftest files ahead
  94. of command line option parsing.
  95. .. note::
  96. This hook will not be called for ``conftest.py`` files, only for setuptools plugins.
  97. :param _pytest.config.Config early_config: pytest config object
  98. :param list[str] args: list of arguments passed on the command line
  99. :param _pytest.config.Parser parser: to add command line options
  100. """
  101. # -------------------------------------------------------------------------
  102. # collection hooks
  103. # -------------------------------------------------------------------------
  104. @hookspec(firstresult=True)
  105. def pytest_collection(session):
  106. """Perform the collection protocol for the given session.
  107. Stops at first non-None result, see :ref:`firstresult`.
  108. :param _pytest.main.Session session: the pytest session object
  109. """
  110. def pytest_collection_modifyitems(session, config, items):
  111. """ called after collection has been performed, may filter or re-order
  112. the items in-place.
  113. :param _pytest.main.Session session: the pytest session object
  114. :param _pytest.config.Config config: pytest config object
  115. :param List[_pytest.nodes.Item] items: list of item objects
  116. """
  117. def pytest_collection_finish(session):
  118. """ called after collection has been performed and modified.
  119. :param _pytest.main.Session session: the pytest session object
  120. """
  121. @hookspec(firstresult=True)
  122. def pytest_ignore_collect(path, config):
  123. """ return True to prevent considering this path for collection.
  124. This hook is consulted for all files and directories prior to calling
  125. more specific hooks.
  126. Stops at first non-None result, see :ref:`firstresult`
  127. :param path: a :py:class:`py.path.local` - the path to analyze
  128. :param _pytest.config.Config config: pytest config object
  129. """
  130. @hookspec(firstresult=True)
  131. def pytest_collect_directory(path, parent):
  132. """ called before traversing a directory for collection files.
  133. Stops at first non-None result, see :ref:`firstresult`
  134. :param path: a :py:class:`py.path.local` - the path to analyze
  135. """
  136. def pytest_collect_file(path, parent):
  137. """ return collection Node or None for the given path. Any new node
  138. needs to have the specified ``parent`` as a parent.
  139. :param path: a :py:class:`py.path.local` - the path to collect
  140. """
  141. # logging hooks for collection
  142. def pytest_collectstart(collector):
  143. """ collector starts collecting. """
  144. def pytest_itemcollected(item):
  145. """ we just collected a test item. """
  146. def pytest_collectreport(report):
  147. """ collector finished collecting. """
  148. def pytest_deselected(items):
  149. """ called for test items deselected, e.g. by keyword. """
  150. @hookspec(firstresult=True)
  151. def pytest_make_collect_report(collector):
  152. """ perform ``collector.collect()`` and return a CollectReport.
  153. Stops at first non-None result, see :ref:`firstresult` """
  154. # -------------------------------------------------------------------------
  155. # Python test function related hooks
  156. # -------------------------------------------------------------------------
  157. @hookspec(firstresult=True)
  158. def pytest_pycollect_makemodule(path, parent):
  159. """ return a Module collector or None for the given path.
  160. This hook will be called for each matching test module path.
  161. The pytest_collect_file hook needs to be used if you want to
  162. create test modules for files that do not match as a test module.
  163. Stops at first non-None result, see :ref:`firstresult`
  164. :param path: a :py:class:`py.path.local` - the path of module to collect
  165. """
  166. @hookspec(firstresult=True)
  167. def pytest_pycollect_makeitem(collector, name, obj):
  168. """ return custom item/collector for a python object in a module, or None.
  169. Stops at first non-None result, see :ref:`firstresult` """
  170. @hookspec(firstresult=True)
  171. def pytest_pyfunc_call(pyfuncitem):
  172. """ call underlying test function.
  173. Stops at first non-None result, see :ref:`firstresult` """
  174. def pytest_generate_tests(metafunc):
  175. """ generate (multiple) parametrized calls to a test function."""
  176. @hookspec(firstresult=True)
  177. def pytest_make_parametrize_id(config, val, argname):
  178. """Return a user-friendly string representation of the given ``val`` that will be used
  179. by @pytest.mark.parametrize calls. Return None if the hook doesn't know about ``val``.
  180. The parameter name is available as ``argname``, if required.
  181. Stops at first non-None result, see :ref:`firstresult`
  182. :param _pytest.config.Config config: pytest config object
  183. :param val: the parametrized value
  184. :param str argname: the automatic parameter name produced by pytest
  185. """
  186. # -------------------------------------------------------------------------
  187. # generic runtest related hooks
  188. # -------------------------------------------------------------------------
  189. @hookspec(firstresult=True)
  190. def pytest_runtestloop(session):
  191. """ called for performing the main runtest loop
  192. (after collection finished).
  193. Stops at first non-None result, see :ref:`firstresult`
  194. :param _pytest.main.Session session: the pytest session object
  195. """
  196. def pytest_itemstart(item, node):
  197. """(**Deprecated**) use pytest_runtest_logstart. """
  198. @hookspec(firstresult=True)
  199. def pytest_runtest_protocol(item, nextitem):
  200. """ implements the runtest_setup/call/teardown protocol for
  201. the given test item, including capturing exceptions and calling
  202. reporting hooks.
  203. :arg item: test item for which the runtest protocol is performed.
  204. :arg nextitem: the scheduled-to-be-next test item (or None if this
  205. is the end my friend). This argument is passed on to
  206. :py:func:`pytest_runtest_teardown`.
  207. :return boolean: True if no further hook implementations should be invoked.
  208. Stops at first non-None result, see :ref:`firstresult` """
  209. def pytest_runtest_logstart(nodeid, location):
  210. """ signal the start of running a single test item.
  211. This hook will be called **before** :func:`pytest_runtest_setup`, :func:`pytest_runtest_call` and
  212. :func:`pytest_runtest_teardown` hooks.
  213. :param str nodeid: full id of the item
  214. :param location: a triple of ``(filename, linenum, testname)``
  215. """
  216. def pytest_runtest_logfinish(nodeid, location):
  217. """ signal the complete finish of running a single test item.
  218. This hook will be called **after** :func:`pytest_runtest_setup`, :func:`pytest_runtest_call` and
  219. :func:`pytest_runtest_teardown` hooks.
  220. :param str nodeid: full id of the item
  221. :param location: a triple of ``(filename, linenum, testname)``
  222. """
  223. def pytest_runtest_setup(item):
  224. """ called before ``pytest_runtest_call(item)``. """
  225. def pytest_runtest_call(item):
  226. """ called to execute the test ``item``. """
  227. def pytest_runtest_teardown(item, nextitem):
  228. """ called after ``pytest_runtest_call``.
  229. :arg nextitem: the scheduled-to-be-next test item (None if no further
  230. test item is scheduled). This argument can be used to
  231. perform exact teardowns, i.e. calling just enough finalizers
  232. so that nextitem only needs to call setup-functions.
  233. """
  234. @hookspec(firstresult=True)
  235. def pytest_runtest_makereport(item, call):
  236. """ return a :py:class:`_pytest.runner.TestReport` object
  237. for the given :py:class:`pytest.Item <_pytest.main.Item>` and
  238. :py:class:`_pytest.runner.CallInfo`.
  239. Stops at first non-None result, see :ref:`firstresult` """
  240. def pytest_runtest_logreport(report):
  241. """ process a test setup/call/teardown report relating to
  242. the respective phase of executing a test. """
  243. @hookspec(firstresult=True)
  244. def pytest_report_to_serializable(config, report):
  245. """
  246. .. warning::
  247. This hook is experimental and subject to change between pytest releases, even
  248. bug fixes.
  249. The intent is for this to be used by plugins maintained by the core-devs, such
  250. as ``pytest-xdist``, ``pytest-subtests``, and as a replacement for the internal
  251. 'resultlog' plugin.
  252. In the future it might become part of the public hook API.
  253. Serializes the given report object into a data structure suitable for sending
  254. over the wire, e.g. converted to JSON.
  255. """
  256. @hookspec(firstresult=True)
  257. def pytest_report_from_serializable(config, data):
  258. """
  259. .. warning::
  260. This hook is experimental and subject to change between pytest releases, even
  261. bug fixes.
  262. The intent is for this to be used by plugins maintained by the core-devs, such
  263. as ``pytest-xdist``, ``pytest-subtests``, and as a replacement for the internal
  264. 'resultlog' plugin.
  265. In the future it might become part of the public hook API.
  266. Restores a report object previously serialized with pytest_report_to_serializable().
  267. """
  268. # -------------------------------------------------------------------------
  269. # Fixture related hooks
  270. # -------------------------------------------------------------------------
  271. @hookspec(firstresult=True)
  272. def pytest_fixture_setup(fixturedef, request):
  273. """ performs fixture setup execution.
  274. :return: The return value of the call to the fixture function
  275. Stops at first non-None result, see :ref:`firstresult`
  276. .. note::
  277. If the fixture function returns None, other implementations of
  278. this hook function will continue to be called, according to the
  279. behavior of the :ref:`firstresult` option.
  280. """
  281. def pytest_fixture_post_finalizer(fixturedef, request):
  282. """ called after fixture teardown, but before the cache is cleared so
  283. the fixture result cache ``fixturedef.cached_result`` can
  284. still be accessed."""
  285. # -------------------------------------------------------------------------
  286. # test session related hooks
  287. # -------------------------------------------------------------------------
  288. def pytest_sessionstart(session):
  289. """ called after the ``Session`` object has been created and before performing collection
  290. and entering the run test loop.
  291. :param _pytest.main.Session session: the pytest session object
  292. """
  293. def pytest_sessionfinish(session, exitstatus):
  294. """ called after whole test run finished, right before returning the exit status to the system.
  295. :param _pytest.main.Session session: the pytest session object
  296. :param int exitstatus: the status which pytest will return to the system
  297. """
  298. def pytest_unconfigure(config):
  299. """ called before test process is exited.
  300. :param _pytest.config.Config config: pytest config object
  301. """
  302. # -------------------------------------------------------------------------
  303. # hooks for customizing the assert methods
  304. # -------------------------------------------------------------------------
  305. def pytest_assertrepr_compare(config, op, left, right):
  306. """return explanation for comparisons in failing assert expressions.
  307. Return None for no custom explanation, otherwise return a list
  308. of strings. The strings will be joined by newlines but any newlines
  309. *in* a string will be escaped. Note that all but the first line will
  310. be indented slightly, the intention is for the first line to be a summary.
  311. :param _pytest.config.Config config: pytest config object
  312. """
  313. # -------------------------------------------------------------------------
  314. # hooks for influencing reporting (invoked from _pytest_terminal)
  315. # -------------------------------------------------------------------------
  316. def pytest_report_header(config, startdir):
  317. """ return a string or list of strings to be displayed as header info for terminal reporting.
  318. :param _pytest.config.Config config: pytest config object
  319. :param startdir: py.path object with the starting dir
  320. .. note::
  321. This function should be implemented only in plugins or ``conftest.py``
  322. files situated at the tests root directory due to how pytest
  323. :ref:`discovers plugins during startup <pluginorder>`.
  324. """
  325. def pytest_report_collectionfinish(config, startdir, items):
  326. """
  327. .. versionadded:: 3.2
  328. return a string or list of strings to be displayed after collection has finished successfully.
  329. This strings will be displayed after the standard "collected X items" message.
  330. :param _pytest.config.Config config: pytest config object
  331. :param startdir: py.path object with the starting dir
  332. :param items: list of pytest items that are going to be executed; this list should not be modified.
  333. """
  334. @hookspec(firstresult=True)
  335. def pytest_report_teststatus(report, config):
  336. """ return result-category, shortletter and verbose word for reporting.
  337. :param _pytest.config.Config config: pytest config object
  338. Stops at first non-None result, see :ref:`firstresult` """
  339. def pytest_terminal_summary(terminalreporter, exitstatus, config):
  340. """Add a section to terminal summary reporting.
  341. :param _pytest.terminal.TerminalReporter terminalreporter: the internal terminal reporter object
  342. :param int exitstatus: the exit status that will be reported back to the OS
  343. :param _pytest.config.Config config: pytest config object
  344. .. versionadded:: 4.2
  345. The ``config`` parameter.
  346. """
  347. @hookspec(historic=True, warn_on_impl=PYTEST_LOGWARNING)
  348. def pytest_logwarning(message, code, nodeid, fslocation):
  349. """
  350. .. deprecated:: 3.8
  351. This hook is will stop working in a future release.
  352. pytest no longer triggers this hook, but the
  353. terminal writer still implements it to display warnings issued by
  354. :meth:`_pytest.config.Config.warn` and :meth:`_pytest.nodes.Node.warn`. Calling those functions will be
  355. an error in future releases.
  356. process a warning specified by a message, a code string,
  357. a nodeid and fslocation (both of which may be None
  358. if the warning is not tied to a particular node/location).
  359. .. note::
  360. This hook is incompatible with ``hookwrapper=True``.
  361. """
  362. @hookspec(historic=True)
  363. def pytest_warning_captured(warning_message, when, item):
  364. """
  365. Process a warning captured by the internal pytest warnings plugin.
  366. :param warnings.WarningMessage warning_message:
  367. The captured warning. This is the same object produced by :py:func:`warnings.catch_warnings`, and contains
  368. the same attributes as the parameters of :py:func:`warnings.showwarning`.
  369. :param str when:
  370. Indicates when the warning was captured. Possible values:
  371. * ``"config"``: during pytest configuration/initialization stage.
  372. * ``"collect"``: during test collection.
  373. * ``"runtest"``: during test execution.
  374. :param pytest.Item|None item:
  375. **DEPRECATED**: This parameter is incompatible with ``pytest-xdist``, and will always receive ``None``
  376. in a future release.
  377. The item being executed if ``when`` is ``"runtest"``, otherwise ``None``.
  378. """
  379. # -------------------------------------------------------------------------
  380. # doctest hooks
  381. # -------------------------------------------------------------------------
  382. @hookspec(firstresult=True)
  383. def pytest_doctest_prepare_content(content):
  384. """ return processed content for a given doctest
  385. Stops at first non-None result, see :ref:`firstresult` """
  386. # -------------------------------------------------------------------------
  387. # error handling and internal debugging hooks
  388. # -------------------------------------------------------------------------
  389. def pytest_internalerror(excrepr, excinfo):
  390. """ called for internal errors. """
  391. def pytest_keyboard_interrupt(excinfo):
  392. """ called for keyboard interrupt. """
  393. def pytest_exception_interact(node, call, report):
  394. """called when an exception was raised which can potentially be
  395. interactively handled.
  396. This hook is only called if an exception was raised
  397. that is not an internal exception like ``skip.Exception``.
  398. """
  399. def pytest_enter_pdb(config, pdb):
  400. """ called upon pdb.set_trace(), can be used by plugins to take special
  401. action just before the python debugger enters in interactive mode.
  402. :param _pytest.config.Config config: pytest config object
  403. :param pdb.Pdb pdb: Pdb instance
  404. """
  405. def pytest_leave_pdb(config, pdb):
  406. """ called when leaving pdb (e.g. with continue after pdb.set_trace()).
  407. Can be used by plugins to take special action just after the python
  408. debugger leaves interactive mode.
  409. :param _pytest.config.Config config: pytest config object
  410. :param pdb.Pdb pdb: Pdb instance
  411. """