pyproject.toml configuration in PyTest - Build an Automation Script
[tool.pytest.ini_options] testpaths = ["tests"] markers = ["smoke: quick smoke tests"] # test_sample.py file content def test_example(): assert 1 + 1 == 2 # To run the test, execute in terminal: # pytest # The test will be discovered in the 'tests' folder as configured in pyproject.toml # Sample command line output expected: # ============================= test session starts ============================= # collected 1 item # # tests/test_sample.py . [100%] # # ============================== 1 passed in 0.01s ==============================
The pyproject.toml file configures pytest to look for tests in the tests folder and defines a custom marker smoke. This replaces the need to pass command line options each time.
The test_sample.py file contains a simple test function test_example that asserts 1 + 1 equals 2.
Running pytest in the terminal will automatically discover tests in the tests folder as specified, run them, and show the results.
This setup follows best practices by centralizing configuration and using pytest's default discovery conventions.
Now add a custom marker 'slow' in pyproject.toml and write two tests, one marked 'slow' and one normal. Run pytest to only execute the normal tests.
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
