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
|
|
|
|
2009-07-15 03:17:13 +08:00
|
|
|
# -------------------------------------------------------------------------
|
2010-10-11 06:49:54 +08:00
|
|
|
# Initialization
|
2009-07-15 03:17:13 +08:00
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
|
2010-11-01 06:28:31 +08:00
|
|
|
def pytest_addhooks(pluginmanager):
|
|
|
|
"""called at plugin load time to allow adding new hooks via a call to
|
|
|
|
pluginmanager.registerhooks(module)."""
|
|
|
|
|
|
|
|
|
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
|
|
|
|
the py.test/pytest namespace. This hook is called before command
|
2010-11-01 06:28:31 +08:00
|
|
|
line options are parsed.
|
2010-10-11 06:49:54 +08:00
|
|
|
"""
|
2009-06-11 20:48:53 +08:00
|
|
|
|
2010-10-12 21:34:32 +08:00
|
|
|
def pytest_cmdline_parse(pluginmanager, args):
|
|
|
|
"""return initialized config object, parsing the specified args. """
|
|
|
|
pytest_cmdline_parse.firstresult = True
|
|
|
|
|
2010-12-07 19:34:18 +08:00
|
|
|
def pytest_cmdline_preparse(config, args):
|
2010-12-07 19:14:12 +08:00
|
|
|
"""modify command line arguments before option parsing. """
|
|
|
|
|
2010-05-02 22:36:53 +08:00
|
|
|
def pytest_addoption(parser):
|
2010-11-05 06:21:23 +08:00
|
|
|
"""add optparse-style options and ini-style config values via calls
|
2010-11-01 06:28:31 +08:00
|
|
|
to ``parser.addoption`` and ``parser.addini(...)``.
|
|
|
|
"""
|
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. """
|
|
|
|
pytest_cmdline_main.firstresult = True
|
2009-07-18 00:07:37 +08:00
|
|
|
|
2009-06-11 20:48:53 +08:00
|
|
|
def pytest_configure(config):
|
2010-07-27 03:15:15 +08:00
|
|
|
""" called after command line options have been parsed.
|
|
|
|
and all plugins and initial conftest files been loaded.
|
2009-06-11 20:48:53 +08:00
|
|
|
"""
|
|
|
|
|
2010-10-11 06:49:54 +08:00
|
|
|
def pytest_unconfigure(config):
|
|
|
|
""" called before test process is exited. """
|
2010-09-15 16:30:50 +08:00
|
|
|
|
2010-11-01 07:27:12 +08:00
|
|
|
def pytest_runtestloop(session):
|
2010-11-07 17:19:58 +08:00
|
|
|
""" called for performing the main runtest loop
|
|
|
|
(after collection finished). """
|
2010-11-01 07:27:12 +08:00
|
|
|
pytest_runtestloop.firstresult = True
|
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
|
|
|
|
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-11-01 07:27:12 +08:00
|
|
|
pytest_collection.firstresult = True
|
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
|
|
|
|
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
|
|
|
"""
|
2010-05-02 21:24:02 +08:00
|
|
|
pytest_ignore_collect.firstresult = True
|
2010-04-29 22:20:55 +08:00
|
|
|
|
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-12-30 23:18:59 +08:00
|
|
|
pytest_collect_directory.firstresult = True
|
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. """
|
|
|
|
|
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
|
|
|
pytest_make_collect_report.firstresult = True
|
|
|
|
|
2009-07-15 03:17:13 +08:00
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
# Python test function related hooks
|
|
|
|
# -------------------------------------------------------------------------
|
2009-06-15 21:15:40 +08:00
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
pytest_pycollect_makemodule.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. """
|
|
|
|
pytest_pycollect_makeitem.firstresult = True
|
|
|
|
|
|
|
|
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
|
|
|
pytest_pyfunc_call.firstresult = True
|
|
|
|
|
|
|
|
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
|
|
|
# -------------------------------------------------------------------------
|
2010-09-15 16:30:50 +08:00
|
|
|
def pytest_itemstart(item, node=None):
|
2010-09-26 22:23:45 +08:00
|
|
|
""" (deprecated, use pytest_runtest_logstart). """
|
2010-09-15 16:30:50 +08:00
|
|
|
|
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.
|
|
|
|
|
|
|
|
:arg nexitem: the scheduled-to-be-next test item (or None if this
|
|
|
|
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
|
|
|
pytest_runtest_protocol.firstresult = True
|
|
|
|
|
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``.
|
|
|
|
|
|
|
|
:arg nexitem: 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.
|
|
|
|
"""
|
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
|
|
|
pytest_runtest_makereport.firstresult = True
|
|
|
|
|
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. """
|
|
|
|
|
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
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
|
2010-01-13 04:43:25 +08:00
|
|
|
def pytest_report_header(config):
|
|
|
|
""" return a string to be displayed as header info for terminal reporting."""
|
|
|
|
|
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
|
|
|
pytest_report_teststatus.firstresult = True
|
|
|
|
|
|
|
|
def pytest_terminal_summary(terminalreporter):
|
|
|
|
""" add additional section in terminal summary reporting. """
|
|
|
|
|
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
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
|
2009-06-11 20:48:53 +08:00
|
|
|
def pytest_doctest_prepare_content(content):
|
|
|
|
""" return processed content for a given doctest"""
|
|
|
|
pytest_doctest_prepare_content.firstresult = True
|
|
|
|
|
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
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
|
2009-12-29 17:59:01 +08:00
|
|
|
def pytest_plugin_registered(plugin, manager):
|
2009-07-15 03:17:13 +08:00
|
|
|
""" a new py lib plugin got registered. """
|
|
|
|
|
|
|
|
def pytest_plugin_unregistered(plugin):
|
|
|
|
""" a py lib plugin got unregistered. """
|
|
|
|
|
|
|
|
def pytest_internalerror(excrepr):
|
|
|
|
""" called for internal errors. """
|
|
|
|
|
2009-07-20 20:01:40 +08:00
|
|
|
def pytest_keyboard_interrupt(excinfo):
|
|
|
|
""" called for keyboard interrupt. """
|