2010-11-02 07:53:53 +08:00
|
|
|
.. _`external plugins`:
|
2010-12-06 23:54:42 +08:00
|
|
|
.. _`extplugins`:
|
2015-04-25 17:29:11 +08:00
|
|
|
.. _`using plugins`:
|
|
|
|
|
2021-03-11 03:18:21 +08:00
|
|
|
How to install and use plugins
|
|
|
|
===============================
|
2010-11-02 07:53:53 +08:00
|
|
|
|
2015-04-25 17:29:11 +08:00
|
|
|
This section talks about installing and using third party plugins.
|
|
|
|
For writing your own plugins, please refer to :ref:`writing-plugins`.
|
2010-11-02 07:53:53 +08:00
|
|
|
|
2019-08-07 07:20:06 +08:00
|
|
|
Installing a third party plugin can be easily done with ``pip``:
|
2010-11-02 07:53:53 +08:00
|
|
|
|
2019-08-07 04:25:54 +08:00
|
|
|
.. code-block:: bash
|
|
|
|
|
2010-11-02 07:53:53 +08:00
|
|
|
pip install pytest-NAME
|
|
|
|
pip uninstall pytest-NAME
|
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
If a plugin is installed, ``pytest`` automatically finds and integrates it,
|
2015-12-11 05:00:01 +08:00
|
|
|
there is no need to activate it.
|
|
|
|
|
|
|
|
Here is a little annotated list for some popular plugins:
|
2012-10-09 20:35:17 +08:00
|
|
|
|
2021-10-22 20:47:57 +08:00
|
|
|
* :pypi:`pytest-django`: write tests
|
2022-02-08 03:32:02 +08:00
|
|
|
for `django <https://docs.djangoproject.com/>`_ apps, using pytest integration.
|
2012-10-09 20:35:17 +08:00
|
|
|
|
2021-10-22 20:47:57 +08:00
|
|
|
* :pypi:`pytest-twisted`: write tests
|
2021-04-26 23:44:27 +08:00
|
|
|
for `twisted <https://twistedmatrix.com/>`_ apps, starting a reactor and
|
2012-10-22 15:32:41 +08:00
|
|
|
processing deferreds from test functions.
|
|
|
|
|
2021-10-22 20:47:57 +08:00
|
|
|
* :pypi:`pytest-cov`:
|
2013-05-08 03:34:59 +08:00
|
|
|
coverage reporting, compatible with distributed testing
|
|
|
|
|
2021-10-22 20:47:57 +08:00
|
|
|
* :pypi:`pytest-xdist`:
|
2012-10-09 20:35:17 +08:00
|
|
|
to distribute tests to CPUs and remote hosts, to run in boxed
|
|
|
|
mode which allows to survive segmentation faults, to run in
|
2014-01-18 19:31:33 +08:00
|
|
|
looponfailing mode, automatically re-running failing tests
|
2016-08-23 10:35:41 +08:00
|
|
|
on file changes.
|
2012-10-09 20:35:17 +08:00
|
|
|
|
2021-10-22 20:47:57 +08:00
|
|
|
* :pypi:`pytest-instafail`:
|
2013-05-08 03:34:59 +08:00
|
|
|
to report failures while the test run is happening.
|
|
|
|
|
2021-10-22 20:47:57 +08:00
|
|
|
* :pypi:`pytest-bdd`:
|
2013-05-08 03:34:59 +08:00
|
|
|
to write tests using behaviour-driven testing.
|
|
|
|
|
2021-10-22 20:47:57 +08:00
|
|
|
* :pypi:`pytest-timeout`:
|
2012-10-09 20:35:17 +08:00
|
|
|
to timeout tests based on function marks or global definitions.
|
|
|
|
|
2021-10-22 20:47:57 +08:00
|
|
|
* :pypi:`pytest-pep8`:
|
2011-02-17 21:46:40 +08:00
|
|
|
a ``--pep8`` option to enable PEP8 compliance checking.
|
2010-12-06 23:54:42 +08:00
|
|
|
|
2021-10-22 20:47:57 +08:00
|
|
|
* :pypi:`pytest-flakes`:
|
2015-12-11 05:00:01 +08:00
|
|
|
check source code with pyflakes.
|
|
|
|
|
2014-02-19 08:33:13 +08:00
|
|
|
To see a complete list of all plugins with their latest testing
|
2016-06-21 22:16:57 +08:00
|
|
|
status against different pytest and Python versions, please visit
|
2021-03-12 04:30:47 +08:00
|
|
|
:ref:`plugin-list`.
|
2014-02-19 08:33:13 +08:00
|
|
|
|
2018-11-05 01:02:28 +08:00
|
|
|
You may also discover more plugins through a `pytest- pypi.org search`_.
|
2010-11-02 07:53:53 +08:00
|
|
|
|
2018-11-05 01:02:28 +08:00
|
|
|
.. _`pytest- pypi.org search`: https://pypi.org/search/?q=pytest-
|
2010-11-02 07:53:53 +08:00
|
|
|
|
2014-08-20 06:52:40 +08:00
|
|
|
|
2018-03-02 05:20:55 +08:00
|
|
|
.. _`available installable plugins`:
|
|
|
|
|
2010-11-02 07:53:53 +08:00
|
|
|
Requiring/Loading plugins in a test module or conftest file
|
2014-08-20 06:52:40 +08:00
|
|
|
-----------------------------------------------------------
|
2010-11-02 07:53:53 +08:00
|
|
|
|
2020-07-10 20:50:03 +08:00
|
|
|
You can require plugins in a test module or a conftest file using :globalvar:`pytest_plugins`:
|
2010-11-02 07:53:53 +08:00
|
|
|
|
2018-10-10 00:30:56 +08:00
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
pytest_plugins = ("myapp.testsupport.myplugin",)
|
2010-11-02 07:53:53 +08:00
|
|
|
|
|
|
|
When the test module or conftest plugin is loaded the specified plugins
|
2015-04-25 17:29:11 +08:00
|
|
|
will be loaded as well.
|
2010-11-02 07:53:53 +08:00
|
|
|
|
2018-02-21 07:27:24 +08:00
|
|
|
.. note::
|
2020-07-10 20:50:03 +08:00
|
|
|
|
2018-02-21 07:27:24 +08:00
|
|
|
Requiring plugins using a ``pytest_plugins`` variable in non-root
|
|
|
|
``conftest.py`` files is deprecated. See
|
2018-03-13 05:32:51 +08:00
|
|
|
:ref:`full explanation <requiring plugins in non-root conftests>`
|
2018-02-21 07:27:24 +08:00
|
|
|
in the Writing plugins section.
|
|
|
|
|
2019-02-07 04:51:05 +08:00
|
|
|
.. note::
|
|
|
|
The name ``pytest_plugins`` is reserved and should not be used as a
|
|
|
|
name for a custom plugin module.
|
|
|
|
|
|
|
|
|
2010-12-06 23:54:42 +08:00
|
|
|
.. _`findpluginname`:
|
|
|
|
|
|
|
|
Finding out which plugins are active
|
2014-08-20 06:52:40 +08:00
|
|
|
------------------------------------
|
2010-12-06 23:54:42 +08:00
|
|
|
|
|
|
|
If you want to find out which plugins are active in your
|
2019-08-07 07:20:06 +08:00
|
|
|
environment you can type:
|
2010-12-06 23:54:42 +08:00
|
|
|
|
2019-08-07 04:25:54 +08:00
|
|
|
.. code-block:: bash
|
|
|
|
|
2016-06-21 22:16:57 +08:00
|
|
|
pytest --trace-config
|
2010-12-06 23:54:42 +08:00
|
|
|
|
|
|
|
and will get an extended test header which shows activated plugins
|
|
|
|
and their names. It will also print local plugins aka
|
2017-11-04 02:37:18 +08:00
|
|
|
:ref:`conftest.py <conftest.py plugins>` files when they are loaded.
|
2010-12-06 23:54:42 +08:00
|
|
|
|
|
|
|
.. _`cmdunregister`:
|
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Deactivating / unregistering a plugin by name
|
2014-08-20 06:52:40 +08:00
|
|
|
---------------------------------------------
|
2010-12-06 23:54:42 +08:00
|
|
|
|
2019-08-07 07:20:06 +08:00
|
|
|
You can prevent plugins from loading or unregister them:
|
2010-12-06 23:54:42 +08:00
|
|
|
|
2019-08-07 04:25:54 +08:00
|
|
|
.. code-block:: bash
|
|
|
|
|
2016-06-21 22:16:57 +08:00
|
|
|
pytest -p no:NAME
|
2010-12-06 23:54:42 +08:00
|
|
|
|
|
|
|
This means that any subsequent try to activate/load the named
|
2015-09-29 05:23:08 +08:00
|
|
|
plugin will not work.
|
|
|
|
|
|
|
|
If you want to unconditionally disable a plugin for a project, you can add
|
|
|
|
this option to your ``pytest.ini`` file:
|
|
|
|
|
|
|
|
.. code-block:: ini
|
|
|
|
|
|
|
|
[pytest]
|
|
|
|
addopts = -p no:NAME
|
|
|
|
|
|
|
|
Alternatively to disable it only in certain environments (for example in a
|
|
|
|
CI server), you can set ``PYTEST_ADDOPTS`` environment variable to
|
|
|
|
``-p no:name``.
|
|
|
|
|
|
|
|
See :ref:`findpluginname` for how to obtain the name of a plugin.
|
2010-11-06 18:38:53 +08:00
|
|
|
|
|
|
|
.. _`builtin plugins`:
|