2015-04-25 17:29:11 +08:00
|
|
|
|
.. _plugins:
|
|
|
|
|
.. _`writing-plugins`:
|
|
|
|
|
|
|
|
|
|
Writing plugins
|
|
|
|
|
===============
|
|
|
|
|
|
|
|
|
|
It is easy to implement `local conftest plugins`_ for your own project
|
|
|
|
|
or `pip-installable plugins`_ that can be used throughout many projects,
|
|
|
|
|
including third party projects. Please refer to :ref:`using plugins` if you
|
|
|
|
|
only want to use but not write plugins.
|
|
|
|
|
|
|
|
|
|
A plugin contains one or multiple hook functions. :ref:`Writing hooks <writinghooks>`
|
|
|
|
|
explains the basics and details of how you can write a hook function yourself.
|
|
|
|
|
``pytest`` implements all aspects of configuration, collection, running and
|
2018-02-08 06:53:31 +08:00
|
|
|
|
reporting by calling :ref:`well specified hooks <hook-reference>` of the following plugins:
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
2018-02-08 06:53:31 +08:00
|
|
|
|
* builtin plugins: loaded from pytest's internal ``_pytest`` directory.
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
2015-12-11 05:00:01 +08:00
|
|
|
|
* :ref:`external plugins <extplugins>`: modules discovered through
|
2015-04-25 17:29:11 +08:00
|
|
|
|
`setuptools entry points`_
|
|
|
|
|
|
|
|
|
|
* `conftest.py plugins`_: modules auto-discovered in test directories
|
|
|
|
|
|
|
|
|
|
In principle, each hook call is a ``1:N`` Python function call where ``N`` is the
|
|
|
|
|
number of registered implementation functions for a given specification.
|
2016-03-21 00:01:04 +08:00
|
|
|
|
All specifications and implementations follow the ``pytest_`` prefix
|
2015-04-25 17:29:11 +08:00
|
|
|
|
naming convention, making them easy to distinguish and find.
|
|
|
|
|
|
|
|
|
|
.. _`pluginorder`:
|
|
|
|
|
|
|
|
|
|
Plugin discovery order at tool startup
|
|
|
|
|
--------------------------------------
|
|
|
|
|
|
|
|
|
|
``pytest`` loads plugin modules at tool startup in the following way:
|
|
|
|
|
|
|
|
|
|
* by loading all builtin plugins
|
|
|
|
|
|
|
|
|
|
* by loading all plugins registered through `setuptools entry points`_.
|
|
|
|
|
|
|
|
|
|
* by pre-scanning the command line for the ``-p name`` option
|
|
|
|
|
and loading the specified plugin before actual command line parsing.
|
|
|
|
|
|
|
|
|
|
* by loading all :file:`conftest.py` files as inferred by the command line
|
|
|
|
|
invocation:
|
|
|
|
|
|
|
|
|
|
- if no test paths are specified use current dir as a test path
|
|
|
|
|
- if exists, load ``conftest.py`` and ``test*/conftest.py`` relative
|
|
|
|
|
to the directory part of the first test path.
|
|
|
|
|
|
|
|
|
|
Note that pytest does not find ``conftest.py`` files in deeper nested
|
|
|
|
|
sub directories at tool startup. It is usually a good idea to keep
|
2017-07-20 08:11:17 +08:00
|
|
|
|
your ``conftest.py`` file in the top level test or project root directory.
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
|
|
|
|
* by recursively loading all plugins specified by the
|
|
|
|
|
``pytest_plugins`` variable in ``conftest.py`` files
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. _`pytest/plugin`: http://bitbucket.org/pytest-dev/pytest/src/tip/pytest/plugin/
|
|
|
|
|
.. _`conftest.py plugins`:
|
|
|
|
|
.. _`localplugin`:
|
|
|
|
|
.. _`local conftest plugins`:
|
|
|
|
|
|
|
|
|
|
conftest.py: local per-directory plugins
|
|
|
|
|
----------------------------------------
|
|
|
|
|
|
|
|
|
|
Local ``conftest.py`` plugins contain directory-specific hook
|
|
|
|
|
implementations. Hook Session and test running activities will
|
|
|
|
|
invoke all hooks defined in ``conftest.py`` files closer to the
|
|
|
|
|
root of the filesystem. Example of implementing the
|
|
|
|
|
``pytest_runtest_setup`` hook so that is called for tests in the ``a``
|
|
|
|
|
sub directory but not for other directories::
|
|
|
|
|
|
|
|
|
|
a/conftest.py:
|
|
|
|
|
def pytest_runtest_setup(item):
|
|
|
|
|
# called for running each test in 'a' directory
|
2018-11-22 16:15:14 +08:00
|
|
|
|
print("setting up", item)
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
|
|
|
|
a/test_sub.py:
|
|
|
|
|
def test_sub():
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
test_flat.py:
|
|
|
|
|
def test_flat():
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
Here is how you might run it::
|
|
|
|
|
|
2018-04-06 02:34:26 +08:00
|
|
|
|
pytest test_flat.py --capture=no # will not show "setting up"
|
|
|
|
|
pytest a/test_sub.py --capture=no # will show "setting up"
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
2017-07-28 07:02:18 +08:00
|
|
|
|
.. note::
|
2015-04-25 17:29:11 +08:00
|
|
|
|
If you have ``conftest.py`` files which do not reside in a
|
|
|
|
|
python package directory (i.e. one containing an ``__init__.py``) then
|
|
|
|
|
"import conftest" can be ambiguous because there might be other
|
2017-07-20 08:11:17 +08:00
|
|
|
|
``conftest.py`` files as well on your ``PYTHONPATH`` or ``sys.path``.
|
2016-01-20 23:35:27 +08:00
|
|
|
|
It is thus good practice for projects to either put ``conftest.py``
|
2015-04-25 17:29:11 +08:00
|
|
|
|
under a package scope or to never import anything from a
|
2017-07-20 08:11:17 +08:00
|
|
|
|
``conftest.py`` file.
|
|
|
|
|
|
|
|
|
|
See also: :ref:`pythonpath`.
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
|
|
|
|
|
2015-08-26 10:54:05 +08:00
|
|
|
|
Writing your own plugin
|
|
|
|
|
-----------------------
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
2018-04-26 21:45:48 +08:00
|
|
|
|
.. _`setuptools`: https://pypi.org/project/setuptools/
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
|
|
|
|
If you want to write a plugin, there are many real-life examples
|
|
|
|
|
you can copy from:
|
|
|
|
|
|
|
|
|
|
* a custom collection example plugin: :ref:`yaml plugin`
|
2018-02-08 06:53:31 +08:00
|
|
|
|
* builtin plugins which provide pytest's own functionality
|
2015-12-11 05:00:01 +08:00
|
|
|
|
* many `external plugins <http://plugincompat.herokuapp.com>`_ providing additional features
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
2018-02-08 06:53:31 +08:00
|
|
|
|
All of these plugins implement :ref:`hooks <hook-reference>` and/or :ref:`fixtures <fixture>`
|
2015-04-25 17:29:11 +08:00
|
|
|
|
to extend and add functionality.
|
|
|
|
|
|
2015-08-26 10:54:05 +08:00
|
|
|
|
.. note::
|
|
|
|
|
Make sure to check out the excellent
|
|
|
|
|
`cookiecutter-pytest-plugin <https://github.com/pytest-dev/cookiecutter-pytest-plugin>`_
|
|
|
|
|
project, which is a `cookiecutter template <https://github.com/audreyr/cookiecutter>`_
|
|
|
|
|
for authoring plugins.
|
|
|
|
|
|
|
|
|
|
The template provides an excellent starting point with a working plugin,
|
2017-07-15 17:45:28 +08:00
|
|
|
|
tests running with tox, a comprehensive README file as well as a
|
|
|
|
|
pre-configured entry-point.
|
2015-08-26 10:54:05 +08:00
|
|
|
|
|
|
|
|
|
Also consider :ref:`contributing your plugin to pytest-dev<submitplugin>`
|
2015-04-25 17:29:11 +08:00
|
|
|
|
once it has some happy users other than yourself.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. _`setuptools entry points`:
|
|
|
|
|
.. _`pip-installable plugins`:
|
|
|
|
|
|
|
|
|
|
Making your plugin installable by others
|
|
|
|
|
----------------------------------------
|
|
|
|
|
|
|
|
|
|
If you want to make your plugin externally available, you
|
|
|
|
|
may define a so-called entry point for your distribution so
|
|
|
|
|
that ``pytest`` finds your plugin module. Entry points are
|
|
|
|
|
a feature that is provided by `setuptools`_. pytest looks up
|
|
|
|
|
the ``pytest11`` entrypoint to discover its
|
|
|
|
|
plugins and you can thus make your plugin available by defining
|
|
|
|
|
it in your setuptools-invocation:
|
|
|
|
|
|
|
|
|
|
.. sourcecode:: python
|
|
|
|
|
|
|
|
|
|
# sample ./setup.py file
|
|
|
|
|
from setuptools import setup
|
|
|
|
|
|
|
|
|
|
setup(
|
|
|
|
|
name="myproject",
|
2018-06-03 11:29:28 +08:00
|
|
|
|
packages=["myproject"],
|
2015-04-25 17:29:11 +08:00
|
|
|
|
# the following makes a plugin available to pytest
|
2018-06-03 11:29:28 +08:00
|
|
|
|
entry_points={"pytest11": ["name_of_plugin = myproject.pluginmodule"]},
|
2016-03-23 07:42:52 +08:00
|
|
|
|
# custom PyPI classifier for pytest plugins
|
2018-06-03 11:29:28 +08:00
|
|
|
|
classifiers=["Framework :: Pytest"],
|
2015-04-25 17:29:11 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
If a package is installed this way, ``pytest`` will load
|
|
|
|
|
``myproject.pluginmodule`` as a plugin which can define
|
2018-02-08 06:53:31 +08:00
|
|
|
|
:ref:`hooks <hook-reference>`.
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
2016-03-23 07:42:52 +08:00
|
|
|
|
.. note::
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
2016-03-23 07:42:52 +08:00
|
|
|
|
Make sure to include ``Framework :: Pytest`` in your list of
|
2017-01-03 06:07:41 +08:00
|
|
|
|
`PyPI classifiers <https://python-packaging-user-guide.readthedocs.io/distributing/#classifiers>`_
|
2016-03-23 07:42:52 +08:00
|
|
|
|
to make it easy for users to find your plugin.
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
|
|
|
|
|
2018-03-02 04:54:54 +08:00
|
|
|
|
.. _assertion-rewriting:
|
|
|
|
|
|
2016-07-17 19:30:21 +08:00
|
|
|
|
Assertion Rewriting
|
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
|
|
One of the main features of ``pytest`` is the use of plain assert
|
|
|
|
|
statements and the detailed introspection of expressions upon
|
|
|
|
|
assertion failures. This is provided by "assertion rewriting" which
|
|
|
|
|
modifies the parsed AST before it gets compiled to bytecode. This is
|
|
|
|
|
done via a :pep:`302` import hook which gets installed early on when
|
2017-11-27 03:46:06 +08:00
|
|
|
|
``pytest`` starts up and will perform this rewriting when modules get
|
2016-07-17 19:30:21 +08:00
|
|
|
|
imported. However since we do not want to test different bytecode
|
2017-11-27 03:46:06 +08:00
|
|
|
|
then you will run in production this hook only rewrites test modules
|
2016-07-17 19:30:21 +08:00
|
|
|
|
themselves as well as any modules which are part of plugins. Any
|
2017-11-27 03:46:06 +08:00
|
|
|
|
other imported module will not be rewritten and normal assertion
|
2016-07-17 19:30:21 +08:00
|
|
|
|
behaviour will happen.
|
|
|
|
|
|
|
|
|
|
If you have assertion helpers in other modules where you would need
|
|
|
|
|
assertion rewriting to be enabled you need to ask ``pytest``
|
2017-11-27 03:46:06 +08:00
|
|
|
|
explicitly to rewrite this module before it gets imported.
|
2016-07-17 19:30:21 +08:00
|
|
|
|
|
|
|
|
|
.. autofunction:: pytest.register_assert_rewrite
|
2018-03-02 04:54:54 +08:00
|
|
|
|
:noindex:
|
2016-07-17 19:30:21 +08:00
|
|
|
|
|
|
|
|
|
This is especially important when you write a pytest plugin which is
|
|
|
|
|
created using a package. The import hook only treats ``conftest.py``
|
|
|
|
|
files and any modules which are listed in the ``pytest11`` entrypoint
|
|
|
|
|
as plugins. As an example consider the following package::
|
|
|
|
|
|
|
|
|
|
pytest_foo/__init__.py
|
|
|
|
|
pytest_foo/plugin.py
|
|
|
|
|
pytest_foo/helper.py
|
|
|
|
|
|
|
|
|
|
With the following typical ``setup.py`` extract:
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
2018-06-03 11:29:28 +08:00
|
|
|
|
setup(..., entry_points={"pytest11": ["foo = pytest_foo.plugin"]}, ...)
|
2016-07-17 19:30:21 +08:00
|
|
|
|
|
2017-11-27 03:46:06 +08:00
|
|
|
|
In this case only ``pytest_foo/plugin.py`` will be rewritten. If the
|
2016-07-17 19:30:21 +08:00
|
|
|
|
helper module also contains assert statements which need to be
|
2017-11-27 03:46:06 +08:00
|
|
|
|
rewritten it needs to be marked as such, before it gets imported.
|
|
|
|
|
This is easiest by marking it for rewriting inside the
|
2016-07-17 19:30:21 +08:00
|
|
|
|
``__init__.py`` module, which will always be imported first when a
|
|
|
|
|
module inside a package is imported. This way ``plugin.py`` can still
|
|
|
|
|
import ``helper.py`` normally. The contents of
|
|
|
|
|
``pytest_foo/__init__.py`` will then need to look like this:
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
2018-06-03 11:29:28 +08:00
|
|
|
|
pytest.register_assert_rewrite("pytest_foo.helper")
|
2016-07-17 19:30:21 +08:00
|
|
|
|
|
|
|
|
|
|
2015-04-25 17:29:11 +08:00
|
|
|
|
Requiring/Loading plugins in a test module or conftest file
|
|
|
|
|
-----------------------------------------------------------
|
|
|
|
|
|
2017-02-15 21:57:03 +08:00
|
|
|
|
You can require plugins in a test module or a ``conftest.py`` file like this:
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
2017-02-15 21:57:03 +08:00
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
pytest_plugins = ["name1", "name2"]
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
|
|
|
|
When the test module or conftest plugin is loaded the specified plugins
|
2017-02-15 21:57:03 +08:00
|
|
|
|
will be loaded as well. Any module can be blessed as a plugin, including internal
|
|
|
|
|
application modules:
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
|
|
|
|
pytest_plugins = "myapp.testsupport.myplugin"
|
|
|
|
|
|
2017-02-15 21:57:03 +08:00
|
|
|
|
``pytest_plugins`` variables are processed recursively, so note that in the example above
|
|
|
|
|
if ``myapp.testsupport.myplugin`` also declares ``pytest_plugins``, the contents
|
|
|
|
|
of the variable will also be loaded as plugins, and so on.
|
|
|
|
|
|
2018-03-13 05:32:51 +08:00
|
|
|
|
.. _`requiring plugins in non-root conftests`:
|
2018-02-21 07:27:24 +08:00
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
Requiring plugins using a ``pytest_plugins`` variable in non-root
|
|
|
|
|
``conftest.py`` files is deprecated.
|
|
|
|
|
|
|
|
|
|
This is important because ``conftest.py`` files implement per-directory
|
|
|
|
|
hook implementations, but once a plugin is imported, it will affect the
|
|
|
|
|
entire directory tree. In order to avoid confusion, defining
|
|
|
|
|
``pytest_plugins`` in any ``conftest.py`` file which is not located in the
|
|
|
|
|
tests root directory is deprecated, and will raise a warning.
|
|
|
|
|
|
2017-02-15 23:15:53 +08:00
|
|
|
|
This mechanism makes it easy to share fixtures within applications or even
|
2017-06-13 10:45:35 +08:00
|
|
|
|
external applications without the need to create external plugins using
|
2017-02-15 21:57:03 +08:00
|
|
|
|
the ``setuptools``'s entry point technique.
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
2017-02-15 21:57:03 +08:00
|
|
|
|
Plugins imported by ``pytest_plugins`` will also automatically be marked
|
2017-06-13 10:45:35 +08:00
|
|
|
|
for assertion rewriting (see :func:`pytest.register_assert_rewrite`).
|
2017-02-15 21:57:03 +08:00
|
|
|
|
However for this to have any effect the module must not be
|
2017-01-01 01:54:47 +08:00
|
|
|
|
imported already; if it was already imported at the time the
|
|
|
|
|
``pytest_plugins`` statement is processed, a warning will result and
|
2017-11-27 03:46:06 +08:00
|
|
|
|
assertions inside the plugin will not be rewritten. To fix this you
|
2016-07-17 19:30:21 +08:00
|
|
|
|
can either call :func:`pytest.register_assert_rewrite` yourself before
|
|
|
|
|
the module is imported, or you can arrange the code to delay the
|
|
|
|
|
importing until after the plugin is registered.
|
|
|
|
|
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
|
|
|
|
Accessing another plugin by name
|
|
|
|
|
--------------------------------
|
|
|
|
|
|
|
|
|
|
If a plugin wants to collaborate with code from
|
|
|
|
|
another plugin it can obtain a reference through
|
|
|
|
|
the plugin manager like this:
|
|
|
|
|
|
|
|
|
|
.. sourcecode:: python
|
|
|
|
|
|
2018-02-23 02:48:59 +08:00
|
|
|
|
plugin = config.pluginmanager.get_plugin("name_of_plugin")
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
|
|
|
|
If you want to look at the names of existing plugins, use
|
2016-03-21 00:12:50 +08:00
|
|
|
|
the ``--trace-config`` option.
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
2019-03-31 11:22:30 +08:00
|
|
|
|
|
|
|
|
|
.. _registering-markers:
|
|
|
|
|
|
|
|
|
|
Registering custom markers
|
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
|
|
If your plugin uses any markers, you should register them so that they appear in
|
|
|
|
|
pytest's help text and do not :ref:`cause spurious warnings <unknown-marks>`.
|
|
|
|
|
For example, the following plugin would register ``cool_marker`` and
|
|
|
|
|
``mark_with`` for all users:
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
def pytest_configure(config):
|
|
|
|
|
config.addinivalue_line("markers", "cool_marker: this one is for cool tests.")
|
|
|
|
|
config.addinivalue_line(
|
|
|
|
|
"markers", "mark_with(arg, arg2): this marker takes arguments."
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2015-04-28 17:54:53 +08:00
|
|
|
|
Testing plugins
|
|
|
|
|
---------------
|
|
|
|
|
|
2017-07-15 22:48:02 +08:00
|
|
|
|
pytest comes with a plugin named ``pytester`` that helps you write tests for
|
|
|
|
|
your plugin code. The plugin is disabled by default, so you will have to enable
|
|
|
|
|
it before you can use it.
|
2015-04-28 17:54:53 +08:00
|
|
|
|
|
2017-07-15 22:48:02 +08:00
|
|
|
|
You can do so by adding the following line to a ``conftest.py`` file in your
|
|
|
|
|
testing directory:
|
2015-04-28 17:54:53 +08:00
|
|
|
|
|
2017-07-15 22:48:02 +08:00
|
|
|
|
.. code-block:: python
|
2015-04-28 17:54:53 +08:00
|
|
|
|
|
2017-07-15 22:48:02 +08:00
|
|
|
|
# content of conftest.py
|
|
|
|
|
|
|
|
|
|
pytest_plugins = ["pytester"]
|
|
|
|
|
|
|
|
|
|
Alternatively you can invoke pytest with the ``-p pytester`` command line
|
|
|
|
|
option.
|
|
|
|
|
|
|
|
|
|
This will allow you to use the :py:class:`testdir <_pytest.pytester.Testdir>`
|
|
|
|
|
fixture for testing your plugin code.
|
|
|
|
|
|
|
|
|
|
Let's demonstrate what you can do with the plugin with an example. Imagine we
|
|
|
|
|
developed a plugin that provides a fixture ``hello`` which yields a function
|
|
|
|
|
and we can invoke this function with one optional parameter. It will return a
|
|
|
|
|
string value of ``Hello World!`` if we do not supply a value or ``Hello
|
|
|
|
|
{value}!`` if we do supply a string value.
|
|
|
|
|
|
2017-07-20 02:14:46 +08:00
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
2018-06-03 11:29:28 +08:00
|
|
|
|
|
2017-07-20 02:14:46 +08:00
|
|
|
|
def pytest_addoption(parser):
|
2018-06-03 11:29:28 +08:00
|
|
|
|
group = parser.getgroup("helloworld")
|
2017-07-20 02:14:46 +08:00
|
|
|
|
group.addoption(
|
2018-06-03 11:29:28 +08:00
|
|
|
|
"--name",
|
|
|
|
|
action="store",
|
|
|
|
|
dest="name",
|
|
|
|
|
default="World",
|
|
|
|
|
help='Default "name" for hello().',
|
2017-07-20 02:14:46 +08:00
|
|
|
|
)
|
|
|
|
|
|
2018-06-03 11:29:28 +08:00
|
|
|
|
|
2017-07-20 02:14:46 +08:00
|
|
|
|
@pytest.fixture
|
|
|
|
|
def hello(request):
|
2018-06-03 11:29:28 +08:00
|
|
|
|
name = request.config.getoption("name")
|
2017-07-20 02:14:46 +08:00
|
|
|
|
|
|
|
|
|
def _hello(name=None):
|
|
|
|
|
if not name:
|
2018-06-03 11:29:28 +08:00
|
|
|
|
name = request.config.getoption("name")
|
2017-07-20 02:14:46 +08:00
|
|
|
|
return "Hello {name}!".format(name=name)
|
|
|
|
|
|
|
|
|
|
return _hello
|
|
|
|
|
|
|
|
|
|
|
2017-07-15 22:48:02 +08:00
|
|
|
|
Now the ``testdir`` fixture provides a convenient API for creating temporary
|
|
|
|
|
``conftest.py`` files and test files. It also allows us to run the tests and
|
|
|
|
|
return a result object, with which we can assert the tests' outcomes.
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
def test_hello(testdir):
|
|
|
|
|
"""Make sure that our plugin works."""
|
|
|
|
|
|
|
|
|
|
# create a temporary conftest.py file
|
2018-06-03 11:29:28 +08:00
|
|
|
|
testdir.makeconftest(
|
|
|
|
|
"""
|
2017-07-15 22:48:02 +08:00
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(params=[
|
|
|
|
|
"Brianna",
|
|
|
|
|
"Andreas",
|
|
|
|
|
"Floris",
|
|
|
|
|
])
|
|
|
|
|
def name(request):
|
|
|
|
|
return request.param
|
2018-06-03 11:29:28 +08:00
|
|
|
|
"""
|
|
|
|
|
)
|
2017-07-15 22:48:02 +08:00
|
|
|
|
|
|
|
|
|
# create a temporary pytest test file
|
2018-06-03 11:29:28 +08:00
|
|
|
|
testdir.makepyfile(
|
|
|
|
|
"""
|
2017-07-15 22:48:02 +08:00
|
|
|
|
def test_hello_default(hello):
|
|
|
|
|
assert hello() == "Hello World!"
|
|
|
|
|
|
|
|
|
|
def test_hello_name(hello, name):
|
|
|
|
|
assert hello(name) == "Hello {0}!".format(name)
|
2018-06-03 11:29:28 +08:00
|
|
|
|
"""
|
|
|
|
|
)
|
2015-04-28 17:54:53 +08:00
|
|
|
|
|
2017-07-15 22:48:02 +08:00
|
|
|
|
# run all tests with pytest
|
|
|
|
|
result = testdir.runpytest()
|
|
|
|
|
|
|
|
|
|
# check that all 4 tests passed
|
|
|
|
|
result.assert_outcomes(passed=4)
|
|
|
|
|
|
|
|
|
|
|
2018-10-30 05:45:45 +08:00
|
|
|
|
additionally it is possible to copy examples for an example folder before running pytest on it
|
2018-06-27 12:52:54 +08:00
|
|
|
|
|
2018-11-24 13:41:22 +08:00
|
|
|
|
.. code-block:: ini
|
2018-06-27 12:52:54 +08:00
|
|
|
|
|
2018-06-27 14:28:21 +08:00
|
|
|
|
# content of pytest.ini
|
2018-06-27 12:52:54 +08:00
|
|
|
|
[pytest]
|
|
|
|
|
pytester_example_dir = .
|
|
|
|
|
|
|
|
|
|
|
2018-11-24 13:41:22 +08:00
|
|
|
|
.. code-block:: python
|
2018-06-27 12:52:54 +08:00
|
|
|
|
|
2018-06-27 14:28:21 +08:00
|
|
|
|
# content of test_example.py
|
2018-06-27 12:52:54 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_plugin(testdir):
|
2018-11-24 13:41:22 +08:00
|
|
|
|
testdir.copy_example("test_example.py")
|
|
|
|
|
testdir.runpytest("-k", "test_example")
|
|
|
|
|
|
2018-06-27 12:52:54 +08:00
|
|
|
|
|
|
|
|
|
def test_example():
|
2018-11-24 13:41:22 +08:00
|
|
|
|
pass
|
2018-06-27 12:52:54 +08:00
|
|
|
|
|
2018-11-24 13:41:22 +08:00
|
|
|
|
.. code-block:: pytest
|
2018-06-27 12:52:54 +08:00
|
|
|
|
|
2018-06-27 14:28:21 +08:00
|
|
|
|
$ pytest
|
2018-06-27 12:52:54 +08:00
|
|
|
|
=========================== test session starts ============================
|
2019-07-05 08:01:16 +08:00
|
|
|
|
platform linux -- Python 3.x.y, pytest-5.x.y, py-1.x.y, pluggy-0.x.y
|
2019-01-31 00:25:38 +08:00
|
|
|
|
cachedir: $PYTHON_PREFIX/.pytest_cache
|
2019-04-15 22:24:17 +08:00
|
|
|
|
rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini
|
2018-06-27 14:28:21 +08:00
|
|
|
|
collected 2 items
|
|
|
|
|
|
|
|
|
|
test_example.py .. [100%]
|
|
|
|
|
|
|
|
|
|
============================= warnings summary =============================
|
2018-10-27 21:07:54 +08:00
|
|
|
|
test_example.py::test_plugin
|
2019-04-15 22:24:17 +08:00
|
|
|
|
$REGENDOC_TMPDIR/test_example.py:4: PytestExperimentalApiWarning: testdir.copy_example is an experimental api that may change over time
|
2018-10-27 21:07:54 +08:00
|
|
|
|
testdir.copy_example("test_example.py")
|
2018-06-27 14:28:21 +08:00
|
|
|
|
|
2018-08-29 23:57:54 +08:00
|
|
|
|
-- Docs: https://docs.pytest.org/en/latest/warnings.html
|
2018-11-03 21:51:39 +08:00
|
|
|
|
=================== 2 passed, 1 warnings in 0.12 seconds ===================
|
2018-06-27 12:52:54 +08:00
|
|
|
|
|
2017-07-20 01:42:33 +08:00
|
|
|
|
For more information about the result object that ``runpytest()`` returns, and
|
2017-07-15 22:48:02 +08:00
|
|
|
|
the methods that it provides please check out the :py:class:`RunResult
|
|
|
|
|
<_pytest.pytester.RunResult>` documentation.
|
2015-04-28 17:54:53 +08:00
|
|
|
|
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
2018-06-27 12:52:54 +08:00
|
|
|
|
|
|
|
|
|
|
2015-04-25 17:29:11 +08:00
|
|
|
|
.. _`writinghooks`:
|
|
|
|
|
|
|
|
|
|
Writing hook functions
|
|
|
|
|
======================
|
|
|
|
|
|
2015-04-28 17:54:53 +08:00
|
|
|
|
|
2015-04-25 17:29:11 +08:00
|
|
|
|
.. _validation:
|
|
|
|
|
|
|
|
|
|
hook function validation and execution
|
|
|
|
|
--------------------------------------
|
|
|
|
|
|
|
|
|
|
pytest calls hook functions from registered plugins for any
|
|
|
|
|
given hook specification. Let's look at a typical hook function
|
|
|
|
|
for the ``pytest_collection_modifyitems(session, config,
|
|
|
|
|
items)`` hook which pytest calls after collection of all test items is
|
|
|
|
|
completed.
|
|
|
|
|
|
|
|
|
|
When we implement a ``pytest_collection_modifyitems`` function in our plugin
|
|
|
|
|
pytest will during registration verify that you use argument
|
|
|
|
|
names which match the specification and bail out if not.
|
|
|
|
|
|
2015-07-10 08:50:38 +08:00
|
|
|
|
Let's look at a possible implementation:
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
|
|
|
|
def pytest_collection_modifyitems(config, items):
|
2015-08-26 10:54:05 +08:00
|
|
|
|
# called after collection is completed
|
2015-04-25 17:29:11 +08:00
|
|
|
|
# you can modify the ``items`` list
|
2018-06-03 11:19:17 +08:00
|
|
|
|
...
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
|
|
|
|
Here, ``pytest`` will pass in ``config`` (the pytest config object)
|
|
|
|
|
and ``items`` (the list of collected test items) but will not pass
|
|
|
|
|
in the ``session`` argument because we didn't list it in the function
|
|
|
|
|
signature. This dynamic "pruning" of arguments allows ``pytest`` to
|
|
|
|
|
be "future-compatible": we can introduce new hook named parameters without
|
|
|
|
|
breaking the signatures of existing hook implementations. It is one of
|
|
|
|
|
the reasons for the general long-lived compatibility of pytest plugins.
|
|
|
|
|
|
|
|
|
|
Note that hook functions other than ``pytest_runtest_*`` are not
|
|
|
|
|
allowed to raise exceptions. Doing so will break the pytest run.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-06-13 10:45:35 +08:00
|
|
|
|
.. _firstresult:
|
|
|
|
|
|
2015-04-27 18:50:34 +08:00
|
|
|
|
firstresult: stop at first non-None result
|
|
|
|
|
-------------------------------------------
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
2015-04-27 18:50:34 +08:00
|
|
|
|
Most calls to ``pytest`` hooks result in a **list of results** which contains
|
|
|
|
|
all non-None results of the called hook functions.
|
|
|
|
|
|
|
|
|
|
Some hook specifications use the ``firstresult=True`` option so that the hook
|
|
|
|
|
call only executes until the first of N registered functions returns a
|
|
|
|
|
non-None result which is then taken as result of the overall hook call.
|
|
|
|
|
The remaining hook functions will not be called in this case.
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
hookwrapper: executing around other hooks
|
|
|
|
|
-------------------------------------------------
|
|
|
|
|
|
|
|
|
|
.. currentmodule:: _pytest.core
|
|
|
|
|
|
2019-04-28 23:37:58 +08:00
|
|
|
|
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
|
|
|
|
pytest plugins can implement hook wrappers which wrap the execution
|
|
|
|
|
of other hook implementations. A hook wrapper is a generator function
|
|
|
|
|
which yields exactly once. When pytest invokes hooks it first executes
|
|
|
|
|
hook wrappers and passes the same arguments as to the regular hooks.
|
|
|
|
|
|
|
|
|
|
At the yield point of the hook wrapper pytest will execute the next hook
|
|
|
|
|
implementations and return their result to the yield point in the form of
|
2017-11-16 00:08:11 +08:00
|
|
|
|
a :py:class:`Result <pluggy._Result>` instance which encapsulates a result or
|
2015-04-25 17:29:11 +08:00
|
|
|
|
exception info. The yield point itself will thus typically not raise
|
|
|
|
|
exceptions (unless there are bugs).
|
|
|
|
|
|
2019-04-12 19:50:26 +08:00
|
|
|
|
Here is an example definition of a hook wrapper:
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
2019-04-12 19:50:26 +08:00
|
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
|
@pytest.hookimpl(hookwrapper=True)
|
2015-04-25 17:29:11 +08:00
|
|
|
|
def pytest_pyfunc_call(pyfuncitem):
|
2018-02-01 04:18:15 +08:00
|
|
|
|
do_something_before_next_hook_executes()
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
|
|
|
|
outcome = yield
|
|
|
|
|
# outcome.excinfo may be None or a (cls, val, tb) tuple
|
|
|
|
|
|
|
|
|
|
res = outcome.get_result() # will raise if outcome was exception
|
2018-02-01 04:18:15 +08:00
|
|
|
|
|
|
|
|
|
post_process_result(res)
|
|
|
|
|
|
|
|
|
|
outcome.force_result(new_res) # to override the return value to the plugin system
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
|
|
|
|
Note that hook wrappers don't return results themselves, they merely
|
|
|
|
|
perform tracing or other side effects around the actual hook implementations.
|
|
|
|
|
If the result of the underlying hook is a mutable object, they may modify
|
2015-05-06 16:08:08 +08:00
|
|
|
|
that result but it's probably better to avoid it.
|
2015-04-27 18:50:34 +08:00
|
|
|
|
|
2018-02-01 04:18:15 +08:00
|
|
|
|
For more information, consult the `pluggy documentation <http://pluggy.readthedocs.io/en/latest/#wrappers>`_.
|
|
|
|
|
|
2015-04-27 18:50:34 +08:00
|
|
|
|
|
|
|
|
|
Hook function ordering / call example
|
|
|
|
|
-------------------------------------
|
|
|
|
|
|
|
|
|
|
For any given hook specification there may be more than one
|
|
|
|
|
implementation and we thus generally view ``hook`` execution as a
|
|
|
|
|
``1:N`` function call where ``N`` is the number of registered functions.
|
|
|
|
|
There are ways to influence if a hook implementation comes before or
|
2015-07-10 08:50:38 +08:00
|
|
|
|
after others, i.e. the position in the ``N``-sized list of functions:
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
2015-04-27 18:50:34 +08:00
|
|
|
|
|
|
|
|
|
# Plugin 1
|
2015-09-30 21:58:12 +08:00
|
|
|
|
@pytest.hookimpl(tryfirst=True)
|
2015-04-27 18:50:34 +08:00
|
|
|
|
def pytest_collection_modifyitems(items):
|
|
|
|
|
# will execute as early as possible
|
2018-06-03 11:19:17 +08:00
|
|
|
|
...
|
2015-04-27 18:50:34 +08:00
|
|
|
|
|
2018-06-03 11:29:28 +08:00
|
|
|
|
|
2015-04-27 18:50:34 +08:00
|
|
|
|
# Plugin 2
|
2015-09-30 21:58:12 +08:00
|
|
|
|
@pytest.hookimpl(trylast=True)
|
2015-04-27 18:50:34 +08:00
|
|
|
|
def pytest_collection_modifyitems(items):
|
|
|
|
|
# will execute as late as possible
|
2018-06-03 11:19:17 +08:00
|
|
|
|
...
|
2015-04-27 18:50:34 +08:00
|
|
|
|
|
2018-06-03 11:29:28 +08:00
|
|
|
|
|
2015-04-27 18:50:34 +08:00
|
|
|
|
# Plugin 3
|
2015-09-30 21:12:33 +08:00
|
|
|
|
@pytest.hookimpl(hookwrapper=True)
|
2015-04-27 18:50:34 +08:00
|
|
|
|
def pytest_collection_modifyitems(items):
|
|
|
|
|
# will execute even before the tryfirst one above!
|
|
|
|
|
outcome = yield
|
|
|
|
|
# will execute after all non-hookwrappers executed
|
|
|
|
|
|
|
|
|
|
Here is the order of execution:
|
|
|
|
|
|
|
|
|
|
1. Plugin3's pytest_collection_modifyitems called until the yield point
|
2015-05-06 16:08:08 +08:00
|
|
|
|
because it is a hook wrapper.
|
|
|
|
|
|
|
|
|
|
2. Plugin1's pytest_collection_modifyitems is called because it is marked
|
|
|
|
|
with ``tryfirst=True``.
|
|
|
|
|
|
|
|
|
|
3. Plugin2's pytest_collection_modifyitems is called because it is marked
|
|
|
|
|
with ``trylast=True`` (but even without this mark it would come after
|
|
|
|
|
Plugin1).
|
|
|
|
|
|
|
|
|
|
4. Plugin3's pytest_collection_modifyitems then executing the code after the yield
|
2017-11-16 00:08:11 +08:00
|
|
|
|
point. The yield receives a :py:class:`Result <pluggy._Result>` instance which encapsulates
|
2015-05-06 16:08:08 +08:00
|
|
|
|
the result from calling the non-wrappers. Wrappers shall not modify the result.
|
2015-04-27 18:50:34 +08:00
|
|
|
|
|
|
|
|
|
It's possible to use ``tryfirst`` and ``trylast`` also in conjunction with
|
|
|
|
|
``hookwrapper=True`` in which case it will influence the ordering of hookwrappers
|
|
|
|
|
among each other.
|
|
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
|
|
2015-04-25 17:29:11 +08:00
|
|
|
|
Declaring new hooks
|
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
|
|
.. currentmodule:: _pytest.hookspec
|
|
|
|
|
|
|
|
|
|
Plugins and ``conftest.py`` files may declare new hooks that can then be
|
|
|
|
|
implemented by other plugins in order to alter behaviour or interact with
|
|
|
|
|
the new plugin:
|
|
|
|
|
|
|
|
|
|
.. autofunction:: pytest_addhooks
|
2018-03-02 04:55:45 +08:00
|
|
|
|
:noindex:
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
|
|
|
|
Hooks are usually declared as do-nothing functions that contain only
|
|
|
|
|
documentation describing when the hook will be called and what return values
|
2019-06-29 00:13:47 +08:00
|
|
|
|
are expected. The names of the functions must start with `pytest_` otherwise pytest won't recognize them.
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
2019-06-29 00:13:47 +08:00
|
|
|
|
Here's an example. Let's assume this code is in the ``hooks.py`` module.
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
def pytest_my_hook(config):
|
|
|
|
|
"""
|
|
|
|
|
Receives the pytest config and does things with it
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
To register the hooks with pytest they need to be structured in their own module or class. This
|
|
|
|
|
class or module can then be passed to the ``pluginmanager`` using the ``pytest_addhooks`` function
|
|
|
|
|
(which itself is a hook exposed by pytest).
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
def pytest_addhooks(pluginmanager):
|
|
|
|
|
""" This example assumes the hooks are grouped in the 'hooks' module. """
|
|
|
|
|
from my_app.tests import hooks
|
2019-06-29 20:11:09 +08:00
|
|
|
|
|
2019-06-29 00:13:47 +08:00
|
|
|
|
pluginmanager.add_hookspecs(hooks)
|
|
|
|
|
|
|
|
|
|
For a real world example, see `newhooks.py`_ from `xdist <https://github.com/pytest-dev/pytest-xdist>`_.
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
2015-12-02 02:20:55 +08:00
|
|
|
|
.. _`newhooks.py`: https://github.com/pytest-dev/pytest-xdist/blob/974bd566c599dc6a9ea291838c6f226197208b46/xdist/newhooks.py
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
2019-06-29 00:13:47 +08:00
|
|
|
|
Hooks may be called both from fixtures or from other hooks. In both cases, hooks are called
|
|
|
|
|
through the ``hook`` object, available in the ``config`` object. Most hooks receive a
|
|
|
|
|
``config`` object directly, while fixtures may use the ``pytestconfig`` fixture which provides the same object.
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
|
def my_fixture(pytestconfig):
|
|
|
|
|
# call the hook called "pytest_my_hook"
|
2019-06-29 20:11:09 +08:00
|
|
|
|
# 'result' will be a list of return values from all registered functions.
|
2019-06-29 00:13:47 +08:00
|
|
|
|
result = pytestconfig.hook.pytest_my_hook(config=pytestconfig)
|
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
Hooks receive parameters using only keyword arguments.
|
|
|
|
|
|
|
|
|
|
Now your hook is ready to be used. To register a function at the hook, other plugins or users must
|
|
|
|
|
now simply define the function ``pytest_my_hook`` with the correct signature in their ``conftest.py``.
|
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
def pytest_my_hook(config):
|
|
|
|
|
"""
|
|
|
|
|
Print all active hooks to the screen.
|
|
|
|
|
"""
|
|
|
|
|
print(config.hook)
|
|
|
|
|
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
2015-05-06 16:08:08 +08:00
|
|
|
|
Optionally using hooks from 3rd party plugins
|
|
|
|
|
---------------------------------------------
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
|
|
|
|
Using new hooks from plugins as explained above might be a little tricky
|
2015-05-06 16:08:08 +08:00
|
|
|
|
because of the standard :ref:`validation mechanism <validation>`:
|
2015-04-25 17:29:11 +08:00
|
|
|
|
if you depend on a plugin that is not installed, validation will fail and
|
|
|
|
|
the error message will not make much sense to your users.
|
|
|
|
|
|
|
|
|
|
One approach is to defer the hook implementation to a new plugin instead of
|
2019-04-12 19:50:26 +08:00
|
|
|
|
declaring the hook functions directly in your plugin module, for example:
|
|
|
|
|
|
|
|
|
|
.. code-block:: python
|
2015-04-25 17:29:11 +08:00
|
|
|
|
|
|
|
|
|
# contents of myplugin.py
|
|
|
|
|
|
2019-04-12 19:50:26 +08:00
|
|
|
|
|
2015-04-25 17:29:11 +08:00
|
|
|
|
class DeferPlugin(object):
|
|
|
|
|
"""Simple plugin to defer pytest-xdist hook functions."""
|
|
|
|
|
|
|
|
|
|
def pytest_testnodedown(self, node, error):
|
|
|
|
|
"""standard xdist hook function.
|
|
|
|
|
"""
|
|
|
|
|
|
2019-04-12 19:50:26 +08:00
|
|
|
|
|
2015-04-25 17:29:11 +08:00
|
|
|
|
def pytest_configure(config):
|
2019-04-12 19:50:26 +08:00
|
|
|
|
if config.pluginmanager.hasplugin("xdist"):
|
2015-04-25 17:29:11 +08:00
|
|
|
|
config.pluginmanager.register(DeferPlugin())
|
|
|
|
|
|
|
|
|
|
This has the added benefit of allowing you to conditionally install hooks
|
|
|
|
|
depending on which plugins are installed.
|