2010-11-06 06:37:31 +08:00
|
|
|
""" hook specifications for pytest plugins, invoked from main.py and builtin plugins. """
|
2009-06-11 20:48:53 +08:00
|
|
|
|
2015-08-26 06:43:09 +08:00
|
|
|
from _pytest._pluggy import HookspecMarker
|
2015-04-29 22:40:52 +08:00
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
hookspec = HookspecMarker("pytest")
|
2015-04-25 17:29:11 +08:00
|
|
|
|
2009-07-15 03:17:13 +08:00
|
|
|
# -------------------------------------------------------------------------
|
2015-04-25 17:29:11 +08:00
|
|
|
# Initialization hooks called for every plugin
|
2009-07-15 03:17:13 +08:00
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@hookspec(historic=True)
|
2010-11-01 06:28:31 +08:00
|
|
|
def pytest_addhooks(pluginmanager):
|
2015-04-25 17:29:11 +08:00
|
|
|
"""called at plugin registration time to allow adding new hooks via a call to
|
2015-05-06 16:08:08 +08:00
|
|
|
pluginmanager.add_hookspecs(module_or_class, prefix)."""
|
2010-11-01 06:28:31 +08:00
|
|
|
|
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@hookspec(historic=True)
|
2010-05-02 22:36:53 +08:00
|
|
|
def pytest_namespace():
|
2010-10-11 06:49:54 +08:00
|
|
|
"""return dict of name->object to be made globally available in
|
2015-04-25 17:29:11 +08:00
|
|
|
the pytest namespace. This hook is called at plugin registration
|
|
|
|
time.
|
2010-10-11 06:49:54 +08:00
|
|
|
"""
|
2009-06-11 20:48:53 +08:00
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@hookspec(historic=True)
|
2015-04-25 19:38:30 +08:00
|
|
|
def pytest_plugin_registered(plugin, manager):
|
|
|
|
""" a new pytest plugin got registered. """
|
|
|
|
|
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@hookspec(historic=True)
|
2010-05-02 22:36:53 +08:00
|
|
|
def pytest_addoption(parser):
|
2013-07-25 21:33:43 +08:00
|
|
|
"""register argparse-style options and ini-style config values.
|
2012-11-06 21:09:12 +08:00
|
|
|
|
2015-08-28 06:14:56 +08:00
|
|
|
.. warning::
|
|
|
|
|
|
|
|
This function must be implemented in a :ref:`plugin <pluginorder>`
|
|
|
|
and is called once at the beginning of a test run.
|
|
|
|
|
|
|
|
Implementing this hook from ``conftest.py`` files is **strongly**
|
|
|
|
discouraged because ``conftest.py`` files are lazily loaded and
|
|
|
|
may give strange *unknown option* errors depending on the directory
|
|
|
|
``py.test`` is invoked from.
|
2012-11-06 21:09:12 +08:00
|
|
|
|
|
|
|
:arg parser: To add command line options, call
|
|
|
|
:py:func:`parser.addoption(...) <_pytest.config.Parser.addoption>`.
|
|
|
|
To add ini-file values call :py:func:`parser.addini(...)
|
|
|
|
<_pytest.config.Parser.addini>`.
|
|
|
|
|
|
|
|
Options can later be accessed through the
|
|
|
|
:py:class:`config <_pytest.config.Config>` object, respectively:
|
|
|
|
|
|
|
|
- :py:func:`config.getoption(name) <_pytest.config.Config.getoption>` to
|
|
|
|
retrieve the value of a command line option.
|
|
|
|
|
|
|
|
- :py:func:`config.getini(name) <_pytest.config.Config.getini>` to retrieve
|
|
|
|
a value read from an ini-style file.
|
|
|
|
|
|
|
|
The config object is passed around on many internal objects via the ``.config``
|
|
|
|
attribute or can be retrieved as the ``pytestconfig`` fixture or accessed
|
|
|
|
via (deprecated) ``pytest.config``.
|
2010-11-01 06:28:31 +08:00
|
|
|
"""
|
2010-10-11 06:49:54 +08:00
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@hookspec(historic=True)
|
2015-04-25 17:29:11 +08:00
|
|
|
def pytest_configure(config):
|
|
|
|
""" called after command line options have been parsed
|
|
|
|
and all plugins and initial conftest files been loaded.
|
|
|
|
This hook is called for every plugin.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
# Bootstrapping hooks called for plugins registered early enough:
|
|
|
|
# internal and 3rd party plugins as well as directly
|
|
|
|
# discoverable conftest.py local plugins.
|
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@hookspec(firstresult=True)
|
2015-04-25 17:29:11 +08:00
|
|
|
def pytest_cmdline_parse(pluginmanager, args):
|
|
|
|
"""return initialized config object, parsing the specified args. """
|
|
|
|
|
|
|
|
def pytest_cmdline_preparse(config, args):
|
|
|
|
"""(deprecated) modify command line arguments before option parsing. """
|
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@hookspec(firstresult=True)
|
2010-10-11 06:49:54 +08:00
|
|
|
def pytest_cmdline_main(config):
|
|
|
|
""" called for performing the main command line action. The default
|
|
|
|
implementation will invoke the configure hooks and runtest_mainloop. """
|
2009-07-18 00:07:37 +08:00
|
|
|
|
2013-09-30 19:14:16 +08:00
|
|
|
def pytest_load_initial_conftests(args, early_config, parser):
|
2014-03-25 21:43:58 +08:00
|
|
|
""" implements the loading of initial conftest files ahead
|
|
|
|
of command line option parsing. """
|
2013-09-30 19:14:16 +08:00
|
|
|
|
2010-09-26 22:23:43 +08:00
|
|
|
|
2009-07-15 03:17:13 +08:00
|
|
|
# -------------------------------------------------------------------------
|
2009-06-11 20:48:53 +08:00
|
|
|
# collection hooks
|
2009-07-15 03:17:13 +08:00
|
|
|
# -------------------------------------------------------------------------
|
2009-06-15 21:15:40 +08:00
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@hookspec(firstresult=True)
|
2010-11-01 07:27:12 +08:00
|
|
|
def pytest_collection(session):
|
2010-09-26 22:23:43 +08:00
|
|
|
""" perform the collection protocol for the given session. """
|
2010-09-15 16:30:50 +08:00
|
|
|
|
2010-11-07 17:19:58 +08:00
|
|
|
def pytest_collection_modifyitems(session, config, items):
|
2010-11-01 06:28:31 +08:00
|
|
|
""" called after collection has been performed, may filter or re-order
|
|
|
|
the items in-place."""
|
2010-09-15 16:30:50 +08:00
|
|
|
|
2010-11-07 17:19:58 +08:00
|
|
|
def pytest_collection_finish(session):
|
2010-11-01 06:28:31 +08:00
|
|
|
""" called after collection has been performed and modified. """
|
2010-09-15 16:30:50 +08:00
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@hookspec(firstresult=True)
|
2010-05-02 21:24:02 +08:00
|
|
|
def pytest_ignore_collect(path, config):
|
2010-10-11 18:54:28 +08:00
|
|
|
""" return True to prevent considering this path for collection.
|
|
|
|
This hook is consulted for all files and directories prior to calling
|
|
|
|
more specific hooks.
|
2010-04-29 22:20:55 +08:00
|
|
|
"""
|
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@hookspec(firstresult=True)
|
2009-06-15 23:28:55 +08:00
|
|
|
def pytest_collect_directory(path, parent):
|
2010-11-07 17:19:58 +08:00
|
|
|
""" called before traversing a directory for collection files. """
|
2009-06-11 20:48:53 +08:00
|
|
|
|
|
|
|
def pytest_collect_file(path, parent):
|
2010-10-11 18:54:28 +08:00
|
|
|
""" return collection Node or None for the given path. Any new node
|
|
|
|
needs to have the specified ``parent`` as a parent."""
|
2009-06-11 20:48:53 +08:00
|
|
|
|
2010-09-15 16:30:50 +08:00
|
|
|
# logging hooks for collection
|
2009-06-11 20:48:53 +08:00
|
|
|
def pytest_collectstart(collector):
|
|
|
|
""" collector starts collecting. """
|
|
|
|
|
2010-11-01 06:28:31 +08:00
|
|
|
def pytest_itemcollected(item):
|
2010-09-15 16:30:50 +08:00
|
|
|
""" we just collected a test item. """
|
|
|
|
|
2009-08-04 18:00:04 +08:00
|
|
|
def pytest_collectreport(report):
|
2009-06-11 20:48:53 +08:00
|
|
|
""" collector finished collecting. """
|
|
|
|
|
2009-07-15 03:17:13 +08:00
|
|
|
def pytest_deselected(items):
|
|
|
|
""" called for test items deselected by keyword. """
|
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@hookspec(firstresult=True)
|
2009-06-15 21:15:40 +08:00
|
|
|
def pytest_make_collect_report(collector):
|
2010-11-01 06:28:31 +08:00
|
|
|
""" perform ``collector.collect()`` and return a CollectReport. """
|
2009-06-15 21:15:40 +08:00
|
|
|
|
2009-07-15 03:17:13 +08:00
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
# Python test function related hooks
|
|
|
|
# -------------------------------------------------------------------------
|
2009-06-15 21:15:40 +08:00
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@hookspec(firstresult=True)
|
2010-04-29 22:53:29 +08:00
|
|
|
def pytest_pycollect_makemodule(path, parent):
|
2010-07-27 03:15:15 +08:00
|
|
|
""" return a Module collector or None for the given path.
|
|
|
|
This hook will be called for each matching test module path.
|
|
|
|
The pytest_collect_file hook needs to be used if you want to
|
2010-04-29 22:53:29 +08:00
|
|
|
create test modules for files that do not match as a test module.
|
|
|
|
"""
|
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@hookspec(firstresult=True)
|
2009-06-15 21:15:40 +08:00
|
|
|
def pytest_pycollect_makeitem(collector, name, obj):
|
|
|
|
""" return custom item/collector for a python object in a module, or None. """
|
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@hookspec(firstresult=True)
|
2009-06-15 21:15:40 +08:00
|
|
|
def pytest_pyfunc_call(pyfuncitem):
|
2009-12-17 16:33:41 +08:00
|
|
|
""" call underlying test function. """
|
2009-06-15 21:15:40 +08:00
|
|
|
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
|
|
""" generate (multiple) parametrized calls to a test function."""
|
|
|
|
|
2009-07-15 03:17:13 +08:00
|
|
|
# -------------------------------------------------------------------------
|
2010-07-27 03:15:15 +08:00
|
|
|
# generic runtest related hooks
|
2009-07-15 03:17:13 +08:00
|
|
|
# -------------------------------------------------------------------------
|
2015-04-25 17:29:11 +08:00
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@hookspec(firstresult=True)
|
2015-04-25 17:29:11 +08:00
|
|
|
def pytest_runtestloop(session):
|
|
|
|
""" called for performing the main runtest loop
|
|
|
|
(after collection finished). """
|
|
|
|
|
2014-10-01 20:55:54 +08:00
|
|
|
def pytest_itemstart(item, node):
|
2010-09-26 22:23:45 +08:00
|
|
|
""" (deprecated, use pytest_runtest_logstart). """
|
2010-09-15 16:30:50 +08:00
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@hookspec(firstresult=True)
|
2011-12-03 05:00:19 +08:00
|
|
|
def pytest_runtest_protocol(item, nextitem):
|
|
|
|
""" implements the runtest_setup/call/teardown protocol for
|
|
|
|
the given test item, including capturing exceptions and calling
|
|
|
|
reporting hooks.
|
|
|
|
|
|
|
|
:arg item: test item for which the runtest protocol is performed.
|
|
|
|
|
2014-06-03 13:21:02 +08:00
|
|
|
:arg nextitem: the scheduled-to-be-next test item (or None if this
|
2014-06-03 15:30:14 +08:00
|
|
|
is the end my friend). This argument is passed on to
|
|
|
|
:py:func:`pytest_runtest_teardown`.
|
2010-10-11 18:54:28 +08:00
|
|
|
|
|
|
|
:return boolean: True if no further hook implementations should be invoked.
|
|
|
|
"""
|
2009-07-15 03:17:13 +08:00
|
|
|
|
2010-11-06 16:58:04 +08:00
|
|
|
def pytest_runtest_logstart(nodeid, location):
|
2011-12-03 05:00:19 +08:00
|
|
|
""" signal the start of running a single test item. """
|
2010-09-26 22:23:45 +08:00
|
|
|
|
2009-06-11 20:48:53 +08:00
|
|
|
def pytest_runtest_setup(item):
|
2010-10-11 18:54:28 +08:00
|
|
|
""" called before ``pytest_runtest_call(item)``. """
|
2009-06-11 20:48:53 +08:00
|
|
|
|
|
|
|
def pytest_runtest_call(item):
|
2010-10-11 18:54:28 +08:00
|
|
|
""" called to execute the test ``item``. """
|
2009-06-11 20:48:53 +08:00
|
|
|
|
2011-12-03 05:00:19 +08:00
|
|
|
def pytest_runtest_teardown(item, nextitem):
|
|
|
|
""" called after ``pytest_runtest_call``.
|
|
|
|
|
2014-06-03 15:30:14 +08:00
|
|
|
:arg nextitem: the scheduled-to-be-next test item (None if no further
|
|
|
|
test item is scheduled). This argument can be used to
|
|
|
|
perform exact teardowns, i.e. calling just enough finalizers
|
|
|
|
so that nextitem only needs to call setup-functions.
|
2011-12-03 05:00:19 +08:00
|
|
|
"""
|
2009-06-11 20:48:53 +08:00
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@hookspec(firstresult=True)
|
2009-06-11 20:48:53 +08:00
|
|
|
def pytest_runtest_makereport(item, call):
|
2010-11-13 18:10:45 +08:00
|
|
|
""" return a :py:class:`_pytest.runner.TestReport` object
|
2010-11-13 16:05:11 +08:00
|
|
|
for the given :py:class:`pytest.Item` and
|
2010-11-13 18:10:45 +08:00
|
|
|
:py:class:`_pytest.runner.CallInfo`.
|
2010-10-11 18:54:28 +08:00
|
|
|
"""
|
2009-06-11 20:48:53 +08:00
|
|
|
|
2009-08-04 18:00:04 +08:00
|
|
|
def pytest_runtest_logreport(report):
|
2011-11-09 01:53:46 +08:00
|
|
|
""" process a test setup/call/teardown report relating to
|
|
|
|
the respective phase of executing a test. """
|
2009-06-11 20:48:53 +08:00
|
|
|
|
2009-07-15 03:17:13 +08:00
|
|
|
# -------------------------------------------------------------------------
|
2010-07-27 03:15:15 +08:00
|
|
|
# test session related hooks
|
2009-07-15 03:17:13 +08:00
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
def pytest_sessionstart(session):
|
|
|
|
""" before session.main() is called. """
|
|
|
|
|
2009-07-20 20:01:40 +08:00
|
|
|
def pytest_sessionfinish(session, exitstatus):
|
2009-07-15 03:17:13 +08:00
|
|
|
""" whole test run finishes. """
|
|
|
|
|
2015-04-25 17:29:11 +08:00
|
|
|
def pytest_unconfigure(config):
|
|
|
|
""" called before test process is exited. """
|
|
|
|
|
2010-09-26 22:23:43 +08:00
|
|
|
|
2010-09-07 02:35:17 +08:00
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
# hooks for customising the assert methods
|
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
|
2010-10-03 01:00:47 +08:00
|
|
|
def pytest_assertrepr_compare(config, op, left, right):
|
|
|
|
"""return explanation for comparisons in failing assert expressions.
|
|
|
|
|
|
|
|
Return None for no custom explanation, otherwise return a list
|
|
|
|
of strings. The strings will be joined by newlines but any newlines
|
|
|
|
*in* a string will be escaped. Note that all but the first line will
|
|
|
|
be indented sligthly, the intention is for the first line to be a summary.
|
2010-09-07 02:35:17 +08:00
|
|
|
"""
|
|
|
|
|
2009-07-15 03:17:13 +08:00
|
|
|
# -------------------------------------------------------------------------
|
2010-11-13 18:10:45 +08:00
|
|
|
# hooks for influencing reporting (invoked from _pytest_terminal)
|
2009-07-15 03:17:13 +08:00
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
|
2012-06-21 17:07:22 +08:00
|
|
|
def pytest_report_header(config, startdir):
|
2010-01-13 04:43:25 +08:00
|
|
|
""" return a string to be displayed as header info for terminal reporting."""
|
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@hookspec(firstresult=True)
|
2009-08-06 21:02:38 +08:00
|
|
|
def pytest_report_teststatus(report):
|
|
|
|
""" return result-category, shortletter and verbose word for reporting."""
|
2009-06-11 20:48:53 +08:00
|
|
|
|
|
|
|
def pytest_terminal_summary(terminalreporter):
|
2013-09-27 21:48:03 +08:00
|
|
|
""" add additional section in terminal summary reporting. """
|
2009-06-11 20:48:53 +08:00
|
|
|
|
2015-09-03 00:49:49 +08:00
|
|
|
|
|
|
|
@hookspec(historic=True)
|
2014-03-12 05:10:17 +08:00
|
|
|
def pytest_logwarning(message, code, nodeid, fslocation):
|
|
|
|
""" process a warning specified by a message, a code string,
|
|
|
|
a nodeid and fslocation (both of which may be None
|
|
|
|
if the warning is not tied to a partilar node/location)."""
|
|
|
|
|
2009-07-15 03:17:13 +08:00
|
|
|
# -------------------------------------------------------------------------
|
2010-07-27 03:15:15 +08:00
|
|
|
# doctest hooks
|
2009-07-15 03:17:13 +08:00
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
@hookspec(firstresult=True)
|
2009-06-11 20:48:53 +08:00
|
|
|
def pytest_doctest_prepare_content(content):
|
|
|
|
""" return processed content for a given doctest"""
|
|
|
|
|
2009-07-15 03:17:13 +08:00
|
|
|
# -------------------------------------------------------------------------
|
2010-07-27 03:15:15 +08:00
|
|
|
# error handling and internal debugging hooks
|
2009-07-15 03:17:13 +08:00
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
|
2013-09-06 17:56:04 +08:00
|
|
|
def pytest_internalerror(excrepr, excinfo):
|
2009-07-15 03:17:13 +08:00
|
|
|
""" called for internal errors. """
|
|
|
|
|
2009-07-20 20:01:40 +08:00
|
|
|
def pytest_keyboard_interrupt(excinfo):
|
|
|
|
""" called for keyboard interrupt. """
|
2013-09-06 17:56:04 +08:00
|
|
|
|
|
|
|
def pytest_exception_interact(node, call, report):
|
|
|
|
""" (experimental, new in 2.4) called when
|
|
|
|
an exception was raised which can potentially be
|
|
|
|
interactively handled.
|
|
|
|
|
|
|
|
This hook is only called if an exception was raised
|
|
|
|
that is not an internal exception like "skip.Exception".
|
|
|
|
"""
|
|
|
|
|
2015-11-23 23:05:56 +08:00
|
|
|
def pytest_enter_pdb(config):
|
2015-11-23 22:59:56 +08:00
|
|
|
""" called upon pdb.set_trace(), can be used by plugins to take special
|
|
|
|
action just before the python debugger enters in interactive mode.
|
2015-11-23 23:05:56 +08:00
|
|
|
|
|
|
|
:arg config: pytest config object
|
|
|
|
:type config: _pytest.config.Config
|
2015-11-23 22:59:56 +08:00
|
|
|
"""
|