hookspec.py 19 KB

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