Skip to content

Commit 147467d

Browse files
committed
config: generalize from INI format
This prepares the code and documentation for adding another configuration file format, TOML. Add a `mode` field to `ConfigValue` to track the parsing mode. Add tabs to all configuration file snippets in the docs, so that we may show both toml and ini in a nice way once toml is added. Uses the sphinx-inline-tabs package that I saw used by tox documentation. Avoid references to "pytest.ini" and "ini file" in docs, instead refer to "configuration file" (I'm sure I missed some).
1 parent cc400d1 commit 147467d

35 files changed

+625
-476
lines changed

doc/en/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"sphinx.ext.todo",
3535
"sphinx.ext.viewcode",
3636
"sphinx_removed_in",
37+
"sphinx_inline_tabs",
3738
"sphinxcontrib_trio",
3839
"sphinxcontrib.towncrier.ext", # provides `towncrier-draft-entries` directive
3940
"sphinx_issues", # implements `:issue:`, `:pr:` and other GH-related roles

doc/en/deprecations.rst

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -845,20 +845,24 @@ that manipulate this type of file (for example, Jenkins, Azure Pipelines, etc.).
845845
Users are recommended to try the new ``xunit2`` format and see if their tooling that consumes the JUnit
846846
XML file supports it.
847847

848-
To use the new format, update your ``pytest.ini``:
848+
To use the new format, update your configuration file:
849849

850-
.. code-block:: ini
850+
.. tab:: ini
851851

852-
[pytest]
853-
junit_family=xunit2
852+
.. code-block:: ini
853+
854+
[pytest]
855+
junit_family = xunit2
854856
855857
If you discover that your tooling does not support the new format, and want to keep using the
856858
legacy version, set the option to ``legacy`` instead:
857859

858-
.. code-block:: ini
860+
.. tab:: ini
861+
862+
.. code-block:: ini
859863
860-
[pytest]
861-
junit_family=legacy
864+
[pytest]
865+
junit_family = legacy
862866
863867
By using ``legacy`` you will keep using the legacy/xunit1 format when upgrading to
864868
pytest 6.0, where the default format will be ``xunit2``.

doc/en/example/markers.rst

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

242-
.. code-block:: ini
242+
.. tab:: ini
243243

244-
# content of pytest.ini
245-
[pytest]
246-
markers =
247-
webtest: mark a test as a webtest.
248-
slow: mark test as slow.
244+
.. code-block:: ini
245+
246+
[pytest]
247+
markers =
248+
webtest: mark a test as a webtest.
249+
slow: mark test as slow.
249250
250251
Multiple custom markers can be registered, by defining each one in its own line, as shown in above example.
251252

doc/en/example/pythoncollection.rst

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,14 @@ Example:
8888
Changing directory recursion
8989
-----------------------------------------------------
9090

91-
You can set the :confval:`norecursedirs` option in an ini-file, for example your ``pytest.ini`` in the project root directory:
91+
You can set the :confval:`norecursedirs` option in a configuration file:
9292

93-
.. code-block:: ini
93+
.. tab:: ini
9494

95-
# content of pytest.ini
96-
[pytest]
97-
norecursedirs = .svn _build tmp*
95+
.. code-block:: ini
96+
97+
[pytest]
98+
norecursedirs = .svn _build tmp*
9899
99100
This would tell ``pytest`` to not recurse into typical subversion or sphinx-build directories or into any ``tmp`` prefixed directory.
100101

@@ -108,14 +109,15 @@ the :confval:`python_files`, :confval:`python_classes` and
108109
:confval:`python_functions` in your :ref:`configuration file <config file formats>`.
109110
Here is an example:
110111

111-
.. code-block:: ini
112+
.. tab:: ini
113+
114+
.. code-block:: ini
112115
113-
# content of pytest.ini
114-
# Example 1: have pytest look for "check" instead of "test"
115-
[pytest]
116-
python_files = check_*.py
117-
python_classes = Check
118-
python_functions = *_check
116+
# Example 1: have pytest look for "check" instead of "test"
117+
[pytest]
118+
python_files = check_*.py
119+
python_classes = Check
120+
python_functions = *_check
119121
120122
This would make ``pytest`` look for tests in files that match the ``check_*
121123
.py`` glob-pattern, ``Check`` prefixes in classes, and functions and methods
@@ -152,12 +154,13 @@ The test collection would look like this:
152154
153155
You can check for multiple glob patterns by adding a space between the patterns:
154156

155-
.. code-block:: ini
157+
.. tab:: ini
158+
159+
.. code-block:: ini
156160
157-
# Example 2: have pytest look for files with "test" and "example"
158-
# content of pytest.ini
159-
[pytest]
160-
python_files = test_*.py example_*.py
161+
# Example 2: have pytest look for files with "test" and "example"
162+
[pytest]
163+
python_files = test_*.py example_*.py
161164
162165
.. note::
163166

@@ -178,14 +181,15 @@ example if you have unittest2 installed you can type:
178181
pytest --pyargs unittest2.test.test_skipping -q
179182
180183
which would run the respective test module. Like with
181-
other options, through an ini-file and the :confval:`addopts` option you
184+
other options, through a configuration file and the :confval:`addopts` option you
182185
can make this change more permanently:
183186

184-
.. code-block:: ini
187+
.. tab:: ini
185188

186-
# content of pytest.ini
187-
[pytest]
188-
addopts = --pyargs
189+
.. code-block:: ini
190+
191+
[pytest]
192+
addopts = --pyargs
189193
190194
Now a simple invocation of ``pytest NAME`` will check
191195
if NAME exists as an importable package/module and otherwise
@@ -224,11 +228,12 @@ Customizing test collection
224228
225229
You can easily instruct ``pytest`` to discover tests from every Python file:
226230

227-
.. code-block:: ini
231+
.. tab:: ini
232+
233+
.. code-block:: ini
228234
229-
# content of pytest.ini
230-
[pytest]
231-
python_files = *.py
235+
[pytest]
236+
python_files = *.py
232237
233238
However, many projects will have a ``setup.py`` which they don't want to be
234239
imported. Moreover, there may files only importable by a specific python

doc/en/example/simple.rst

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ 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-
.. code-block:: ini
14+
.. tab:: ini
1515

16-
# content of pytest.ini
17-
[pytest]
18-
addopts = -ra -q
16+
.. code-block:: ini
17+
18+
[pytest]
19+
addopts = -ra -q
1920
2021
2122
Alternatively, you can set a ``PYTEST_ADDOPTS`` environment variable to add command
@@ -29,7 +30,7 @@ Here's how the command-line is built in the presence of ``addopts`` or the envir
2930

3031
.. code-block:: text
3132
32-
<pytest.ini:addopts> $PYTEST_ADDOPTS <extra command-line arguments>
33+
<configuration file addopts> $PYTEST_ADDOPTS <extra command-line arguments>
3334
3435
So if the user executes in the command-line:
3536

doc/en/explanation/goodpractices.rst

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ This has the following benefits:
9494

9595
For new projects, we recommend to use ``importlib`` :ref:`import mode <import-modes>`
9696
(see which-import-mode_ for a detailed explanation).
97-
To this end, add the following to your ``pyproject.toml``:
97+
To this end, add the following to your configuration file:
9898

99-
.. code-block:: toml
99+
.. tab:: ini
100+
101+
.. code-block:: ini
100102
101-
[tool.pytest.ini_options]
102-
addopts = [
103-
"--import-mode=importlib",
104-
]
103+
[pytest]
104+
addopts = --import-mode=importlib
105105
106106
.. _src-layout:
107107

@@ -126,12 +126,14 @@ which are better explained in this excellent `blog post`_ by Ionel Cristian Măr
126126
PYTHONPATH=src pytest
127127
128128
or in a permanent manner by using the :confval:`pythonpath` configuration variable and adding the
129-
following to your ``pyproject.toml``:
129+
following to your configuration file:
130+
131+
.. tab:: ini
130132

131-
.. code-block:: toml
133+
.. code-block:: ini
132134
133-
[tool.pytest.ini_options]
134-
pythonpath = "src"
135+
[pytest]
136+
pythonpath = src
135137
136138
.. note::
137139

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

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -80,30 +80,20 @@ as an error:
8080
FAILED test_show_warnings.py::test_one - UserWarning: api v1, should use ...
8181
1 failed in 0.12s
8282
83-
The same option can be set in the ``pytest.ini`` or ``pyproject.toml`` file using the
84-
``filterwarnings`` ini option. For example, the configuration below will ignore all
83+
The same option can be set in the configuration file using the
84+
:confval:`filterwarnings` configuration option. For example, the configuration below will ignore all
8585
user warnings and specific deprecation warnings matching a regex, but will transform
8686
all other warnings into errors.
8787

88-
.. code-block:: ini
88+
.. tab:: ini
8989

90-
# pytest.ini
91-
[pytest]
92-
filterwarnings =
93-
error
94-
ignore::UserWarning
95-
ignore:function ham\(\) is deprecated:DeprecationWarning
96-
97-
.. code-block:: toml
90+
.. code-block:: ini
9891
99-
# pyproject.toml
100-
[tool.pytest.ini_options]
101-
filterwarnings = [
102-
"error",
103-
"ignore::UserWarning",
104-
# note the use of single quote below to denote "raw" strings in TOML
105-
'ignore:function ham\(\) is deprecated:DeprecationWarning',
106-
]
92+
[pytest]
93+
filterwarnings =
94+
error
95+
ignore::UserWarning
96+
ignore:function ham\(\) is deprecated:DeprecationWarning
10797
10898
10999
When a warning matches more than one option in the list, the action for the last matching option
@@ -112,7 +102,7 @@ is performed.
112102

113103
.. note::
114104

115-
The ``-W`` flag and the ``filterwarnings`` ini option use warning filters that are
105+
The ``-W`` flag and the :confval:`filterwarnings` configuration option use warning filters that are
116106
similar in structure, but each configuration option interprets its filter
117107
differently. For example, *message* in ``filterwarnings`` is a string containing a
118108
regular expression that the start of the warning message must match,
@@ -169,7 +159,7 @@ You can specify multiple filters with separate decorators:
169159

170160

171161
Filters applied using a mark take precedence over filters passed on the command line or configured
172-
by the :confval:`filterwarnings` ini option.
162+
by the :confval:`filterwarnings` configuration option.
173163

174164
You may apply a filter to all tests of a class by using the :ref:`filterwarnings <pytest.mark.filterwarnings ref>` mark as a class
175165
decorator or to all tests in a module by setting the :globalvar:`pytestmark` variable:
@@ -202,7 +192,9 @@ warning summary entirely from the test run output.
202192
Disabling warning capture entirely
203193
----------------------------------
204194

205-
This plugin is enabled by default but can be disabled entirely in your ``pytest.ini`` file with:
195+
This plugin is enabled by default but can be disabled entirely in your configuration file with:
196+
197+
.. tab:: ini
206198

207199
.. code-block:: ini
208200
@@ -227,16 +219,18 @@ However, in the specific case where users capture any type of warnings in their
227219
no warning will be displayed at all.
228220

229221
Sometimes it is useful to hide some specific deprecation warnings that happen in code that you have no control over
230-
(such as third-party libraries), in which case you might use the warning filters options (ini or marks) to ignore
222+
(such as third-party libraries), in which case you might use the warning filters options (configuration or marks) to ignore
231223
those warnings.
232224

233225
For example:
234226

235-
.. code-block:: ini
227+
.. tab:: ini
236228

237-
[pytest]
238-
filterwarnings =
239-
ignore:.*U.*mode is deprecated:DeprecationWarning
229+
.. code-block:: ini
230+
231+
[pytest]
232+
filterwarnings =
233+
ignore:.*U.*mode is deprecated:DeprecationWarning
240234
241235
242236
This will ignore all warnings of type ``DeprecationWarning`` where the start of the message matches

doc/en/how-to/doctest.rst

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,29 @@ and functions, including from test modules:
6868
============================ 2 passed in 0.12s =============================
6969
7070
You can make these changes permanent in your project by
71-
putting them into a pytest.ini file like this:
71+
putting them into a configuration file like this:
7272

73-
.. code-block:: ini
73+
.. tab:: ini
7474

75-
# content of pytest.ini
76-
[pytest]
77-
addopts = --doctest-modules
75+
.. code-block:: ini
76+
77+
[pytest]
78+
addopts = --doctest-modules
7879
7980
8081
Encoding
8182
--------
8283

8384
The default encoding is **UTF-8**, but you can specify the encoding
8485
that will be used for those doctest files using the
85-
``doctest_encoding`` ini option:
86+
:confval:`doctest_encoding` configuration option:
87+
88+
.. tab:: ini
8689

87-
.. code-block:: ini
90+
.. code-block:: ini
8891
89-
# content of pytest.ini
90-
[pytest]
91-
doctest_encoding = latin1
92+
[pytest]
93+
doctest_encoding = latin1
9294
9395
.. _using doctest options:
9496

@@ -102,10 +104,12 @@ configuration file.
102104
For example, to make pytest ignore trailing whitespaces and ignore
103105
lengthy exception stack traces you can just write:
104106

105-
.. code-block:: ini
107+
.. tab:: ini
108+
109+
.. code-block:: ini
106110
107-
[pytest]
108-
doctest_optionflags = NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL
111+
[pytest]
112+
doctest_optionflags = NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL
109113
110114
Alternatively, options can be enabled by an inline comment in the doc test
111115
itself:

doc/en/how-to/fixtures.rst

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

1741-
.. code-block:: ini
1741+
.. tab:: ini
17421742

1743-
# content of pytest.ini
1744-
[pytest]
1745-
usefixtures = cleandir
1743+
.. code-block:: ini
1744+
1745+
[pytest]
1746+
usefixtures = cleandir
17461747
17471748
17481749
.. warning::

0 commit comments

Comments
 (0)