Skip to content

Commit c17d508

Browse files
authored
Add pyproject.toml support (#7247)
1 parent ceac673 commit c17d508

16 files changed

+609
-236
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ doc/*/_changelog_towncrier_draft.rst
2929
build/
3030
dist/
3131
*.egg-info
32+
htmlcov/
3233
issue/
3334
env/
3435
.env/

changelog/1556.feature.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
pytest now supports ``pyproject.toml`` files for configuration.
2+
3+
The configuration options is similar to the one available in other formats, but must be defined
4+
in a ``[tool.pytest.ini_options]`` table to be picked up by pytest:
5+
6+
.. code-block:: toml
7+
8+
# pyproject.toml
9+
[tool.pytest.ini_options]
10+
minversion = "6.0"
11+
addopts = "-ra -q"
12+
testpaths = [
13+
"tests",
14+
"integration",
15+
]
16+
17+
More information can be found `in the docs <https://docs.pytest.org/en/stable/customize.html#configuration-file-formats>`__.

doc/en/customize.rst

Lines changed: 139 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,112 @@ configurations files by using the general help option:
1414
This will display command line and configuration file settings
1515
which were registered by installed plugins.
1616

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 on the root of your repository or in your
24+
tests folder.
25+
26+
A quick example of the configuration files supported by pytest:
27+
28+
pytest.ini
29+
~~~~~~~~~~
30+
31+
``pytest.ini`` files take precedence over other files, even when empty.
32+
33+
.. code-block:: ini
34+
35+
# pytest.ini
36+
[pytest]
37+
minversion = 6.0
38+
addopts = -ra -q
39+
testpaths =
40+
tests
41+
integration
42+
43+
44+
pyproject.toml
45+
~~~~~~~~~~~~~~
46+
47+
.. versionadded:: 6.0
48+
49+
``pyproject.toml`` are considered for configuration when they contain a ``tool.pytest.ini_options`` table.
50+
51+
.. code-block:: toml
52+
53+
# pyproject.toml
54+
[tool.pytest.ini_options]
55+
minversion = "6.0"
56+
addopts = "-ra -q"
57+
testpaths = [
58+
"tests",
59+
"integration",
60+
]
61+
62+
.. note::
63+
64+
One might wonder why ``[tool.pytest.ini_options]`` instead of ``[tool.pytest]`` as is the
65+
case with other tools.
66+
67+
The reason is that the pytest team intends to fully utilize the rich TOML data format
68+
for configuration in the future, reserving the ``[tool.pytest]`` table for that.
69+
The ``ini_options`` table is being used, for now, as a bridge between the existing
70+
``.ini`` configuration system and the future configuration format.
71+
72+
tox.ini
73+
~~~~~~~
74+
75+
``tox.ini`` files are the configuration files of the `tox <https://tox.readthedocs.io>`__ project,
76+
and can also be used to hold pytest configuration if they have a ``[pytest]`` section.
77+
78+
.. code-block:: ini
79+
80+
# tox.ini
81+
[pytest]
82+
minversion = 6.0
83+
addopts = -ra -q
84+
testpaths =
85+
tests
86+
integration
87+
88+
89+
setup.cfg
90+
~~~~~~~~~
91+
92+
``setup.cfg`` files are general purpose configuration files, used originally by `distutils <https://docs.python.org/3/distutils/configfile.html>`__, and can also be used to hold pytest configuration
93+
if they have a ``[tool:pytest]`` section.
94+
95+
.. code-block:: ini
96+
97+
# setup.cfg
98+
[tool:pytest]
99+
minversion = 6.0
100+
addopts = -ra -q
101+
testpaths =
102+
tests
103+
integration
104+
105+
.. warning::
106+
107+
Usage of ``setup.cfg`` is not recommended unless for very simple use cases. ``.cfg``
108+
files use a different parser than ``pytest.ini`` and ``tox.ini`` which might cause hard to track
109+
down problems.
110+
When possible, it is recommended to use the latter files, or ``pyproject.toml``, to hold your
111+
pytest configuration.
112+
113+
17114
.. _rootdir:
18-
.. _inifiles:
115+
.. _configfiles:
19116

20-
Initialization: determining rootdir and inifile
21-
-----------------------------------------------
117+
Initialization: determining rootdir and configfile
118+
--------------------------------------------------
22119

23120
pytest determines a ``rootdir`` for each test run which depends on
24121
the command line arguments (specified test files, paths) and on
25-
the existence of *ini-files*. The determined ``rootdir`` and *ini-file* are
122+
the existence of configuration files. The determined ``rootdir`` and ``configfile`` are
26123
printed as part of the pytest header during startup.
27124

28125
Here's a summary what ``pytest`` uses ``rootdir`` for:
@@ -48,48 +145,47 @@ Finding the ``rootdir``
48145

49146
Here is the algorithm which finds the rootdir from ``args``:
50147

51-
- determine the common ancestor directory for the specified ``args`` that are
148+
- Determine the common ancestor directory for the specified ``args`` that are
52149
recognised as paths that exist in the file system. If no such paths are
53150
found, the common ancestor directory is set to the current working directory.
54151

55-
- look for ``pytest.ini``, ``tox.ini`` and ``setup.cfg`` files in the ancestor
56-
directory and upwards. If one is matched, it becomes the ini-file and its
57-
directory becomes the rootdir.
152+
- Look for ``pytest.ini``, ``pyproject.toml``, ``tox.ini``, and ``setup.cfg`` files in the ancestor
153+
directory and upwards. If one is matched, it becomes the ``configfile`` and its
154+
directory becomes the ``rootdir``.
58155

59-
- if no ini-file was found, look for ``setup.py`` upwards from the common
156+
- If no configuration file was found, look for ``setup.py`` upwards from the common
60157
ancestor directory to determine the ``rootdir``.
61158

62-
- if no ``setup.py`` was found, look for ``pytest.ini``, ``tox.ini`` and
159+
- If no ``setup.py`` was found, look for ``pytest.ini``, ``pyproject.toml``, ``tox.ini``, and
63160
``setup.cfg`` in each of the specified ``args`` and upwards. If one is
64-
matched, it becomes the ini-file and its directory becomes the rootdir.
161+
matched, it becomes the ``configfile`` and its directory becomes the ``rootdir``.
65162

66-
- if no ini-file was found, use the already determined common ancestor as root
163+
- If no ``configfile`` was found, use the already determined common ancestor as root
67164
directory. This allows the use of pytest in structures that are not part of
68-
a package and don't have any particular ini-file configuration.
165+
a package and don't have any particular configuration file.
69166

70167
If no ``args`` are given, pytest collects test below the current working
71-
directory and also starts determining the rootdir from there.
168+
directory and also starts determining the ``rootdir`` from there.
72169

73-
:warning: custom pytest plugin commandline arguments may include a path, as in
74-
``pytest --log-output ../../test.log args``. Then ``args`` is mandatory,
75-
otherwise pytest uses the folder of test.log for rootdir determination
76-
(see also `issue 1435 <https://github.com/pytest-dev/pytest/issues/1435>`_).
77-
A dot ``.`` for referencing to the current working directory is also
78-
possible.
170+
Files will only be matched for configuration if:
79171

80-
Note that an existing ``pytest.ini`` file will always be considered a match,
81-
whereas ``tox.ini`` and ``setup.cfg`` will only match if they contain a
82-
``[pytest]`` or ``[tool:pytest]`` section, respectively. Options from multiple ini-files candidates are never
83-
merged - the first one wins (``pytest.ini`` always wins, even if it does not
84-
contain a ``[pytest]`` section).
172+
* ``pytest.ini``: will always match and take precedence, even if empty.
173+
* ``pyproject.toml``: contains a ``[tool.pytest.ini_options]`` table.
174+
* ``tox.ini``: contains a ``[pytest]`` section.
175+
* ``setup.cfg``: contains a ``[tool:pytest]`` section.
85176

86-
The ``config`` object will subsequently carry these attributes:
177+
The files are considered in the order above. Options from multiple ``configfiles`` candidates
178+
are never merged - the first match wins.
179+
180+
The internal :class:`Config <_pytest.config.Config>` object (accessible via hooks or through the :fixture:`pytestconfig` fixture)
181+
will subsequently carry these attributes:
87182

88183
- ``config.rootdir``: the determined root directory, guaranteed to exist.
89184

90-
- ``config.inifile``: the determined ini-file, may be ``None``.
185+
- ``config.inifile``: the determined ``configfile``, may be ``None`` (it is named ``inifile``
186+
for historical reasons).
91187

92-
The rootdir is used as a reference directory for constructing test
188+
The ``rootdir`` is used as a reference directory for constructing test
93189
addresses ("nodeids") and can be used also by plugins for storing
94190
per-testrun information.
95191

@@ -100,73 +196,36 @@ Example:
100196
pytest path/to/testdir path/other/
101197
102198
will determine the common ancestor as ``path`` and then
103-
check for ini-files as follows:
199+
check for configuration files as follows:
104200

105201
.. code-block:: text
106202
107203
# first look for pytest.ini files
108204
path/pytest.ini
109-
path/tox.ini # must also contain [pytest] section to match
110-
path/setup.cfg # must also contain [tool:pytest] section to match
205+
path/pyproject.toml # must contain a [tool.pytest.ini_options] table to match
206+
path/tox.ini # must contain [pytest] section to match
207+
path/setup.cfg # must contain [tool:pytest] section to match
111208
pytest.ini
112-
... # all the way down to the root
209+
... # all the way up to the root
113210
114211
# now look for setup.py
115212
path/setup.py
116213
setup.py
117-
... # all the way down to the root
118-
119-
120-
.. _`how to change command line options defaults`:
121-
.. _`adding default options`:
122-
123-
214+
... # all the way up to the root
124215
125-
How to change command line options defaults
126-
------------------------------------------------
127216
128-
It can be tedious to type the same series of command line options
129-
every time you use ``pytest``. For example, if you always want to see
130-
detailed info on skipped and xfailed tests, as well as have terser "dot"
131-
progress output, you can write it into a configuration file:
217+
.. warning::
132218

133-
.. code-block:: ini
134-
135-
# content of pytest.ini or tox.ini
136-
[pytest]
137-
addopts = -ra -q
138-
139-
# content of setup.cfg
140-
[tool:pytest]
141-
addopts = -ra -q
142-
143-
Alternatively, you can set a ``PYTEST_ADDOPTS`` environment variable to add command
144-
line options while the environment is in use:
145-
146-
.. code-block:: bash
147-
148-
export PYTEST_ADDOPTS="-v"
149-
150-
Here's how the command-line is built in the presence of ``addopts`` or the environment variable:
151-
152-
.. code-block:: text
153-
154-
<pytest.ini:addopts> $PYTEST_ADDOPTS <extra command-line arguments>
155-
156-
So if the user executes in the command-line:
157-
158-
.. code-block:: bash
159-
160-
pytest -m slow
161-
162-
The actual command line executed is:
163-
164-
.. code-block:: bash
219+
Custom pytest plugin commandline arguments may include a path, as in
220+
``pytest --log-output ../../test.log args``. Then ``args`` is mandatory,
221+
otherwise pytest uses the folder of test.log for rootdir determination
222+
(see also `issue 1435 <https://github.com/pytest-dev/pytest/issues/1435>`_).
223+
A dot ``.`` for referencing to the current working directory is also
224+
possible.
165225

166-
pytest -ra -q -v -m slow
167226

168-
Note that as usual for other command-line applications, in case of conflicting options the last one wins, so the example
169-
above will show verbose output because ``-v`` overwrites ``-q``.
227+
.. _`how to change command line options defaults`:
228+
.. _`adding default options`:
170229

171230

172231
Builtin configuration file options

doc/en/example/pythoncollection.rst

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,13 @@ Changing naming conventions
115115

116116
You can configure different naming conventions by setting
117117
the :confval:`python_files`, :confval:`python_classes` and
118-
:confval:`python_functions` configuration options.
118+
:confval:`python_functions` in your :ref:`configuration file <config file formats>`.
119119
Here is an example:
120120

121121
.. code-block:: ini
122122
123123
# content of pytest.ini
124124
# Example 1: have pytest look for "check" instead of "test"
125-
# can also be defined in tox.ini or setup.cfg file, although the section
126-
# name in setup.cfg files should be "tool:pytest"
127125
[pytest]
128126
python_files = check_*.py
129127
python_classes = Check
@@ -165,8 +163,7 @@ You can check for multiple glob patterns by adding a space between the patterns:
165163
.. code-block:: ini
166164
167165
# Example 2: have pytest look for files with "test" and "example"
168-
# content of pytest.ini, tox.ini, or setup.cfg file (replace "pytest"
169-
# with "tool:pytest" for setup.cfg)
166+
# content of pytest.ini
170167
[pytest]
171168
python_files = test_*.py example_*.py
172169

doc/en/example/simple.rst

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,50 @@
33
Basic patterns and examples
44
==========================================================
55

6+
How to change command line options defaults
7+
-------------------------------------------
8+
9+
It can be tedious to type the same series of command line options
10+
every time you use ``pytest``. For example, if you always want to see
11+
detailed info on skipped and xfailed tests, as well as have terser "dot"
12+
progress output, you can write it into a configuration file:
13+
14+
.. code-block:: ini
15+
16+
# content of pytest.ini
17+
[pytest]
18+
addopts = -ra -q
19+
20+
21+
Alternatively, you can set a ``PYTEST_ADDOPTS`` environment variable to add command
22+
line options while the environment is in use:
23+
24+
.. code-block:: bash
25+
26+
export PYTEST_ADDOPTS="-v"
27+
28+
Here's how the command-line is built in the presence of ``addopts`` or the environment variable:
29+
30+
.. code-block:: text
31+
32+
<pytest.ini:addopts> $PYTEST_ADDOPTS <extra command-line arguments>
33+
34+
So if the user executes in the command-line:
35+
36+
.. code-block:: bash
37+
38+
pytest -m slow
39+
40+
The actual command line executed is:
41+
42+
.. code-block:: bash
43+
44+
pytest -ra -q -v -m slow
45+
46+
Note that as usual for other command-line applications, in case of conflicting options the last one wins, so the example
47+
above will show verbose output because ``-v`` overwrites ``-q``.
48+
49+
650
.. _request example:
751

852
Pass different values to a test function, depending on command line options

0 commit comments

Comments
 (0)