Skip to content

Commit 7672daf

Browse files
committed
config: add native TOML configuration
Add support for using native TOML configuration, while maintaining full backwards compatibility with the existing INI-based configuration system. In pyproject.toml, the native configuration is under ``[pytest.tool]``. Also add support for ``pytest.toml``/``.pytest.toml`` files. `--override-ini` always uses "ini" mode for compatibility.
1 parent 147467d commit 7672daf

26 files changed

+1170
-35
lines changed

changelog/13743.feature.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Added support for native TOML configuration files.
2+
3+
While pytest, since version 6, supports configuration in ``pyproject.toml`` files under ``[tool.pytest.ini_options]``,
4+
it does so in an "INI compatibility mode", where all configuration values are treated as strings or list of strings.
5+
Now, pytest supports the native TOML data model.
6+
7+
In ``pyproject.toml``, the native TOML configuration is under the ``[tool.pytest]`` table.
8+
9+
.. code-block:: toml
10+
11+
# pyproject.toml
12+
[tool.pytest]
13+
minversion = "9.0"
14+
addopts = ["-ra", "-q"]
15+
testpaths = [
16+
"tests",
17+
"integration",
18+
]
19+
20+
The ``[tool.pytest.ini_options]`` table remains supported, but both tables cannot be used at the same time.
21+
22+
If you prefer to use a separate configuration file, or don't use ``pyproject.toml``, you can use ``pytest.toml`` or ``.pytest.toml``:
23+
24+
.. code-block:: toml
25+
26+
# pytest.toml or .pytest.toml
27+
[pytest]
28+
minversion = "9.0"
29+
addopts = ["-ra", "-q"]
30+
testpaths = [
31+
"tests",
32+
"integration",
33+
]
34+
35+
The documentation now shows configuration snippets in both TOML and INI formats, in a tabbed interface.
36+
37+
See :ref:`config file formats` for full details.

doc/en/deprecations.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,13 @@ XML file supports it.
847847

848848
To use the new format, update your configuration file:
849849

850+
.. tab:: toml
851+
852+
.. code-block:: toml
853+
854+
[pytest]
855+
junit_family = "xunit2"
856+
850857
.. tab:: ini
851858

852859
.. code-block:: ini
@@ -857,6 +864,13 @@ To use the new format, update your configuration file:
857864
If you discover that your tooling does not support the new format, and want to keep using the
858865
legacy version, set the option to ``legacy`` instead:
859866

867+
.. tab:: toml
868+
869+
.. code-block:: toml
870+
871+
[pytest]
872+
junit_family = "legacy"
873+
860874
.. tab:: ini
861875

862876
.. code-block:: ini

doc/en/example/markers.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,13 @@ Registering markers
239239
240240
Registering markers for your test suite is simple:
241241

242+
.. tab:: toml
243+
244+
.. code-block:: toml
245+
246+
[pytest]
247+
markers = ["webtest: mark a test as a webtest.", "slow: mark test as slow."]
248+
242249
.. tab:: ini
243250

244251
.. code-block:: ini

doc/en/example/pythoncollection.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ Changing directory recursion
9090

9191
You can set the :confval:`norecursedirs` option in a configuration file:
9292

93+
.. tab:: toml
94+
95+
.. code-block:: toml
96+
97+
[pytest]
98+
norecursedirs = [".svn", "_build", "tmp*"]
99+
93100
.. tab:: ini
94101

95102
.. code-block:: ini
@@ -109,6 +116,16 @@ the :confval:`python_files`, :confval:`python_classes` and
109116
:confval:`python_functions` in your :ref:`configuration file <config file formats>`.
110117
Here is an example:
111118

119+
.. tab:: toml
120+
121+
.. code-block:: toml
122+
123+
# Example 1: have pytest look for "check" instead of "test"
124+
[pytest]
125+
python_files = ["check_*.py"]
126+
python_classes = ["Check"]
127+
python_functions = ["*_check"]
128+
112129
.. tab:: ini
113130

114131
.. code-block:: ini
@@ -154,6 +171,14 @@ The test collection would look like this:
154171
155172
You can check for multiple glob patterns by adding a space between the patterns:
156173

174+
.. tab:: toml
175+
176+
.. code-block:: toml
177+
178+
# Example 2: have pytest look for files with "test" and "example"
179+
[pytest]
180+
python_files = ["test_*.py", "example_*.py"]
181+
157182
.. tab:: ini
158183

159184
.. code-block:: ini
@@ -184,6 +209,13 @@ which would run the respective test module. Like with
184209
other options, through a configuration file and the :confval:`addopts` option you
185210
can make this change more permanently:
186211

212+
.. tab:: toml
213+
214+
.. code-block:: toml
215+
216+
[pytest]
217+
addopts = ["--pyargs"]
218+
187219
.. tab:: ini
188220

189221
.. code-block:: ini
@@ -228,6 +260,13 @@ Customizing test collection
228260
229261
You can easily instruct ``pytest`` to discover tests from every Python file:
230262

263+
.. tab:: toml
264+
265+
.. code-block:: toml
266+
267+
[pytest]
268+
python_files = ["*.py"]
269+
231270
.. tab:: ini
232271

233272
.. code-block:: ini

doc/en/example/simple.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ every time you use ``pytest``. For example, if you always want to see
1111
detailed info on skipped and xfailed tests, as well as have terser "dot"
1212
progress output, you can write it into a configuration file:
1313

14+
.. tab:: toml
15+
16+
.. code-block:: toml
17+
18+
[pytest]
19+
addopts = ["-ra", "-q"]
20+
1421
.. tab:: ini
1522

1623
.. code-block:: ini

doc/en/explanation/goodpractices.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ For new projects, we recommend to use ``importlib`` :ref:`import mode <import-mo
9696
(see which-import-mode_ for a detailed explanation).
9797
To this end, add the following to your configuration file:
9898

99+
.. tab:: toml
100+
101+
.. code-block:: toml
102+
103+
[pytest]
104+
addopts = ["--import-mode=importlib"]
105+
99106
.. tab:: ini
100107

101108
.. code-block:: ini
@@ -128,6 +135,13 @@ which are better explained in this excellent `blog post`_ by Ionel Cristian Măr
128135
or in a permanent manner by using the :confval:`pythonpath` configuration variable and adding the
129136
following to your configuration file:
130137

138+
.. tab:: toml
139+
140+
.. code-block:: toml
141+
142+
[pytest]
143+
pythonpath = ["src"]
144+
131145
.. tab:: ini
132146

133147
.. code-block:: ini

doc/en/how-to/capture-warnings.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ The same option can be set in the configuration file using the
8585
user warnings and specific deprecation warnings matching a regex, but will transform
8686
all other warnings into errors.
8787

88+
.. tab:: toml
89+
90+
.. code-block:: toml
91+
92+
[pytest]
93+
filterwarnings = [
94+
"error",
95+
"ignore::UserWarning",
96+
# note the use of single quote below to denote "raw" strings in TOML
97+
'ignore:function ham\(\) is deprecated:DeprecationWarning',
98+
]
99+
88100
.. tab:: ini
89101

90102
.. code-block:: ini
@@ -194,6 +206,13 @@ Disabling warning capture entirely
194206

195207
This plugin is enabled by default but can be disabled entirely in your configuration file with:
196208

209+
.. tab:: toml
210+
211+
.. code-block:: toml
212+
213+
[pytest]
214+
addopts = ["-p", "no:warnings"]
215+
197216
.. tab:: ini
198217

199218
.. code-block:: ini
@@ -224,6 +243,15 @@ those warnings.
224243

225244
For example:
226245

246+
.. tab:: toml
247+
248+
.. code-block:: toml
249+
250+
[pytest]
251+
filterwarnings = [
252+
'ignore:.*U.*mode is deprecated:DeprecationWarning',
253+
]
254+
227255
.. tab:: ini
228256

229257
.. code-block:: ini

doc/en/how-to/doctest.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ and functions, including from test modules:
7070
You can make these changes permanent in your project by
7171
putting them into a configuration file like this:
7272

73+
.. tab:: toml
74+
75+
.. code-block:: toml
76+
77+
[pytest]
78+
addopts = ["--doctest-modules"]
79+
7380
.. tab:: ini
7481

7582
.. code-block:: ini
@@ -85,6 +92,13 @@ The default encoding is **UTF-8**, but you can specify the encoding
8592
that will be used for those doctest files using the
8693
:confval:`doctest_encoding` configuration option:
8794

95+
.. tab:: toml
96+
97+
.. code-block:: toml
98+
99+
[pytest]
100+
doctest_encoding = "latin1"
101+
88102
.. tab:: ini
89103

90104
.. code-block:: ini
@@ -104,6 +118,13 @@ configuration file.
104118
For example, to make pytest ignore trailing whitespaces and ignore
105119
lengthy exception stack traces you can just write:
106120

121+
.. tab:: toml
122+
123+
.. code-block:: toml
124+
125+
[pytest]
126+
doctest_optionflags = ["NORMALIZE_WHITESPACE", "IGNORE_EXCEPTION_DETAIL"]
127+
107128
.. tab:: ini
108129

109130
.. code-block:: ini

doc/en/how-to/fixtures.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,6 +1738,13 @@ and you may specify fixture usage at the test module level using :globalvar:`pyt
17381738
It is also possible to put fixtures required by all tests in your project
17391739
into a configuration file:
17401740

1741+
.. tab:: toml
1742+
1743+
.. code-block:: toml
1744+
1745+
[pytest]
1746+
usefixtures = ["cleandir"]
1747+
17411748
.. tab:: ini
17421749

17431750
.. code-block:: ini

doc/en/how-to/logging.rst

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ Shows failed tests like so:
4949
5050
These options can also be customized through a configuration file:
5151

52+
.. tab:: toml
53+
54+
.. code-block:: toml
55+
56+
[pytest]
57+
log_format = "%(asctime)s %(levelname)s %(message)s"
58+
log_date_format = "%Y-%m-%d %H:%M:%S"
59+
5260
.. tab:: ini
5361

5462
.. code-block:: ini
@@ -270,6 +278,13 @@ has been dropped when this feature was introduced, so if for that reason you
270278
still need ``pytest-catchlog`` you can disable the internal feature by
271279
adding to your configuration file:
272280

281+
.. tab:: toml
282+
283+
.. code-block:: toml
284+
285+
[pytest]
286+
addopts = ["-p", "no:logging"]
287+
273288
.. tab:: ini
274289

275290
.. code-block:: ini
@@ -300,6 +315,14 @@ made in ``3.4`` after community feedback:
300315
If you want to partially restore the logging behavior of version ``3.3``, you can add this options to your configuration
301316
file:
302317

318+
.. tab:: toml
319+
320+
.. code-block:: toml
321+
322+
[pytest]
323+
log_cli = true
324+
log_level = "NOTSET"
325+
303326
.. tab:: ini
304327

305328
.. code-block:: ini

0 commit comments

Comments
 (0)