@@ -8,7 +8,7 @@ don’t have to enter them again and again. In addition to the configuration
88files, there are a handful of other files that are helpful when using pytest to
99make writing and running tests easier:
1010
11- :file: `pytest.ini `
11+ :file: `pyproject.toml `
1212 This is the most important configuration file of pytest, with which you can
1313 change the default behaviour of pytest. It also defines the root directory
1414 of pytest, or ``rootdir ``.
@@ -19,14 +19,14 @@ make writing and running tests easier:
1919 If this file is stored in test subdirectories, it enables the use of
2020 identical test file names in several test directories.
2121
22- If you already have a :file: `tox.ini `, :file: `pyproject.toml ` or
23- :file: ` setup.cfg ` in your project, they can take the place of the
24- :file: `pytest.ini ` file: :file: `tox.ini ` is used by :doc: ` ../tox `,
25- :file: ` pyproject.toml ` and :file: ` setup.cfg ` are used for packaging Python
26- projects and can be used to store settings for various tools, including pytest.
22+ If you already have a :file: `tox.ini `, :file: `pytest.ini `, or :file: ` setup.cfg `
23+ in your project, they can take the place of the configuration in the
24+ :file: `pyproject.toml ` file: :file: `tox.ini ` is used by tox, :file: ` setup.cfg `
25+ is used for packaging Python projects and can be used to store settings for
26+ various tools, including pytest.
2727
28- You should have a configuration file, either :file: `pytest.ini `, or a `` pytest ``
29- section in :file: `tox .ini `, :file: `pyproject.toml ` or in :file: `setup.cfg `.
28+ You should have a configuration file, either in :file: `pyproject.toml `,
29+ :file: `pytest .ini `, :file: `tox.ini `, or :file: `setup.cfg `.
3030
3131The configuration file defines the top-level directory from which ``pytest `` is
3232started.
@@ -39,7 +39,7 @@ structure:
3939
4040 items
4141 ├── …
42- ├── pytest.ini
42+ ├── pyproject.toml
4343 ├── src
4444 │ └── …
4545 └── tests
@@ -48,58 +48,72 @@ structure:
4848 └── test_….py
4949
5050 In the case of the ``items `` project that we have used for testing so far, there
51- is a :file: `pytest.ini ` file and a :file: `tests ` directory at the top level. We
52- will refer to this structure when we talk about the various files in the rest of
53- this section.
51+ is a :file: `pyproject.toml ` file and a :file: `tests ` directory at the top level.
52+ We will refer to this structure when we talk about the various files in the rest
53+ of this section.
5454
55- Saving settings and options in :file: `pytest.ini `
56- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55+ Saving settings and options in :file: `pyproject.toml `
56+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5757
58- .. code-block :: ini
58+ The :file: `pyproject.toml ` file was originally intended for packaging Python
59+ projects; however, it can also be used to define project settings.
5960
60- [pytest]
61- addopts =
62- --strict-markers
63- --strict-config
64- -ra
65- testpaths = tests
66- markers =
67- smoke: Small subset of all tests
68- exception: Only run expected exceptions
61+ .. code-block :: toml
62+
63+ [tool.pytest]
64+ minversion = "9.0"
65+ addopts = [
66+ "--strict-markers",
67+ "--strict-config",
68+ "-ra",
69+ ]
70+ testpaths = [
71+ "tests",
72+ ]
73+ markers = [
74+ "exception: Only run expected exceptions",
75+ "finish: Only run finish tests",
76+ "smoke: Small subset of all tests",
77+ "num_items: Number of items to be pre-filled for the items_db fixture",
78+ ]
6979
70- ``[pytest] `` marks the start of the pytest section. This is followed by the
80+ ``[tool. pytest] `` marks the start of the pytest section. This is followed by the
7181individual settings. For configuration settings that allow more than one value,
7282the values can be written either in one or more lines in the form
73- :samp: `{ SETTING } = { VALUE1 } { VALUE2 } `. With ``markers ``, however, only one
74- marker per line is permitted.
83+ :samp: `:samp:`{ EINSTELLUNG } = ["{ WERT1 } ", "{ WERT2 } "]` `.
7584
76- This example is a simple :file: `pytest.ini ` file that I use in almost all my
77- projects. Let’s briefly go through the individual lines:
85+ This example is a simple pytest configuration in the :file: `pyproject.toml `
86+ file, which I use in almost all of my projects in this form or a similar one.
87+ Let’s briefly go through the individual lines:
88+
89+ ``minversion ``
90+ specifies the minimum pytest version.
7891
7992.. _addopts :
8093
8194``addopts ``
8295 allows you to specify the pytest options that we always want to execute in
8396 this project.
84- ``--strict-markers ``
85- instructs pytest to issue an error instead of a warning for every
86- unregistered marker that appears in the test code. This allows us to avoid
87- typos in marker names.
88- ``--strict-config ``
89- instructs pytest to issue an error instead of a warning if difficulties
90- arise when parsing configuration files. This prevents typing errors in the
91- configuration file from going unnoticed.
92- ``-ra ``
93- instructs pytest to display not only additional information on failures and
94- errors at the end of a test run, but also a test summary.
95-
96- ``-r ``
97- displays additional information on the test summary.
98- ``a ``
99- displays all but the passed tests. This adds the information
100- ``skipped ``, ``xfailed `` or ``xpassed `` to the failures and errors.
101-
102- ``testpaths = tests ``
97+
98+ ``--strict-markers ``
99+ instructs pytest to issue an error instead of a warning for every
100+ unregistered marker that appears in the test code. This allows us to
101+ avoid typos in marker names.
102+ ``--strict-config ``
103+ instructs pytest to issue an error instead of a warning if difficulties
104+ arise when parsing configuration files. This prevents typing errors in
105+ the configuration file from going unnoticed.
106+ ``-ra ``
107+ instructs pytest to display not only additional information on failures
108+ and errors at the end of a test run, but also a test summary.
109+
110+ ``-r ``
111+ displays additional information on the test summary.
112+ ``a ``
113+ displays all but the passed tests. This adds the information
114+ ``skipped ``, ``xfailed `` or ``xpassed `` to the failures and errors.
115+
116+ ``testpaths = ["tests",] ``
103117 tells pytest where to look for tests if you have not specified a file or
104118 directory name on the command line. In our case, pytest searches in the
105119 :file: `tests ` directory.
@@ -111,7 +125,7 @@ projects. Let’s briefly go through the individual lines:
111125 especially if our :file: `src `, :file: `docs ` or other directories are quite
112126 large.
113127
114- ``markers = ``
128+ ``markers = ["exception: Only run expected exceptions", …] ``
115129 is used to declare markers, as described in
116130 :ref: `select-tests-with-markers `.
117131
@@ -123,42 +137,32 @@ projects. Let’s briefly go through the individual lines:
123137Using other configuration files
124138~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
125139
126- If you are writing tests for a project that already has a
127- :file: `pyproject.toml `, :file: `tox.ini ` or :file: `setup.cfg ` file, you can use
128- :file: `pytest.ini ` to store your pytest configuration settings, or you can store
129- your configuration settings in one of these alternative configuration files. The
130- syntax of the two non-ini files is slightly different, so we will take a closer
131- look at both files.
140+ If you are writing tests for a project that already has a :file: `pytest.ini `,
141+ :file: `tox.ini `, or :file: `setup.cfg ` file, you can also store your pytest
142+ configuration settings in one of these alternative configuration files. The
143+ syntax of the :file: `*.ini ` files differs slightly, so we will take a closer
144+ look at these files.
132145
133- :file: `pyproject.toml `
134- ::::::::::::::::::::::
146+ :file: `pytest.ini `
147+ ::::::::::::::::::
135148
136- The :file: `pyproject.toml ` file was originally intended for the packaging of
137- Python projects; however, it can also be used to define project settings.
149+ The :file: `pytest.ini ` file was originally intended for configuring pytest.
138150
139151As :doc: `Python4DataScience:data-processing/serialisation-formats/toml/index ` is
140152a different standard for configuration files than :file: `.ini ` files, the format
141153is also slightly different:
142154
143- .. code-block :: toml
155+ .. code-block :: ini
144156
145- [tool.pytest.ini_options]
146- addopts = [
147- "--strict-markers",
148- "--strict-config",
149- "-ra"
150- ]
151- testpaths = "tests"
152- markers = [
153- "exception: Only run expected exceptions",
154- "finish: Only run finish tests",
155- "smoke: Small subset of all tests",
156- "num_items: Number of items to be pre-filled for the items_db fixture"
157- ]
158-
159- Instead of ``[pytest] ``, the section begins with ``[tool.pytest.ini_options] ``,
160- the values must be enclosed in quotes and lists of values must be lists of
161- character strings in square brackets.
157+ [pytest]
158+ addopts =
159+ --strict-markers
160+ --strict-config
161+ -ra
162+ testpaths = tests
163+ markers =
164+ smoke: Small subset of all tests
165+ exception: Only run expected exceptions
162166
163167:file: `setup.cfg `
164168:::::::::::::::::
0 commit comments