The pytest.ini file is used to configure pytest settings globally for a project. It helps set options like test paths, markers, and command-line flags so you don't have to type them every time.
Click to reveal answer
beginner
How do you specify the directory where pytest should look for tests in pytest.ini?
You use the testpaths option under the [pytest] section. For example:
[pytest]
testpaths = tests
This tells pytest to look inside the tests folder for test files.
Click to reveal answer
intermediate
What is the use of the markers option in pytest.ini?
The markers option lets you declare custom markers to label tests. This helps organize or selectively run tests. For example:
[pytest]
markers = slow: marks tests as slow to run
Click to reveal answer
intermediate
How can you add command-line options permanently using pytest.ini?
You can add default command-line options using the addopts setting. For example:
[pytest]
addopts = -v --maxfail=2
This runs tests in verbose mode and stops after 2 failures.
Click to reveal answer
beginner
Where should the pytest.ini file be placed in your project?
Place pytest.ini in the root folder of your project. Pytest automatically finds it there and applies the settings when you run tests.
Click to reveal answer
What section header must be used in pytest.ini to configure pytest?
A[settings]
B[config]
C[pytest]
D[test]
✗ Incorrect
The correct section header is [pytest]. This tells pytest to read the options under this section.
Which option in pytest.ini sets the default command-line arguments?
Aaddopts
Bmarkers
Ctestpaths
Dlog_level
✗ Incorrect
The addopts option lets you specify default command-line arguments for pytest.
How do you declare a custom marker named 'slow' in pytest.ini?
Amarker = slow
Baddopts = slow
Ctestpaths = slow
Dmarkers = slow: marks slow tests
✗ Incorrect
You declare custom markers under the markers option with a description.
Where should you place the pytest.ini file for pytest to find it automatically?
AIn the root folder of the project
BInside the tests folder
CIn the home directory
DIn the Python installation folder
✗ Incorrect
pytest looks for pytest.ini in the root folder of your project.
What does the testpaths option do in pytest.ini?
ASpecifies which tests to skip
BSpecifies directories where pytest looks for tests
CSets the verbosity level
DDefines test markers
✗ Incorrect
testpaths tells pytest which directories to search for test files.
Explain how to configure pytest to run tests only from a specific folder using pytest.ini.
Think about telling pytest where to look for tests.
You got /3 concepts.
Describe how to add a custom marker and use it to label tests in pytest.ini.
Markers help organize tests by labels.
You got /3 concepts.
Practice
(1/5)
1. What is the primary purpose of the pytest.ini file in a pytest project?
easy
A. To install pytest dependencies automatically
B. To store configuration settings for pytest test runs
C. To write test functions and test cases
D. To generate test reports after running tests
Solution
Step 1: Understand pytest.ini role
The pytest.ini file is used to configure pytest behavior before running tests.
Step 2: Differentiate from other files
Test functions are written in Python files, not in pytest.ini. Dependency installation and report generation are handled elsewhere.
Final Answer:
To store configuration settings for pytest test runs -> Option B
Quick Check:
pytest.ini = config file [OK]
Hint: pytest.ini holds settings, not test code or reports [OK]
Common Mistakes:
Confusing pytest.ini with test script files
Thinking pytest.ini installs packages
Assuming pytest.ini generates reports
2. Which of the following is the correct way to set the verbosity level to 2 in pytest.ini?
easy
A. [pytest]
addopts = -vv
B. [pytest]
verbosity = 2
C. [pytest]
verbosity: 2
D. [pytest]
addopts = --verbosity=2
Solution
Step 1: Recall pytest verbosity setting
pytest uses addopts to pass command-line options like -vv for verbosity level 2.
Hint: Use addopts with -vv for verbosity level 2 in pytest.ini [OK]
Common Mistakes:
Using 'verbosity' instead of 'addopts'
Incorrect syntax with colon instead of equals
Using unsupported flags like --verbosity
3. Given this pytest.ini content:
[pytest]
markers = slow: marks tests as slow
smoke: marks smoke tests
addopts = -rs
What will be the effect of running pytest with this configuration?
medium
A. Tests marked as slow or smoke will be skipped automatically
B. pytest will run tests with no output
C. Only tests marked as slow will run
D. pytest will show extra summary info for skipped and failed tests
Solution
Step 1: Understand markers and addopts
The markers section defines labels for tests but does not skip them automatically. The addopts = -rs option tells pytest to show extra summary info for skipped and failed tests.
Step 2: Analyze each option
Tests marked as slow or smoke will be skipped automatically is wrong because markers alone don't skip tests. Only tests marked as slow will run is wrong because no selection is made. pytest will run tests with no output is wrong because -rs increases output detail.
Final Answer:
pytest will show extra summary info for skipped and failed tests -> Option D
Quick Check:
addopts = -rs shows skip/fail summary [OK]
Hint: addopts = -rs shows skip and fail summaries [OK]
But when you run pytest, you get an error: ERROR: unknown option: --maxfail=3 What is the most likely cause?
medium
A. The addopts line should use -maxfail=3 instead
B. The pytest.ini file is missing the [pytest] header
C. The pytest version is outdated and does not support --maxfail
D. Markers must be defined after addopts in pytest.ini
Solution
Step 1: Understand the error message
The error says unknown option: --maxfail=3, meaning pytest does not recognize this option.
Step 2: Check possible causes
The addopts line should use -maxfail=3 instead is wrong because --maxfail requires two dashes. The pytest.ini file is missing the [pytest] header is wrong because the header is present. Markers must be defined after addopts in pytest.ini is wrong because order does not matter. The most common cause is an outdated pytest version that lacks support for --maxfail.
Final Answer:
The pytest version is outdated and does not support --maxfail -> Option C
Quick Check:
Unknown option usually means outdated pytest [OK]
Hint: Unknown option often means pytest version too old [OK]
Common Mistakes:
Changing option syntax incorrectly
Forgetting to include [pytest] header
Thinking order of keys matters in pytest.ini
5. You want to configure pytest.ini to: 1. Mark tests with slow and critical labels. 2. Run only tests matching the pattern test_*.py. 3. Stop after 2 failures.
Which of the following pytest.ini configurations achieves all three correctly?
Options A, C, and D correctly define markers with descriptions. [pytest]
markers = slow
critical
python_files = test_*.py
addopts = -x --maxfail=2 lacks descriptions, which is allowed but less clear.
Step 2: Verify test file pattern
All options set python_files = test_*.py correctly.
Step 3: Confirm failure stop and run options
[pytest]
markers = slow: slow tests
critical: critical tests
python_files = test_*.py
addopts = --maxfail=2 uses addopts = --maxfail=2 which stops after 2 failures but does not stop immediately on first failure. Options C and D add -x which stops after first failure, conflicting with requirement to stop after 2 failures. [pytest]
markers = slow
critical
python_files = test_*.py
addopts = -x --maxfail=2 lacks descriptions in markers.
Final Answer:
Markers with descriptions, correct python_files, and stop after 2 failures -> Option A
Quick Check:
Use --maxfail=2 without -x to stop after 2 failures [OK]
Hint: Use --maxfail=2 without -x to stop after 2 failures [OK]
Common Mistakes:
Adding -x stops after 1 failure, not 2
Omitting marker descriptions
Misconfiguring python_files pattern
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.