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