Skip to content

Commit ae2d84a

Browse files
committed
Add docs for pyproject.toml
1 parent 293bf9f commit ae2d84a

File tree

5 files changed

+179
-92
lines changed

5 files changed

+179
-92
lines changed

doc/en/customize.rst

Lines changed: 126 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,102 @@ 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+
tox.ini
63+
~~~~~~~
64+
65+
``tox.ini`` files are the configuration files of the `tox <https://tox.readthedocs.io>`__ project,
66+
and can also be used to hold pytest configuration if they have a ``[pytest]`` section.
67+
68+
.. code-block:: ini
69+
70+
# tox.ini
71+
[pytest]
72+
minversion = 6.0
73+
addopts = -ra -q
74+
testpaths =
75+
tests
76+
integration
77+
78+
79+
setup.cfg
80+
~~~~~~~~~
81+
82+
``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
83+
if they have a ``[tool:pytest]`` section.
84+
85+
.. code-block:: ini
86+
87+
# setup.cfg
88+
[tool:pytest]
89+
minversion = 6.0
90+
addopts = -ra -q
91+
testpaths =
92+
tests
93+
integration
94+
95+
.. warning::
96+
97+
Usage of ``setup.cfg`` is not recommended unless for very simple use cases. ``.cfg``
98+
files use a different parser than ``pytest.ini`` and ``tox.ini`` which might cause hard to track
99+
down problems.
100+
When possible, it is recommended to use the latter files, or ``pyproject.toml``, to hold your
101+
pytest configuration.
102+
103+
17104
.. _rootdir:
18-
.. _inifiles:
105+
.. _configfiles:
19106

20-
Initialization: determining rootdir and inifile
21-
-----------------------------------------------
107+
Initialization: determining rootdir and configfile
108+
--------------------------------------------------
22109

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

28115
Here's a summary what ``pytest`` uses ``rootdir`` for:
@@ -47,48 +134,46 @@ Finding the ``rootdir``
47134

48135
Here is the algorithm which finds the rootdir from ``args``:
49136

50-
- determine the common ancestor directory for the specified ``args`` that are
137+
- Determine the common ancestor directory for the specified ``args`` that are
51138
recognised as paths that exist in the file system. If no such paths are
52139
found, the common ancestor directory is set to the current working directory.
53140

54-
- look for ``pytest.ini``, ``tox.ini`` and ``setup.cfg`` files in the ancestor
55-
directory and upwards. If one is matched, it becomes the ini-file and its
56-
directory becomes the rootdir.
141+
- Look for ``pytest.ini``, ``pyproject.toml``, ``tox.ini``, and ``setup.cfg`` files in the ancestor
142+
directory and upwards. If one is matched, it becomes the ``configfile`` and its
143+
directory becomes the ``rootdir``.
57144

58-
- if no ini-file was found, look for ``setup.py`` upwards from the common
145+
- If no configuration file was found, look for ``setup.py`` upwards from the common
59146
ancestor directory to determine the ``rootdir``.
60147

61-
- if no ``setup.py`` was found, look for ``pytest.ini``, ``tox.ini`` and
148+
- If no ``setup.py`` was found, look for ``pytest.ini``, ``pyproject.toml``, ``tox.ini``, and
62149
``setup.cfg`` in each of the specified ``args`` and upwards. If one is
63-
matched, it becomes the ini-file and its directory becomes the rootdir.
150+
matched, it becomes the ``configfile`` and its directory becomes the ``rootdir``.
64151

65-
- if no ini-file was found, use the already determined common ancestor as root
152+
- If no ``configfile`` was found, use the already determined common ancestor as root
66153
directory. This allows the use of pytest in structures that are not part of
67-
a package and don't have any particular ini-file configuration.
154+
a package and don't have any particular configuration file.
68155

69156
If no ``args`` are given, pytest collects test below the current working
70-
directory and also starts determining the rootdir from there.
157+
directory and also starts determining the ``rootdir`` from there.
71158

72-
:warning: custom pytest plugin commandline arguments may include a path, as in
73-
``pytest --log-output ../../test.log args``. Then ``args`` is mandatory,
74-
otherwise pytest uses the folder of test.log for rootdir determination
75-
(see also `issue 1435 <https://github.com/pytest-dev/pytest/issues/1435>`_).
76-
A dot ``.`` for referencing to the current working directory is also
77-
possible.
159+
Files will only be matched for configuration if:
160+
161+
* ``pytest.ini``: will always match and take precedence, even if empty.
162+
* ``pyproject.toml``: contains a ``[tool.pytest.ini_options]`` table.
163+
* ``tox.ini``: contains a ``[pytest]`` section.
164+
* ``setup.cfg``: contains a ``[tool:pytest]`` section.
78165

79-
Note that an existing ``pytest.ini`` file will always be considered a match,
80-
whereas ``tox.ini`` and ``setup.cfg`` will only match if they contain a
81-
``[pytest]`` or ``[tool:pytest]`` section, respectively. Options from multiple ini-files candidates are never
82-
merged - the first one wins (``pytest.ini`` always wins, even if it does not
166+
Options from multiple ``configfiles`` candidates are never merged - the first one wins (``pytest.ini`` always wins, even if it does not
83167
contain a ``[pytest]`` section).
84168

85169
The ``config`` object will subsequently carry these attributes:
86170

87171
- ``config.rootdir``: the determined root directory, guaranteed to exist.
88172

89-
- ``config.inifile``: the determined ini-file, may be ``None``.
173+
- ``config.inifile``: the determined ``configfile``, may be ``None`` (it is named ``inifile``
174+
for historical reasons).
90175

91-
The rootdir is used as a reference directory for constructing test
176+
The ``rootdir`` is used as a reference directory for constructing test
92177
addresses ("nodeids") and can be used also by plugins for storing
93178
per-testrun information.
94179

@@ -99,73 +184,36 @@ Example:
99184
pytest path/to/testdir path/other/
100185
101186
will determine the common ancestor as ``path`` and then
102-
check for ini-files as follows:
187+
check for configuration files as follows:
103188

104189
.. code-block:: text
105190
106191
# first look for pytest.ini files
107192
path/pytest.ini
108-
path/tox.ini # must also contain [pytest] section to match
109-
path/setup.cfg # must also contain [tool:pytest] section to match
193+
path/pyproject.toml # must contain a [tool.pytest.ini_options] table to match
194+
path/tox.ini # must contain [pytest] section to match
195+
path/setup.cfg # must contain [tool:pytest] section to match
110196
pytest.ini
111-
... # all the way down to the root
197+
... # all the way up to the root
112198
113199
# now look for setup.py
114200
path/setup.py
115201
setup.py
116-
... # all the way down to the root
117-
118-
119-
.. _`how to change command line options defaults`:
120-
.. _`adding default options`:
121-
122-
123-
124-
How to change command line options defaults
125-
------------------------------------------------
126-
127-
It can be tedious to type the same series of command line options
128-
every time you use ``pytest``. For example, if you always want to see
129-
detailed info on skipped and xfailed tests, as well as have terser "dot"
130-
progress output, you can write it into a configuration file:
131-
132-
.. code-block:: ini
133-
134-
# content of pytest.ini or tox.ini
135-
[pytest]
136-
addopts = -ra -q
137-
138-
# content of setup.cfg
139-
[tool:pytest]
140-
addopts = -ra -q
202+
... # all the way up to the root
141203
142-
Alternatively, you can set a ``PYTEST_ADDOPTS`` environment variable to add command
143-
line options while the environment is in use:
144204
145-
.. code-block:: bash
146-
147-
export PYTEST_ADDOPTS="-v"
148-
149-
Here's how the command-line is built in the presence of ``addopts`` or the environment variable:
150-
151-
.. code-block:: text
152-
153-
<pytest.ini:addopts> $PYTEST_ADDOPTS <extra command-line arguments>
154-
155-
So if the user executes in the command-line:
156-
157-
.. code-block:: bash
158-
159-
pytest -m slow
205+
.. warning::
160206

161-
The actual command line executed is:
162-
163-
.. code-block:: bash
207+
Custom pytest plugin commandline arguments may include a path, as in
208+
``pytest --log-output ../../test.log args``. Then ``args`` is mandatory,
209+
otherwise pytest uses the folder of test.log for rootdir determination
210+
(see also `issue 1435 <https://github.com/pytest-dev/pytest/issues/1435>`_).
211+
A dot ``.`` for referencing to the current working directory is also
212+
possible.
164213

165-
pytest -ra -q -v -m slow
166214

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

170218

171219
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

doc/en/reference.rst

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,17 +1019,17 @@ UsageError
10191019
Configuration Options
10201020
---------------------
10211021

1022-
Here is a list of builtin configuration options that may be written in a ``pytest.ini``, ``tox.ini`` or ``setup.cfg``
1023-
file, usually located at the root of your repository. All options must be under a ``[pytest]`` section
1024-
(``[tool:pytest]`` for ``setup.cfg`` files).
1022+
Here is a list of builtin configuration options that may be written in a ``pytest.ini``, ``pyproject.toml``, ``tox.ini`` or ``setup.cfg``
1023+
file, usually located at the root of your repository. To see each file format in details, see
1024+
:ref:`config file formats`.
10251025

10261026
.. warning::
1027-
Usage of ``setup.cfg`` is not recommended unless for very simple use cases. ``.cfg``
1027+
Usage of ``setup.cfg`` is not recommended except for very simple use cases. ``.cfg``
10281028
files use a different parser than ``pytest.ini`` and ``tox.ini`` which might cause hard to track
10291029
down problems.
1030-
When possible, it is recommended to use the latter files to hold your pytest configuration.
1030+
When possible, it is recommended to use the latter files, or ``pyproject.toml``, to hold your pytest configuration.
10311031

1032-
Configuration file options may be overwritten in the command-line by using ``-o/--override-ini``, which can also be
1032+
Configuration options may be overwritten in the command-line by using ``-o/--override-ini``, which can also be
10331033
passed multiple times. The expected format is ``name=value``. For example::
10341034

10351035
pytest -o console_output_style=classic -o cache_dir=/tmp/mycache
@@ -1057,8 +1057,6 @@ passed multiple times. The expected format is ``name=value``. For example::
10571057

10581058
.. confval:: cache_dir
10591059

1060-
1061-
10621060
Sets a directory where stores content of cache plugin. Default directory is
10631061
``.pytest_cache`` which is created in :ref:`rootdir <rootdir>`. Directory may be
10641062
relative or absolute path. If setting relative path, then directory is created

src/_pytest/config/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ def addinivalue_line(self, name, line):
11131113
x.append(line) # modifies the cached list inline
11141114

11151115
def getini(self, name: str):
1116-
""" return configuration value from an :ref:`ini file <inifiles>`. If the
1116+
""" return configuration value from an :ref:`ini file <configfiles>`. If the
11171117
specified name hasn't been registered through a prior
11181118
:py:func:`parser.addini <_pytest.config.argparsing.Parser.addini>`
11191119
call (usually from a plugin), a ValueError is raised. """

0 commit comments

Comments
 (0)