Sub topic 14
Configuring pytest in pyproject.toml
This chapter assumes you can run pytest from a terminal, write tests with markers, and work inside a virtual environment. It covers the [tool.pytest.ini_options] table, which holds the settings you would otherwise retype on every command line.
Where pytest reads configuration
Pytest looks for configuration in the project's root directory. It checks pytest.ini, then pyproject.toml, then tox.ini, then setup.cfg. The first file that contains pytest settings wins. Most modern projects already have a pyproject.toml, so putting the settings there keeps everything in one place.
In pyproject.toml, pytest settings live under the [tool.pytest.ini_options] table. The name ini_options is historical. The values are ordinary TOML.
A working configuration
[tool.pytest.ini_options]
testpaths = ["tests"]
addopts = "-ra"
markers = [
"slow: marks tests as slow (deselect with -m 'not slow')",
]Each setting does one job:
testpathstells pytest where to search when you runpytestwith no path arguments. Without it, pytest scans the current directory and everything below it.addoptsis a string of command-line options that pytest adds to every run. Here-raprints a short summary of every test that was not a plain pass, including skipped, xfailed, and failed tests.markersregisters custom markers. Each entry has the formname: description.
Run pytest --markers to see your custom markers listed next to the built-in ones.
Why register markers
A marker is a label you attach with @pytest.mark.slow. Pytest does not reject unknown markers by default. It only warns, which means a typo such as @pytest.mark.slwo silently creates a new marker that selects nothing.
Registering markers makes the list explicit and lets you catch typos. Add --strict-markers to addopts and pytest turns unregistered markers into errors:
[tool.pytest.ini_options]
testpaths = ["tests"]
addopts = "-ra --strict-markers"
markers = [
"slow: marks tests as slow (deselect with -m 'not slow')",
]With this in place, you can skip slow tests from the command line:
pytest -m "not slow"How addopts combines with the command line
Options in addopts are placed before the ones you type. If you run pytest -v, pytest behaves as if you had run pytest -ra -v. Later options generally override earlier ones when they conflict, so you can adjust a single run without editing the file.
Common errors
- Settings are ignored. Pytest picked a different file first. A
pytest.iniin the same directory takes priority overpyproject.toml, even if it is nearly empty. Runpytestand read theconfigfile:line in the header to see which file was used. addoptsas a list. Inpyproject.toml,addoptscan be a string, as shown above. Writing a single string with spaces between options is the safest form.- Plugin options fail. Options such as
--covonly exist once the plugin is installed. If you put--cov=cartinaddoptswithout installingpytest-cov, every run fails with an "unrecognized arguments" error. - Wrong table name.
[tool.pytest]is not the same as[tool.pytest.ini_options]. Use the longer name.
Exercise
Your team has a test marked @pytest.mark.integration, but pytest only prints a warning about an unknown marker, and pytest -m integration still works. What two changes to pyproject.toml make pytest fail loudly when someone mistypes a marker name?
Show answer
Register the marker by adding an entry such as "integration: tests that need external services" to the markers list, and add --strict-markers to addopts. With both in place, any marker not in the list, including a misspelled one, causes an error.