test_ok2/doc/en/how-to/plugins.rst

137 lines
3.8 KiB
ReStructuredText
Raw Normal View History

2010-11-02 07:53:53 +08:00
.. _`external plugins`:
.. _`extplugins`:
.. _`using plugins`:
How to install and use plugins
===============================
2010-11-02 07:53:53 +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
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
If a plugin is installed, ``pytest`` automatically finds and integrates it,
there is no need to activate it.
Here is a little annotated list for some popular plugins:
* :pypi:`pytest-django`: write tests
for `django <https://docs.djangoproject.com/>`_ apps, using pytest integration.
* :pypi:`pytest-twisted`: write tests
for `twisted <https://twistedmatrix.com/>`_ apps, starting a reactor and
2012-10-22 15:32:41 +08:00
processing deferreds from test functions.
* :pypi:`pytest-cov`:
coverage reporting, compatible with distributed testing
* :pypi:`pytest-xdist`:
to distribute tests to CPUs and remote hosts, to run in boxed
mode which allows to survive segmentation faults, to run in
looponfailing mode, automatically re-running failing tests
on file changes.
* :pypi:`pytest-instafail`:
to report failures while the test run is happening.
* :pypi:`pytest-bdd`:
to write tests using behaviour-driven testing.
* :pypi:`pytest-timeout`:
to timeout tests based on function marks or global definitions.
* :pypi:`pytest-pep8`:
a ``--pep8`` option to enable PEP8 compliance checking.
* :pypi:`pytest-flakes`:
check source code with pyflakes.
* :pypi:`allure-pytest`:
report test results via `allure-framework <https://github.com/allure-framework/>`_.
To see a complete list of all plugins with their latest testing
status against different pytest and Python versions, please visit
:ref:`plugin-list`.
You may also discover more plugins through a `pytest- pypi.org search`_.
2010-11-02 07:53:53 +08:00
.. _`pytest- pypi.org search`: https://pypi.org/search/?q=pytest-
2010-11-02 07:53:53 +08:00
.. _`available installable plugins`:
2010-11-02 07:53:53 +08:00
Requiring/Loading plugins in a test module or conftest file
-----------------------------------------------------------
2010-11-02 07:53:53 +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
.. 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
will be loaded as well.
2010-11-02 07:53:53 +08:00
.. note::
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>`
in the Writing plugins section.
.. note::
The name ``pytest_plugins`` is reserved and should not be used as a
name for a custom plugin module.
.. _`findpluginname`:
Finding out which plugins are active
------------------------------------
If you want to find out which plugins are active in your
environment you can type:
2019-08-07 04:25:54 +08:00
.. code-block:: bash
pytest --trace-config
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.
.. _`cmdunregister`:
Deactivating / unregistering a plugin by name
---------------------------------------------
You can prevent plugins from loading or unregister them:
2019-08-07 04:25:54 +08:00
.. code-block:: bash
pytest -p no:NAME
This means that any subsequent try to activate/load the named
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.
.. _`builtin plugins`: