Debian Codegraph @

doc/en/how-to/plugins.rst

raw
1 .. _`external plugins`:
2 .. _`extplugins`:
3 .. _`using plugins`:
4
5 How to install and use plugins
6 ===============================
7
8 This section talks about installing and using third party plugins.
9 For writing your own plugins, please refer to :ref:`writing-plugins`.
10
11 Installing a third party plugin can be easily done with ``pip``:
12
13 .. code-block:: bash
14
15 pip install pytest-NAME
16 pip uninstall pytest-NAME
17
18 If a plugin is installed, ``pytest`` automatically finds and integrates it,
19 there is no need to activate it.
20
21 Here is a little annotated list for some popular plugins:
22
23 * :pypi:`pytest-django`: write tests
24 for `django <https://docs.djangoproject.com/>`_ apps, using pytest integration.
25
26 * :pypi:`pytest-twisted`: write tests
27 for `twisted <https://twistedmatrix.com/>`_ apps, starting a reactor and
28 processing deferreds from test functions.
29
30 * :pypi:`pytest-cov`:
31 coverage reporting, compatible with distributed testing
32
33 * :pypi:`pytest-xdist`:
34 to distribute tests to CPUs and remote hosts, to run in boxed
35 mode that allows pytest to survive segmentation faults, to run in
36 looponfailing mode, automatically re-running failing tests
37 on file changes.
38
39 * :pypi:`pytest-instafail`:
40 to report failures while the test run is happening.
41
42 * :pypi:`pytest-bdd`:
43 to write tests using behaviour-driven testing.
44
45 * :pypi:`pytest-timeout`:
46 to timeout tests based on function marks or global definitions.
47
48 * :pypi:`pytest-pep8`:
49 a ``--pep8`` option to enable PEP8 compliance checking.
50
51 * :pypi:`pytest-flakes`:
52 check source code with pyflakes.
53
54 * :pypi:`allure-pytest`:
55 report test results via `allure-framework <https://github.com/allure-framework/>`_.
56
57 To see a complete list of all plugins with their latest testing
58 status against different pytest and Python versions, please visit
59 :ref:`plugin-list`.
60
61 You may also discover more plugins through a `pytest- pypi.org search`_.
62
63 .. _`pytest- pypi.org search`: https://pypi.org/search/?q=pytest-
64
65
66 .. _`available installable plugins`:
67
68 Requiring/Loading plugins in a test module or conftest file
69 -----------------------------------------------------------
70
71 You can require plugins in a test module or a conftest file using :globalvar:`pytest_plugins`:
72
73 .. code-block:: python
74
75 pytest_plugins = ("myapp.testsupport.myplugin",)
76
77 When the test module or conftest plugin is loaded the specified plugins
78 will be loaded as well.
79
80 .. note::
81
82 Requiring plugins using a ``pytest_plugins`` variable in non-root
83 ``conftest.py`` files is deprecated. See
84 :ref:`full explanation <requiring plugins in non-root conftests>`
85 in the Writing plugins section.
86
87 .. note::
88 The name ``pytest_plugins`` is reserved and should not be used as a
89 name for a custom plugin module.
90
91
92 .. _`findpluginname`:
93
94 Finding out which plugins are active
95 ------------------------------------
96
97 If you want to find out which plugins are active in your
98 environment you can type:
99
100 .. code-block:: bash
101
102 pytest --trace-config
103
104 and will get an extended test header which shows activated plugins
105 and their names. It will also print local plugins aka
106 :ref:`conftest.py <conftest.py plugins>` files when they are loaded.
107
108 .. _`cmdunregister`:
109
110 Deactivating / unregistering a plugin by name
111 ---------------------------------------------
112
113 You can prevent plugins from loading or unregister them:
114
115 .. code-block:: bash
116
117 pytest -p no:NAME
118
119 This means that any subsequent try to activate/load the named
120 plugin will not work.
121
122 If you want to unconditionally disable a plugin for a project, you can add
123 this option to your configuration file:
124
125 .. tab:: toml
126
127 .. code-block:: toml
128
129 [pytest]
130 addopts = ["-p", "no:NAME"]
131
132 .. tab:: ini
133
134 .. code-block:: ini
135
136 [pytest]
137 addopts = -p no:NAME
138
139 Alternatively to disable it only in certain environments (for example in a
140 CI server), you can set ``PYTEST_ADDOPTS`` environment variable to
141 ``-p no:name``.
142
143 See :ref:`findpluginname` for how to obtain the name of a plugin.
144
145 .. _`disable_plugin_autoload`:
146
147 Disabling plugins from autoloading
148 ----------------------------------
149
150 If you want to disable plugins from loading automatically, instead of requiring you to
151 manually specify each plugin with :option:`-p` or :envvar:`PYTEST_PLUGINS`, you can use :option:`--disable-plugin-autoload` or :envvar:`PYTEST_DISABLE_PLUGIN_AUTOLOAD`.
152
153 .. code-block:: bash
154
155 export PYTEST_DISABLE_PLUGIN_AUTOLOAD=1
156 export PYTEST_PLUGINS=NAME,NAME2
157 pytest
158
159 .. code-block:: bash
160
161 pytest --disable-plugin-autoload -p NAME -p NAME2
162
163 .. tab:: toml
164
165 .. code-block:: toml
166
167 [pytest]
168 addopts = ["--disable-plugin-autoload", "-p", "NAME", "-p", "NAME2"]
169
170 .. tab:: ini
171
172 .. code-block:: ini
173
174 [pytest]
175 addopts =
176 --disable-plugin-autoload
177 -p NAME
178 -p NAME2
179
180 .. versionadded:: 8.4
181
182 The :option:`--disable-plugin-autoload` command-line flag.
183
184 .. note::
185
186 :option:`-p` and :envvar:`PYTEST_PLUGINS` are both ways to explicitly control which
187 plugins are loaded, but they serve slightly different use-cases.
188
189 * :option:`-p` loads (or disables with ``-p no:<name>``) a plugin by name or entry point
190 for a specific pytest invocation, and is processed early during startup.
191 * :envvar:`PYTEST_PLUGINS` is a comma-separated list of Python modules that are imported
192 and registered as plugins during startup. This mechanism is commonly used by test
193 suites, for example when testing a plugin.
194
195 When explicitly controlling plugin loading (especially with
196 :envvar:`PYTEST_DISABLE_PLUGIN_AUTOLOAD` or :option:`--disable-plugin-autoload`),
197 avoid specifying the same plugin via multiple mechanisms. Registering the same plugin
198 more than once can lead to errors during plugin registration.
199
200 Examples:
201
202 .. code-block:: bash
203
204 # Disable auto-loading and load only specific plugins for this invocation
205 PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 pytest -p xdist
206
207 .. code-block:: bash
208
209 # Disable auto-loading and load plugin modules during startup
210 PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 PYTEST_PLUGINS=mymodule.plugin,xdist pytest