2017-07-20 08:11:17 +08:00
Configuration
=============
2009-08-19 01:04:57 +08:00
2010-11-01 00:41:58 +08:00
Command line options and configuration file settings
-----------------------------------------------------------------
2009-08-19 01:04:57 +08:00
2011-03-04 06:40:38 +08:00
You can get help on command line options and values in INI-style
2019-02-15 21:10:37 +08:00
configurations files by using the general help option:
.. code-block :: bash
2009-08-19 01:04:57 +08:00
2016-06-21 22:16:57 +08:00
pytest -h # prints options _and_ config file settings
2009-08-19 01:04:57 +08:00
2010-11-01 00:41:58 +08:00
This will display command line and configuration file settings
which were registered by installed plugins.
2009-08-19 01:04:57 +08:00
2020-06-08 21:03:10 +08:00
.. _`config file formats`:
Configuration file formats
--------------------------
Many :ref: `pytest settings <ini options ref>` can be set in a *configuration file* , which
2022-01-14 00:29:08 +08:00
by convention resides in the root directory of your repository.
2020-06-08 21:03:10 +08:00
A quick example of the configuration files supported by pytest:
pytest.ini
~~~~~~~~~~
`` pytest.ini `` files take precedence over other files, even when empty.
2022-06-14 19:24:35 +08:00
Alternatively, the hidden version `` .pytest.ini `` can be used.
2020-06-08 21:03:10 +08:00
.. code-block :: ini
2022-06-14 19:24:35 +08:00
# pytest.ini or .pytest.ini
2020-06-08 21:03:10 +08:00
[pytest]
minversion = 6.0
addopts = -ra -q
testpaths =
tests
integration
pyproject.toml
~~~~~~~~~~~~~~
.. versionadded :: 6.0
`` pyproject.toml `` are considered for configuration when they contain a `` tool.pytest.ini_options `` table.
.. code-block :: toml
# pyproject.toml
[tool.pytest.ini_options]
minversion = "6.0"
addopts = "-ra -q"
testpaths = [
"tests",
"integration",
]
.. note ::
One might wonder why `` [tool.pytest.ini_options] `` instead of `` [tool.pytest] `` as is the
case with other tools.
The reason is that the pytest team intends to fully utilize the rich TOML data format
for configuration in the future, reserving the `` [tool.pytest] `` table for that.
The `` ini_options `` table is being used, for now, as a bridge between the existing
`` .ini `` configuration system and the future configuration format.
tox.ini
~~~~~~~
`` tox.ini `` files are the configuration files of the `tox <https://tox.readthedocs.io> `__ project,
and can also be used to hold pytest configuration if they have a `` [pytest] `` section.
.. code-block :: ini
# tox.ini
[pytest]
minversion = 6.0
addopts = -ra -q
testpaths =
tests
integration
setup.cfg
~~~~~~~~~
2023-10-23 20:56:33 +08:00
`` 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
2020-06-08 21:03:10 +08:00
if they have a `` [tool:pytest] `` section.
.. code-block :: ini
# setup.cfg
[tool:pytest]
minversion = 6.0
addopts = -ra -q
testpaths =
tests
integration
.. warning ::
Usage of `` setup.cfg `` is not recommended unless for very simple use cases. `` .cfg ``
files use a different parser than `` pytest.ini `` and `` tox.ini `` which might cause hard to track
down problems.
When possible, it is recommended to use the latter files, or `` pyproject.toml `` , to hold your
pytest configuration.
2015-02-27 04:56:44 +08:00
.. _rootdir:
2020-06-08 21:03:10 +08:00
.. _configfiles:
2012-11-06 21:09:12 +08:00
2020-06-08 21:03:10 +08:00
Initialization: determining rootdir and configfile
--------------------------------------------------
2010-11-25 19:11:10 +08:00
2017-07-20 08:11:17 +08:00
pytest determines a `` rootdir `` for each test run which depends on
2015-02-27 04:56:44 +08:00
the command line arguments (specified test files, paths) and on
2020-06-08 21:03:10 +08:00
the existence of configuration files. The determined `` rootdir `` and `` configfile `` are
2017-07-20 08:11:17 +08:00
printed as part of the pytest header during startup.
Here's a summary what `` pytest `` uses `` rootdir `` for:
* Construct *nodeids* during collection; each test is assigned
2020-01-15 16:43:03 +08:00
a unique *nodeid* which is rooted at the `` rootdir `` and takes into account
the full path, class name, function name and parametrization (if any).
2017-07-20 08:11:17 +08:00
* Is used by plugins as a stable location to store project/test run specific information;
2018-09-20 03:06:36 +08:00
for example, the internal :ref: `cache <cache>` plugin creates a `` .pytest_cache `` subdirectory
2017-07-20 08:11:17 +08:00
in `` rootdir `` to store its cross-test run state.
2020-01-15 16:43:03 +08:00
`` rootdir `` is **NOT** used to modify `` sys.path `` /`` PYTHONPATH `` or
2017-07-20 08:11:17 +08:00
influence how modules are imported. See :ref: `pythonpath` for more details.
2020-01-15 16:43:03 +08:00
The `` --rootdir=path `` command-line option can be used to force a specific directory.
2020-06-07 06:17:40 +08:00
Note that contrary to other command-line options, `` --rootdir `` cannot be used with
:confval: `addopts` inside `` pytest.ini `` because the `` rootdir `` is used to *find* `` pytest.ini ``
already.
2018-02-01 16:20:37 +08:00
2017-07-20 08:11:17 +08:00
Finding the `` rootdir ``
~~~~~~~~~~~~~~~~~~~~~~~
2015-02-27 04:56:44 +08:00
Here is the algorithm which finds the rootdir from `` args `` :
2021-04-17 01:38:35 +08:00
- If `` -c `` is passed in the command-line, use that as configuration file, and its directory as `` rootdir `` .
2020-06-08 21:03:10 +08:00
- Determine the common ancestor directory for the specified `` args `` that are
2016-06-22 03:48:59 +08:00
recognised as paths that exist in the file system. If no such paths are
found, the common ancestor directory is set to the current working directory.
2015-02-27 04:56:44 +08:00
2020-06-08 21:03:10 +08:00
- Look for `` pytest.ini `` , `` pyproject.toml `` , `` tox.ini `` , and `` setup.cfg `` files in the ancestor
directory and upwards. If one is matched, it becomes the `` configfile `` and its
directory becomes the `` rootdir `` .
2015-02-27 04:56:44 +08:00
2020-06-08 21:03:10 +08:00
- If no configuration file was found, look for `` setup.py `` upwards from the common
2016-06-22 03:48:59 +08:00
ancestor directory to determine the `` rootdir `` .
2015-02-27 04:56:44 +08:00
2020-06-08 21:03:10 +08:00
- If no `` setup.py `` was found, look for `` pytest.ini `` , `` pyproject.toml `` , `` tox.ini `` , and
2016-06-22 03:48:59 +08:00
`` setup.cfg `` in each of the specified `` args `` and upwards. If one is
2020-06-08 21:03:10 +08:00
matched, it becomes the `` configfile `` and its directory becomes the `` rootdir `` .
2015-02-27 04:56:44 +08:00
2021-04-17 01:38:35 +08:00
- If no `` configfile `` was found and no configuration argument is passed, use the already determined common ancestor as root
2016-09-16 05:10:57 +08:00
directory. This allows the use of pytest in structures that are not part of
2020-06-08 21:03:10 +08:00
a package and don't have any particular configuration file.
2016-06-22 03:48:59 +08:00
2016-08-19 15:01:12 +08:00
If no `` args `` are given, pytest collects test below the current working
2020-06-08 21:03:10 +08:00
directory and also starts determining the `` rootdir `` from there.
2016-08-19 15:01:12 +08:00
2020-06-08 21:03:10 +08:00
Files will only be matched for configuration if:
2016-08-19 15:01:12 +08:00
2020-06-08 21:03:10 +08:00
* `` pytest.ini `` : will always match and take precedence, even if empty.
* `` pyproject.toml `` : contains a `` [tool.pytest.ini_options] `` table.
* `` tox.ini `` : contains a `` [pytest] `` section.
* `` setup.cfg `` : contains a `` [tool:pytest] `` section.
2015-02-27 04:56:44 +08:00
2024-02-15 03:08:45 +08:00
Finally, a `` pyproject.toml `` file will be considered the `` configfile `` if no other match was found, in this case
even if it does not contain a `` [tool.pytest.ini_options] `` table (this was added in `` 8.1 `` ).
2020-06-08 21:03:10 +08:00
The files are considered in the order above. Options from multiple `` configfiles `` candidates
are never merged - the first match wins.
2024-02-15 03:08:45 +08:00
The configuration file also determines the value of the `` rootpath `` .
2021-05-24 16:45:07 +08:00
The :class: `Config <pytest.Config>` object (accessible via hooks or through the :fixture: `pytestconfig` fixture)
2020-06-08 21:03:10 +08:00
will subsequently carry these attributes:
2010-11-07 23:10:22 +08:00
2024-02-15 03:08:45 +08:00
- :attr: `config.rootpath <pytest.Config.rootpath>` : the determined root directory, guaranteed to exist. It is used as
a reference directory for constructing test addresses ("nodeids") and can be used also by plugins for storing
per-testrun information.
2010-10-28 01:35:27 +08:00
2021-05-24 16:45:07 +08:00
- :attr: `config.inipath <pytest.Config.inipath>` : the determined `` configfile `` , may be `` None ``
2020-08-06 19:37:19 +08:00
(it is named `` inipath `` for historical reasons).
.. versionadded :: 6.1
The `` config.rootpath `` and `` config.inipath `` properties. They are :class: `pathlib.Path`
versions of the older `` config.rootdir `` and `` config.inifile `` , which have type
`` py.path.local `` , and still exist for backward compatibility.
2010-10-28 01:35:27 +08:00
2024-02-15 03:08:45 +08:00
2010-10-28 01:35:27 +08:00
2019-02-15 21:10:37 +08:00
Example:
.. code-block :: bash
2015-02-27 04:56:44 +08:00
2016-06-21 22:16:57 +08:00
pytest path/to/testdir path/other/
2015-02-27 04:56:44 +08:00
will determine the common ancestor as `` path `` and then
2020-06-08 21:03:10 +08:00
check for configuration files as follows:
2019-02-15 21:10:37 +08:00
.. code-block :: text
2015-02-27 04:56:44 +08:00
# first look for pytest.ini files
path/pytest.ini
2020-06-08 21:03:10 +08:00
path/pyproject.toml # must contain a [tool.pytest.ini_options] table to match
path/tox.ini # must contain [pytest] section to match
path/setup.cfg # must contain [tool:pytest] section to match
2015-02-27 04:56:44 +08:00
pytest.ini
2020-06-08 21:03:10 +08:00
... # all the way up to the root
2015-02-27 04:56:44 +08:00
# now look for setup.py
path/setup.py
setup.py
2020-06-08 21:03:10 +08:00
... # all the way up to the root
2016-09-16 01:46:15 +08:00
2010-11-25 19:11:10 +08:00
2020-06-08 21:03:10 +08:00
.. warning ::
2015-07-10 08:50:38 +08:00
2020-06-08 21:03:10 +08:00
Custom pytest plugin commandline arguments may include a path, as in
`` pytest --log-output ../../test.log args `` . Then `` args `` is mandatory,
otherwise pytest uses the folder of test.log for rootdir determination
2021-11-06 17:16:11 +08:00
(see also :issue: `1435` ).
2020-06-08 21:03:10 +08:00
A dot `` . `` for referencing to the current working directory is also
possible.
2017-07-19 02:37:01 +08:00
2020-06-08 21:03:10 +08:00
.. _`how to change command line options defaults`:
.. _`adding default options`:
2015-02-27 04:56:44 +08:00
2011-09-06 17:43:42 +08:00
Builtin configuration file options
2010-11-01 00:41:58 +08:00
----------------------------------------------
2010-10-28 01:35:27 +08:00
2018-03-08 07:45:41 +08:00
For the full list of options consult the :ref: `reference documentation <ini options ref>` .
2021-06-11 23:04:27 +08:00
Syntax highlighting theme customization
---------------------------------------
The syntax highlighting themes used by pytest can be customized using two environment variables:
- :envvar: `PYTEST_THEME` sets a `pygment style <https://pygments.org/docs/styles/> `_ to use.
- :envvar: `PYTEST_THEME_MODE` sets this style to *light* or *dark* .