Debian Codegraph @

doc/en/reference/customize.rst

raw
1 Configuration
2 =============
3
4 Command line options and configuration file settings
5 -----------------------------------------------------------------
6
7 You can get help on command line and configuration options by using the general help option:
8
9 .. code-block:: bash
10
11 pytest -h # prints options _and_ config file settings
12
13 This will display command line and configuration file settings
14 which were registered by installed plugins.
15
16 .. _`config file formats`:
17
18 Configuration file formats
19 --------------------------
20
21 Many :ref:`pytest settings <ini options ref>` can be set in a *configuration file*, which
22 by convention resides in the root directory of your repository.
23
24 A quick example of the configuration files supported by pytest:
25
26 pytest.toml
27 ~~~~~~~~~~~
28
29 .. versionadded:: 9.0
30
31 ``pytest.toml`` files take precedence over other files, even when empty.
32
33 Alternatively, the hidden version ``.pytest.toml`` can be used.
34
35 .. tab:: toml
36
37 .. code-block:: toml
38
39 # pytest.toml or .pytest.toml
40 [pytest]
41 minversion = "9.0"
42 addopts = ["-ra", "-q"]
43 testpaths = [
44 "tests",
45 "integration",
46 ]
47
48 pytest.ini
49 ~~~~~~~~~~
50
51 ``pytest.ini`` files take precedence over other files (except ``pytest.toml`` and ``.pytest.toml``), even when empty.
52
53 Alternatively, the hidden version ``.pytest.ini`` can be used.
54
55 .. tab:: ini
56
57 .. code-block:: ini
58
59 # pytest.ini or .pytest.ini
60 [pytest]
61 minversion = 6.0
62 addopts = -ra -q
63 testpaths =
64 tests
65 integration
66
67
68 pyproject.toml
69 ~~~~~~~~~~~~~~
70
71 .. versionadded:: 6.0
72 .. versionchanged:: 9.0
73
74 ``pyproject.toml`` files are supported for configuration.
75
76 .. tab:: toml
77
78 Use ``[tool.pytest]`` to leverage native TOML types (supported since pytest 9.0):
79
80 .. code-block:: toml
81
82 # pyproject.toml
83 [tool.pytest]
84 minversion = "9.0"
85 addopts = ["-ra", "-q"]
86 testpaths = [
87 "tests",
88 "integration",
89 ]
90
91 .. tab:: ini
92
93 Use ``[tool.pytest.ini_options]`` for INI-style configuration (supported since pytest 6.0):
94
95 .. code-block:: toml
96
97 # pyproject.toml
98 [tool.pytest.ini_options]
99 minversion = "6.0"
100 addopts = "-ra -q"
101 testpaths = [
102 "tests",
103 "integration",
104 ]
105
106 For projects that still run pytest versions older than 6.0, keep
107 ``minversion`` in ``pytest.ini`` or ``setup.cfg`` too. Those versions
108 do not read ``pyproject.toml``.
109
110 tox.ini
111 ~~~~~~~
112
113 ``tox.ini`` files are the configuration files of the `tox <https://tox.readthedocs.io>`__ project,
114 and can also be used to hold pytest configuration if they have a ``[pytest]`` section.
115
116 .. tab:: ini
117
118 .. code-block:: ini
119
120 # tox.ini
121 [pytest]
122 minversion = 6.0
123 addopts = -ra -q
124 testpaths =
125 tests
126 integration
127
128
129 setup.cfg
130 ~~~~~~~~~
131
132 ``setup.cfg`` files are general purpose configuration files, used originally by ``distutils`` (now deprecated) and :std:doc:`setuptools <setuptools:userguide/declarative_config>`, and can also be used to hold pytest configuration
133 if they have a ``[tool:pytest]`` section.
134
135 .. tab:: ini
136
137 .. code-block:: ini
138
139 # setup.cfg
140 [tool:pytest]
141 minversion = 6.0
142 addopts = -ra -q
143 testpaths =
144 tests
145 integration
146
147 .. warning::
148
149 Usage of ``setup.cfg`` is not recommended unless for very simple use cases. ``.cfg``
150 files use a different parser than ``pytest.ini`` and ``tox.ini`` which might cause hard to track
151 down problems.
152 When possible, it is recommended to use the latter files, or ``pyproject.toml``, to hold your
153 pytest configuration.
154
155
156 .. _rootdir:
157 .. _configfiles:
158
159 Initialization: determining rootdir and configfile
160 --------------------------------------------------
161
162 pytest determines a ``rootdir`` for each test run which depends on
163 the command line arguments (specified test files, paths) and on
164 the existence of configuration files. The determined ``rootdir`` and ``configfile`` are
165 printed as part of the pytest header during startup.
166
167 Here's a summary of what ``pytest`` uses ``rootdir`` for:
168
169 * Construct *nodeids* during collection; each test is assigned
170 a unique *nodeid* which is rooted at the ``rootdir`` and takes into account
171 the full path, class name, function name and parametrization (if any).
172
173 * Is used by plugins as a stable location to store project/test run specific information;
174 for example, the internal :ref:`cache <cache>` plugin creates a ``.pytest_cache`` subdirectory
175 in ``rootdir`` to store its cross-test run state.
176
177 ``rootdir`` is **NOT** used to modify ``sys.path``/``PYTHONPATH`` or
178 influence how modules are imported. See :ref:`pythonpath` for more details.
179
180 The :option:`--rootdir=path` command-line option can be used to force a specific directory.
181 Note that contrary to other command-line options, ``--rootdir`` cannot be used with
182 :confval:`addopts` inside a configuration file because the ``rootdir`` is used to *find* the configuration file
183 already.
184
185 Finding the ``rootdir``
186 ~~~~~~~~~~~~~~~~~~~~~~~
187
188 Here is the algorithm which finds the rootdir from ``args``:
189
190 - If :option:`-c` is passed in the command-line, use that as configuration file, and its directory as ``rootdir``.
191
192 - Determine the common ancestor directory for the specified ``args`` that are
193 recognised as paths that exist in the file system. If no such paths are
194 found, the common ancestor directory is set to the current working directory.
195
196 - Look for ``pytest.toml``, ``.pytest.toml``, ``pytest.ini``, ``.pytest.ini``, ``pyproject.toml``, ``tox.ini``, and ``setup.cfg`` files in the ancestor
197 directory and upwards. If one is matched, it becomes the ``configfile`` and its
198 directory becomes the ``rootdir``.
199
200 - If no configuration file was found, look for ``setup.py`` upwards from the common
201 ancestor directory to determine the ``rootdir``.
202
203 - If no ``setup.py`` was found, look for ``pytest.toml``, ``.pytest.toml``, ``pytest.ini``, ``.pytest.ini``, ``pyproject.toml``, ``tox.ini``, and
204 ``setup.cfg`` in each of the specified ``args`` and upwards. If one is
205 matched, it becomes the ``configfile`` and its directory becomes the ``rootdir``.
206
207 - If no ``configfile`` was found and no configuration argument is passed, use the already determined common ancestor as root
208 directory. This allows the use of pytest in structures that are not part of
209 a package and don't have any particular configuration file.
210
211 If no ``args`` are given, pytest collects tests below the current working
212 directory and also starts determining the ``rootdir`` from there.
213
214 Files will only be matched for configuration if:
215
216 * ``pytest.toml``: will always match and take highest precedence, even if empty.
217 * ``pytest.ini``: will always match and take precedence (after ``pytest.toml`` and ``.pytest.toml``), even if empty.
218 * ``pyproject.toml``: contains a ``[tool.pytest]`` or ``[tool.pytest.ini_options]`` table.
219 * ``tox.ini``: contains a ``[pytest]`` section.
220 * ``setup.cfg``: contains a ``[tool:pytest]`` section.
221
222 Finally, a ``pyproject.toml`` file will be considered the ``configfile`` if no other match was found, in this case
223 even if it does not contain a ``[tool.pytest]`` table (since version ``9.0``) or a ``[tool.pytest.ini_options]``
224 table (since version ``8.1``).
225
226 The files are considered in the order above. Options from multiple ``configfiles`` candidates
227 are never merged - the first match wins.
228
229 The configuration file also determines the value of the ``rootpath``.
230
231 The :class:`Config <pytest.Config>` object (accessible via hooks or through the :fixture:`pytestconfig` fixture)
232 will subsequently carry these attributes:
233
234 - :attr:`config.rootpath <pytest.Config.rootpath>`: the determined root directory, guaranteed to exist. It is used as
235 a reference directory for constructing test addresses ("nodeids") and can be used also by plugins for storing
236 per-testrun information.
237
238 - :attr:`config.inipath <pytest.Config.inipath>`: the determined ``configfile``, may be ``None``
239 (it is named ``inipath`` for historical reasons).
240
241 .. versionadded:: 6.1
242 The ``config.rootpath`` and ``config.inipath`` properties. They are :class:`pathlib.Path`
243 versions of the older ``config.rootdir`` and ``config.inifile``, which have type
244 ``py.path.local``, and still exist for backward compatibility.
245
246
247
248 Example:
249
250 .. code-block:: bash
251
252 pytest path/to/testdir path/other/
253
254 will determine the common ancestor as ``path`` and then
255 check for configuration files as follows:
256
257 .. code-block:: text
258
259 # first look for path/pytest.toml
260 path/pytest.toml
261 path/pytest.ini
262 path/pyproject.toml # must contain a [tool.pytest] table to match
263 path/tox.ini # must contain [pytest] section to match
264 path/setup.cfg # must contain [tool:pytest] section to match
265 pytest.toml
266 pytest.ini
267 ... # all the way up to the root
268
269 # now look for setup.py
270 path/setup.py
271 setup.py
272 ... # all the way up to the root
273
274
275 .. warning::
276
277 Custom pytest plugin commandline arguments may include a path, as in
278 ``pytest --log-output ../../test.log args``. Then ``args`` is mandatory,
279 otherwise pytest uses the directory of test.log for rootdir determination
280 (see also :issue:`1435`).
281 A dot ``.`` for referencing the current working directory is also
282 possible.
283
284
285 .. _`how to change command line options defaults`:
286 .. _`adding default options`:
287
288
289 Builtin configuration file options
290 ----------------------------------------------
291
292 For the full list of options consult the :ref:`reference documentation <ini options ref>`.
293
294 Syntax highlighting theme customization
295 ---------------------------------------
296
297 The syntax highlighting themes used by pytest can be customized using two environment variables:
298
299 - :envvar:`PYTEST_THEME` sets a `pygment style <https://pygments.org/docs/styles/>`_ to use.
300 - :envvar:`PYTEST_THEME_MODE` sets this style to *light* or *dark*.