pytest.ini helps you set up rules and options for running tests easily. It saves time by keeping settings in one place.
pytest.ini configuration
[pytest]
addopts = <options>
markers =
<marker1>: <description>
<marker2>: <description>
python_files = <pattern>
python_classes = <pattern>
python_functions = <pattern>
minversion = <version>
filterwarnings = <warning rules>The file is named pytest.ini and placed in your project root.
Indent marker descriptions with 4 spaces for readability.
test_.[pytest] addopts = -v --maxfail=2 markers = slow: marks tests as slow smoke: marks smoke tests python_files = test_*.py minversion = 6.0
[pytest] addopts = --tb=short filterwarnings = ignore::DeprecationWarning
This pytest.ini file sets verbose output, stops after one failure, defines a 'fast' marker, looks for test files starting with 'test_', and requires pytest version 6.0 or higher.
[pytest] addopts = -v --maxfail=1 --tb=short markers = fast: marks fast tests python_files = test_*.py minversion = 6.0
Always place pytest.ini in the root folder of your project.
Use markers to organize tests and run specific groups easily.
Keep your addopts simple to avoid confusion when running tests manually.
pytest.ini stores test run settings in one file.
You can set options like verbosity, failure limits, and test file patterns.
Markers help label tests for easier selection.
Practice
pytest.ini file in a pytest project?Solution
Step 1: Understand pytest.ini role
Thepytest.inifile is used to configure pytest behavior before running tests.Step 2: Differentiate from other files
Test functions are written in Python files, not inpytest.ini. Dependency installation and report generation are handled elsewhere.Final Answer:
To store configuration settings for pytest test runs -> Option BQuick Check:
pytest.ini = config file [OK]
- Confusing pytest.ini with test script files
- Thinking pytest.ini installs packages
- Assuming pytest.ini generates reports
pytest.ini?Solution
Step 1: Recall pytest verbosity setting
pytest usesaddoptsto pass command-line options like-vvfor verbosity level 2.Step 2: Check syntax correctness
[pytest] addopts = -vv correctly usesaddopts = -vv. [pytest] verbosity = 2 uses an invalid keyverbosity. [pytest] verbosity: 2 uses wrong syntax with colon. [pytest] addopts = --verbosity=2 uses an invalid flag.Final Answer:
[pytest] addopts = -vv -> Option AQuick Check:
Verbosity level 2 via addopts = -vv [OK]
- Using 'verbosity' instead of 'addopts'
- Incorrect syntax with colon instead of equals
- Using unsupported flags like --verbosity
pytest.ini content:[pytest]
markers = slow: marks tests as slow
smoke: marks smoke tests
addopts = -rsWhat will be the effect of running
pytest with this configuration?Solution
Step 1: Understand markers and addopts
Themarkerssection defines labels for tests but does not skip them automatically. Theaddopts = -rsoption 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-rsincreases output detail.Final Answer:
pytest will show extra summary info for skipped and failed tests -> Option DQuick Check:
addopts = -rs shows skip/fail summary [OK]
- Thinking markers skip tests automatically
- Confusing addopts flags with test selection
- Assuming no output with -rs option
pytest.ini file:[pytest] markers = regression addopts = --maxfail=3
But when you run pytest, you get an error:
ERROR: unknown option: --maxfail=3What is the most likely cause?
Solution
Step 1: Understand the error message
The error saysunknown option: --maxfail=3, meaning pytest does not recognize this option.Step 2: Check possible causes
Theaddoptsline should use-maxfail=3instead is wrong because--maxfailrequires two dashes. Thepytest.inifile is missing the[pytest]header is wrong because the header is present. Markers must be defined afteraddoptsinpytest.iniis 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 CQuick Check:
Unknown option usually means outdated pytest [OK]
- Changing option syntax incorrectly
- Forgetting to include [pytest] header
- Thinking order of keys matters in pytest.ini
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?Solution
Step 1: Check markers syntax
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 setpython_files = test_*.pycorrectly.Step 3: Confirm failure stop and run options
[pytest] markers = slow: slow tests critical: critical tests python_files = test_*.py addopts = --maxfail=2 usesaddopts = --maxfail=2which stops after 2 failures but does not stop immediately on first failure. Options C and D add-xwhich 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 AQuick Check:
Use --maxfail=2 without -x to stop after 2 failures [OK]
- Adding -x stops after 1 failure, not 2
- Omitting marker descriptions
- Misconfiguring python_files pattern
Ready to prove it?
Pass the test and this pattern moves unknown
