What is the purpose of the pyproject.toml file in pytest?
The pyproject.toml file is used to configure pytest settings in a simple, centralized way without needing command-line options or separate config files.
Click to reveal answer
intermediate
How do you specify the minimum pytest version required in pyproject.toml?
Use the minversion option under [tool.pytest.ini_options], for example:
[tool.pytest.ini_options]
minversion = "7.0"
Click to reveal answer
beginner
Which section header is used in pyproject.toml to configure pytest options?
The section header is [tool.pytest.ini_options]. Under this, you can set options like addopts, testpaths, and others.
Click to reveal answer
intermediate
How do you add extra command-line options like verbose mode in pyproject.toml for pytest?
Use the addopts option under [tool.pytest.ini_options], for example:
addopts = "-v --maxfail=2"
to run tests verbosely and stop after 2 failures.
Click to reveal answer
intermediate
Can you configure test discovery paths in pyproject.toml? How?
Yes. Use the testpaths option under [tool.pytest.ini_options] to list directories or files where pytest looks for tests, e.g.:
testpaths = ["tests", "integration"]
Click to reveal answer
Which section header is correct to configure pytest in pyproject.toml?
A[tool.pytest.settings]
B[pytest.config]
C[tool.pytest.ini_options]
D[pytest.ini]
✗ Incorrect
The correct section header for pytest configuration in pyproject.toml is [tool.pytest.ini_options].
How do you add verbose output to pytest runs using pyproject.toml?
Aaddopts = "-v" under [tool.pytest.ini_options]
Bverbose = true under [tool.pytest]
Cverbosity = 1 under [pytest]
Dverbose = yes under [tool.pytest.ini_options]
✗ Incorrect
You add command-line options like verbose mode using addopts = "-v" under [tool.pytest.ini_options].
What does the testpaths option do in pyproject.toml for pytest?
ASets the output directory for test reports
BLists plugins to load
CDefines the Python version to use
DSpecifies directories or files where pytest looks for tests
✗ Incorrect
testpaths tells pytest where to find test files to run.
Which of these is a valid way to stop pytest after 3 failures using pyproject.toml?
Amaxfail = 3
Baddopts = "--maxfail=3"
Cstop_after_failures = 3
Dfail_limit = 3
✗ Incorrect
You use addopts = "--maxfail=3" to pass this option to pytest.
Is it possible to configure pytest entirely using pyproject.toml without command-line arguments?
AYes, all common pytest options can be set there
BNo, you must always use command-line options
COnly some options can be set in pyproject.toml
Dpyproject.toml is only for plugin configuration
✗ Incorrect
pytest supports configuring all common options in pyproject.toml under [tool.pytest.ini_options].
Explain how to configure pytest options using the pyproject.toml file.
Think about the section header and common options you can set.
You got /4 concepts.
Describe the benefits of using pyproject.toml for pytest configuration instead of command-line arguments.
Consider how configuration files help in projects.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of the pyproject.toml file in pytest?
easy
A. To store pytest configuration settings in one place
B. To write test functions directly
C. To install pytest dependencies
D. To generate test reports automatically
Solution
Step 1: Understand the role of pyproject.toml
This file is used to keep configuration settings for pytest centralized.
Step 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 A
Quick Check:
Configuration file [OK]
Hint: Remember: pyproject.toml holds settings, not test code [OK]
Common Mistakes:
Confusing configuration file with test code file
Thinking it installs packages
Assuming it generates reports automatically
2. Which of the following is the correct way to specify the test paths in pyproject.toml for pytest?
easy
A. [pytest]
testpaths = "tests"
B. [tool.pytest]
testpaths = ["tests"]
C. [tool.pytest]
test_paths = ["tests"]
D. [tool.pytest]
paths = "tests"
Solution
Step 1: Identify correct section header
pytest settings go under [tool.pytest] in pyproject.toml.
Step 2: Check correct key and value format
The key is testpaths and it expects a list of strings, e.g., ["tests"].
Final Answer:
[tool.pytest]
testpaths = ["tests"] -> Option B
Quick Check:
Section and key names must be exact [OK]
Hint: Use [tool.pytest] and testpaths as a list [OK]
Common Mistakes:
Using wrong section header like [pytest]
Using wrong key names like test_paths
Assigning string instead of list for testpaths
3. Given this pyproject.toml snippet:
[tool.pytest]
markers = ["slow: marks tests as slow"]
addopts = "-v --maxfail=2"
What happens when you run pytest?
medium
A. Tests run verbosely but do not stop on failures; 'slow' marker ignored
B. Tests run silently and stop after 2 failures; 'slow' marker ignored
C. Tests run verbosely and stop after 2 failures; 'slow' marker is recognized
D. Tests run silently and continue on failures; 'slow' marker recognized
Solution
Step 1: Understand addopts settings
The -v flag makes output verbose; --maxfail=2 stops tests after 2 failures.
Step 2: Understand markers setting
Defining slow marker 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 C
Quick Check:
Verbose + maxfail + marker recognized [OK]
Hint: addopts controls run options; markers define test tags [OK]
Common Mistakes:
Ignoring that -v means verbose output
Not knowing --maxfail stops after failures
Assuming markers are ignored without explicit use
4. You wrote this in pyproject.toml:
[tool.pytest]
markers = "slow: marks slow tests"
addopts = -v
What is the problem when running pytest?
medium
A. addopts ignored but markers work
B. No error; configuration works fine
C. Markers ignored but addopts works
D. Syntax error because markers should be a list, and addopts needs quotes
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 D
Quick Check:
Lists need brackets; strings need quotes [OK]
Hint: Always use brackets for lists and quotes for strings in TOML [OK]
Common Mistakes:
Writing markers as a plain string instead of list
Forgetting quotes around string values
Assuming pytest ignores bad config silently
5. You want pytest to automatically run tests only in the integration_tests folder and recognize a custom marker api. Which pyproject.toml configuration is correct?
hard
A. [tool.pytest]
testpaths = ["integration_tests"]
markers = ["api: marks API tests"]
B. [tool.pytest]
testpaths = "integration_tests"
markers = "api: marks API tests"
C. [pytest]
testpaths = ["integration_tests"]
markers = ["api"]
D. [tool.pytest]
paths = ["integration_tests"]
markers = ["api: marks API tests"]
Solution
Step 1: Set correct testpaths key and value
Use testpaths as 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 A
Quick Check:
Correct keys, list format, and section [OK]
Hint: Use [tool.pytest], testpaths list, and markers list with descriptions [OK]
Common Mistakes:
Using string instead of list for testpaths
Wrong section header like [pytest]
Markers missing description or wrong format
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.