2009-05-19 15:30:11 +08:00
|
|
|
==========================
|
2009-06-09 00:31:10 +08:00
|
|
|
hooks and plugins
|
2009-05-19 15:30:11 +08:00
|
|
|
==========================
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-06-09 22:08:34 +08:00
|
|
|
py.test implements much of its functionality by calling **hooks**.
|
|
|
|
A hook is a function with a ``pytest_`` prefixed name. Hook functions
|
|
|
|
are usually defined in plugins. A plugin is a regular python module or
|
|
|
|
package that makes hook functions available.
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-06-09 00:31:10 +08:00
|
|
|
When loading a plugin module (which needs to have a ``pytest_`` prefix as well)
|
|
|
|
py.test performs strict checking on the function signature. Function
|
|
|
|
and argument names need to match exactly the `original definition of the hook`_.
|
|
|
|
This allows for early mismatch reporting and minimizes version incompatibilites.
|
2009-03-26 19:15:14 +08:00
|
|
|
|
2009-06-09 22:08:34 +08:00
|
|
|
.. _`original definition of the hook`: http://bitbucket.org/hpk42/py-trunk/src/tip/py/test/plugin/api.py
|
2009-03-26 19:15:14 +08:00
|
|
|
|
2009-03-22 08:38:43 +08:00
|
|
|
Loading plugins and specifying dependencies
|
2009-04-02 15:10:16 +08:00
|
|
|
============================================
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-03-22 08:38:43 +08:00
|
|
|
py.test loads and configures plugins at tool startup:
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-03-22 08:38:43 +08:00
|
|
|
* by reading the ``PYTEST_PLUGINS`` environment variable
|
|
|
|
and importing the comma-separated list of plugin names.
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-05-19 15:30:11 +08:00
|
|
|
* by pre-scanning the command line for the ``-p name`` option
|
|
|
|
and loading the specified plugin *before actual command line parsing*.
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-06-09 00:31:10 +08:00
|
|
|
* by loading all plugins specified by the ``pytest_plugins``
|
|
|
|
variable in a ``conftest.py`` file or test modules.
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-05-19 00:59:29 +08:00
|
|
|
Specifying a plugin in a test module or ``conftest.py`` will
|
|
|
|
only lead to activitation when ``py.test`` actually sees the
|
2009-06-09 00:31:10 +08:00
|
|
|
directory and the file during the collection process. This happens
|
2009-05-19 00:59:29 +08:00
|
|
|
already after command line parsing and there is no try to do
|
|
|
|
a "pre-scan of all subdirs" as this would mean a potentially
|
2009-05-19 15:30:11 +08:00
|
|
|
very large delay. As long as you don't add command line
|
|
|
|
options this detail does not need to worry you.
|
2009-05-19 00:59:29 +08:00
|
|
|
|
2009-06-09 00:31:10 +08:00
|
|
|
A plugin module may specify its dependencies via
|
|
|
|
another ``pytest_plugins`` definition.
|
|
|
|
|
|
|
|
Included plugins
|
|
|
|
================
|
|
|
|
|
|
|
|
You can find the source code of all default plugins in
|
2009-06-09 22:08:34 +08:00
|
|
|
|
|
|
|
http://bitbucket.org/hpk42/py-trunk/src/tip/py/test/plugin/
|
|
|
|
|
|
|
|
Additionally you can check out some more contributed plugins here
|
|
|
|
|
|
|
|
http://bitbucket.org/hpk42/py-trunk/src/tip/contrib/
|
|
|
|
|
2009-06-09 00:31:10 +08:00
|
|
|
|
|
|
|
Overview on available hooks
|
|
|
|
====================================
|
|
|
|
|
|
|
|
"runtest" hooks
|
|
|
|
-------------------
|
2009-05-19 00:59:29 +08:00
|
|
|
|
2009-06-09 22:08:34 +08:00
|
|
|
Each test item is usually executed by calling the following three hooks::
|
|
|
|
|
|
|
|
pytest_runtest_setup(item)
|
|
|
|
pytest_runtest_call(item)
|
|
|
|
pytest_runtest_teardown(item)
|
|
|
|
|
|
|
|
For each of the three invocations a `call object`_ encapsulates
|
|
|
|
information about the outcome of the call and is subsequently used
|
|
|
|
to make a report object::
|
|
|
|
|
|
|
|
report = hook.pytest_runtest_makereport(item, call)
|
|
|
|
|
|
|
|
For example, the `pytest_pdb plugin`_ uses this hook to activate
|
|
|
|
interactive debugging on failures when ``--pdb`` is specified on the
|
|
|
|
command line.
|
|
|
|
|
|
|
|
Usually three reports will be generated for a single test item. However,
|
|
|
|
if the ``pytest_runtest_setup`` fails no call or teardown hooks
|
|
|
|
will be called and only one report will be created.
|
|
|
|
|
|
|
|
Each of the up to three reports is eventually fed to the logreport hook::
|
|
|
|
|
|
|
|
pytest_runtest_logreport(report)
|
|
|
|
|
|
|
|
A ``report`` object contains status and reporting information::
|
|
|
|
|
|
|
|
report.longrepr = string/lines/object to print
|
|
|
|
report.when = "setup", "call" or "teardown"
|
|
|
|
report.shortrepr = letter for progress-report
|
|
|
|
report.passed = True or False
|
|
|
|
report.failed = True or False
|
|
|
|
report.skipped = True or False
|
|
|
|
|
|
|
|
The `pytest_terminal plugin`_ uses this hook to print information
|
|
|
|
about a test run.
|
|
|
|
|
|
|
|
The protocol described here is implemented via this hook::
|
|
|
|
|
|
|
|
pytest_runtest_protocol(item) -> True
|
|
|
|
|
|
|
|
.. _`call object`:
|
|
|
|
|
|
|
|
The call object contains information about a performed call::
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-06-09 22:08:34 +08:00
|
|
|
call.excinfo = ExceptionInfo object or None
|
|
|
|
call.when = "setup", "call" or "teardown"
|
|
|
|
call.outerr = None or tuple of strings representing captured stdout/stderr
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2009-06-09 22:08:34 +08:00
|
|
|
.. _`pytest_pdb plugin`: http://bitbucket.org/hpk42/py-trunk/src/tip/py/test/plugin/pytest_pdb.py
|
|
|
|
.. _`pytest_terminal plugin`: http://bitbucket.org/hpk42/py-trunk/src/tip/py/test/plugin/pytest_terminal.py
|
2009-02-27 18:18:27 +08:00
|
|
|
|