Framework Mode - pytest.ini configuration
Folder Structure
project-root/ ├── tests/ │ ├── test_example.py │ └── conftest.py ├── src/ │ └── app_code.py ├── pytest.ini └── requirements.txt
project-root/ ├── tests/ │ ├── test_example.py │ └── conftest.py ├── src/ │ └── app_code.py ├── pytest.ini └── requirements.txt
pytest.ini file to set global pytest options and markers.tests/ folder containing test functions.conftest.py for shared setup and teardown code.src/ folder.The pytest.ini file is used to configure pytest behavior globally for the project.
Common configurations include:
Example pytest.ini content:
[pytest]
minversion = 7.0
addopts = -ra -q
testpaths = tests
markers =
smoke: Quick smoke tests
regression: Full regression suite
log_cli = true
log_cli_level = INFO
This setup helps run tests consistently across environments without repeating command line arguments.
pytest.ini can configure reporting options such as verbosity and logging to help understand test results.
For CI/CD pipelines:
addopts to include plugins like pytest-html for HTML reports.pytest.ini for consistent runs.Example snippet for CI usage:
pytest --html=report.html --self-contained-html
Where in this framework structure would you add a new custom marker called slow to categorize tests that take longer to run?
pytest.ini file in a pytest project?pytest.ini file is used to configure pytest behavior before running tests.pytest.ini. Dependency installation and report generation are handled elsewhere.pytest.ini?addopts to pass command-line options like -vv for verbosity level 2.addopts = -vv. [pytest]
verbosity = 2 uses an invalid key verbosity. [pytest]
verbosity: 2 uses wrong syntax with colon. [pytest]
addopts = --verbosity=2 uses an invalid flag.pytest.ini content:[pytest]
markers = slow: marks tests as slow
smoke: marks smoke tests
addopts = -rspytest with this configuration?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.-rs increases output detail.pytest.ini file:[pytest] markers = regression addopts = --maxfail=3
ERROR: unknown option: --maxfail=3unknown option: --maxfail=3, meaning pytest does not recognize this option.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.pytest.ini to:slow and critical labels.test_*.py.pytest.ini configurations achieves all three correctly?python_files = test_*.py correctly.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.Pass the test and this pattern moves unknown