The pyproject.toml file helps you set up pytest settings in one place. It makes running tests easier and organized.
pyproject.toml configuration in PyTest
[tool.pytest.ini_options] addopts = "--maxfail=3 --disable-warnings" testpaths = ["tests"] markers = ["slow: marks tests as slow"]
The section [tool.pytest.ini_options] is where pytest reads its settings from pyproject.toml.
Use simple key-value pairs to set options like addopts or testpaths.
tests folder.[tool.pytest.ini_options] addopts = "-v" testpaths = ["tests"]
[tool.pytest.ini_options] markers = ["slow: marks tests as slow", "fast: marks tests as fast"]
[tool.pytest.ini_options]
addopts = "--maxfail=2 --tb=short"This setup runs tests verbosely and stops after the first failure. The tests include one passing, one failing, and one skipped test.
# pyproject.toml content [tool.pytest.ini_options] addopts = "-v --maxfail=1" testpaths = ["tests"] # tests/test_sample.py import pytest def test_pass(): assert 1 == 1 def test_fail(): assert 1 == 2 def test_skip(): pytest.skip("Skipping this test")
Make sure pyproject.toml is in your project root folder.
Use quotes around strings and square brackets for lists in TOML syntax.
After changing pyproject.toml, just run pytest without extra options.
pyproject.toml stores pytest settings in one file.
It helps run tests with your preferred options automatically.
You can define test folders, markers, and run options easily.
Practice
pyproject.toml file in pytest?Solution
Step 1: Understand the role of
This file is used to keep configuration settings for pytest centralized.pyproject.tomlStep 2: Differentiate from other files
It does not contain test code or install dependencies but configures how pytest runs.Final Answer:
To store pytest configuration settings in one place -> Option AQuick Check:
Configuration file [OK]
- Confusing configuration file with test code file
- Thinking it installs packages
- Assuming it generates reports automatically
pyproject.toml for pytest?Solution
Step 1: Identify correct section header
pytest settings go under[tool.pytest]inpyproject.toml.Step 2: Check correct key and value format
The key istestpathsand it expects a list of strings, e.g., ["tests"].Final Answer:
[tool.pytest] testpaths = ["tests"] -> Option BQuick Check:
Section and key names must be exact [OK]
- Using wrong section header like [pytest]
- Using wrong key names like test_paths
- Assigning string instead of list for testpaths
pyproject.toml snippet:[tool.pytest] markers = ["slow: marks tests as slow"] addopts = "-v --maxfail=2"
What happens when you run
pytest?Solution
Step 1: Understand
Theaddoptssettings-vflag makes output verbose;--maxfail=2stops tests after 2 failures.Step 2: Understand
Definingmarkerssettingslowmarker means pytest recognizes it and can filter or mark tests accordingly.Final Answer:
Tests run verbosely and stop after 2 failures; 'slow' marker is recognized -> Option CQuick Check:
Verbose + maxfail + marker recognized [OK]
- Ignoring that -v means verbose output
- Not knowing --maxfail stops after failures
- Assuming markers are ignored without explicit use
pyproject.toml:[tool.pytest] markers = "slow: marks slow tests" addopts = -v
What is the problem when running pytest?
Solution
Step 1: Check markers format
Markers must be a list of strings, but here it is a single string without brackets.Step 2: Check addopts format
addopts value must be a string in quotes; here it is unquoted, causing syntax error.Final Answer:
Syntax error because markers should be a list, and addopts needs quotes -> Option DQuick Check:
Lists need brackets; strings need quotes [OK]
- Writing markers as a plain string instead of list
- Forgetting quotes around string values
- Assuming pytest ignores bad config silently
integration_tests folder and recognize a custom marker api. Which pyproject.toml configuration is correct?Solution
Step 1: Set correct testpaths key and value
Usetestpathsas a list under[tool.pytest]to specify test folders.Step 2: Define markers properly
Markers must be a list of strings describing each marker with a colon and description.Final Answer:
[tool.pytest] testpaths = ["integration_tests"] markers = ["api: marks API tests"] -> Option AQuick Check:
Correct keys, list format, and section [OK]
- Using string instead of list for testpaths
- Wrong section header like [pytest]
- Markers missing description or wrong format
Ready to prove it?
Pass the test and this pattern moves unknown
