Command line options and configuration file settings¶
You can get help on command line options and values in INI-style configurations files by using the general help option:
pytest -h # prints options _and_ config file settings
This will display command line and configuration file settings which were registered by installed plugins.
Configuration file formats¶
Many pytest settings can be set in a configuration file, which by convention resides on the root of your repository or in your tests folder.
A quick example of the configuration files supported by pytest:
pytest.ini files take precedence over other files, even when empty.
# pytest.ini [pytest] minversion = 6.0 addopts = -ra -q testpaths = tests integration
New in version 6.0.
pyproject.toml are considered for configuration when they contain a
# pyproject.toml [tool.pytest.ini_options] minversion = "6.0" addopts = "-ra -q" testpaths = [ "tests", "integration", ]
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.
ini_options table is being used, for now, as a bridge between the existing
.ini configuration system and the future configuration format.
tox.ini files are the configuration files of the tox project,
and can also be used to hold pytest configuration if they have a
# tox.ini [pytest] minversion = 6.0 addopts = -ra -q testpaths = tests integration
setup.cfg files are general purpose configuration files, used originally by distutils, and can also be used to hold pytest configuration
if they have a
# setup.cfg [tool:pytest] minversion = 6.0 addopts = -ra -q testpaths = tests integration
setup.cfg is not recommended unless for very simple use cases.
files use a different parser than
tox.ini which might cause hard to track
When possible, it is recommended to use the latter files, or
pyproject.toml, to hold your
Initialization: determining rootdir and configfile¶
pytest determines a
rootdir for each test run which depends on
the command line arguments (specified test files, paths) and on
the existence of configuration files. The determined
printed as part of the pytest header during startup.
Here’s a summary what
Construct nodeids during collection; each test is assigned a unique nodeid which is rooted at the
rootdirand takes into account the full path, class name, function name and parametrization (if any).
Is used by plugins as a stable location to store project/test run specific information; for example, the internal cache plugin creates a
rootdirto store its cross-test run state.
rootdir is NOT used to modify
influence how modules are imported. See pytest import mechanisms and sys.path/PYTHONPATH for more details.
--rootdir=path command-line option can be used to force a specific directory.
Note that contrary to other command-line options,
--rootdir cannot be used with
pytest.ini because the
rootdir is used to find
Here is the algorithm which finds the rootdir from
Determine the common ancestor directory for the specified
argsthat are 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.
setup.cfgfiles in the ancestor directory and upwards. If one is matched, it becomes the
configfileand its directory becomes the
If no configuration file was found, look for
setup.pyupwards from the common ancestor directory to determine the
setup.pywas found, look for
setup.cfgin each of the specified
argsand upwards. If one is matched, it becomes the
configfileand its directory becomes the
configfilewas found, use the already determined common ancestor as root directory. This allows the use of pytest in structures that are not part of a package and don’t have any particular configuration file.
args are given, pytest collects test below the current working
directory and also starts determining the
rootdir from there.
Files will only be matched for configuration if:
pytest.ini: will always match and take precedence, even if empty.
pyproject.toml: contains a
tox.ini: contains a
setup.cfg: contains a
The files are considered in the order above. Options from multiple
are never merged - the first match wins.
Config object (accessible via hooks or through the
will subsequently carry these attributes:
config.rootpath: the determined root directory, guaranteed to exist.
config.inipath: the determined
configfile, may be
None(it is named
inipathfor historical reasons).
New in version 6.1: The
config.inipath properties. They are
versions of the older
config.inifile, which have type
py.path.local, and still exist for backward compatibility.
rootdir is used as a reference directory for constructing test
addresses (“nodeids”) and can be used also by plugins for storing
pytest path/to/testdir path/other/
will determine the common ancestor as
path and then
check for configuration files as follows:
# first look for pytest.ini files path/pytest.ini 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 pytest.ini ... # all the way up to the root # now look for setup.py path/setup.py setup.py ... # all the way up to the root
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
(see also issue 1435).
. for referencing to the current working directory is also
Builtin configuration file options¶
For the full list of options consult the reference documentation.