pytest-project/
├── tests/
│ ├── test_example.py
│ └── test_login.py
├── conftest.py
├── pytest.ini
└── requirements.txt
Addopts for default options in PyTest - Framework Patterns
- Tests: Located in
tests/folder, contains test scripts liketest_example.py. - Fixtures & Hooks: Defined in
conftest.pyfor setup and teardown logic shared across tests. - Configuration:
pytest.inifile holds default options usingaddoptsto customize pytest runs. - Dependencies:
requirements.txtlists Python packages needed for testing.
The pytest.ini file uses the addopts setting to specify default command-line options for pytest runs. This helps avoid typing options every time.
[pytest]
addopts = -v --maxfail=2 --tb=short
# Explanation:
# -v : verbose output
# --maxfail=2 : stop after 2 failures
# --tb=short : shorter traceback for failures
Use environment variables or separate config files for different environments if needed, but addopts is the main place for default pytest options.
Default options in addopts can include plugins for reporting, like --junitxml=reports/junit-report.xml to generate XML reports for CI tools.
[pytest]
addopts = -v --maxfail=2 --tb=short --junitxml=reports/junit-report.xml
CI/CD pipelines can run pytest without extra flags, relying on pytest.ini defaults. This keeps pipeline scripts simple and consistent.
- Use
addoptsinpytest.inito set common options like verbosity and failure limits. - Keep
pytest.iniin the project root for easy discovery by pytest. - Include reporting options in
addoptsfor automated test reports in CI/CD. - Avoid putting sensitive info in
addopts; use environment variables orconftest.pyfixtures for secrets. - Document the purpose of each option in comments inside
pytest.inifor team clarity.
Question: Where would you add a default option to generate an HTML report automatically after tests run?
Answer: Add the option --html=report.html inside the addopts setting in the pytest.ini file at the project root.
Practice
addopts setting in a pytest.ini file?Solution
Step 1: Understand
addoptsrole in pytest.iniaddoptsis used to provide default command-line options so you don't have to type them every time.Step 2: Compare other options
Other options like naming tests or Python version are not controlled byaddopts.Final Answer:
To set default command-line options for pytest runs -> Option AQuick Check:
Default pytest options = addopts [OK]
- Thinking addopts defines test names
- Confusing addopts with Python version settings
- Assuming addopts lists test files
pytest.ini using addopts?Solution
Step 1: Check correct syntax for ini files
Inpytest.ini, options are set as key = value pairs under [pytest] section.Step 2: Validate option format
Options must be space-separated strings with dashes, e.g.,--maxfail=3 --verbose.Final Answer:
[pytest]\naddopts = --maxfail=3 --verbose -> Option CQuick Check:
Ini syntax uses = and spaces for addopts [OK]
- Using ':' instead of '='
- Omitting dashes before options
- Not placing options under [pytest] section
pytest.ini content:[pytest] addopts = --maxfail=2 --tb=short
What will happen when you run
pytest tests/ without extra options?Solution
Step 1: Understand addopts effect on pytest run
The options--maxfail=2and--tb=shortset limits on failures and traceback style by default.Step 2: Predict behavior without extra command-line options
Since addopts sets defaults, pytest will stop after 2 failures and show short tracebacks automatically.Final Answer:
Pytest stops after 2 failures and shows short tracebacks -> Option BQuick Check:
addopts options apply by default [OK]
- Assuming addopts is ignored
- Thinking full tracebacks show by default
- Expecting all tests to run despite maxfail
addopts = --maxfail=1 --verbose to pytest.ini but pytest still runs all tests without stopping early. What is the likely cause?Solution
Step 1: Check pytest.ini location
Pytest only readspytest.iniif it is in the current directory or a parent directory of the test run.Step 2: Understand addopts behavior
Ifpytest.iniis misplaced, addopts settings are ignored and default pytest behavior occurs.Final Answer:
The pytest.ini file is not in the current directory or pytest root -> Option AQuick Check:
pytest.ini location affects addopts usage [OK]
- Believing maxfail needs special flags
- Thinking verbose disables maxfail
- Ignoring pytest.ini file location
addopts line in pytest.ini achieves this correctly?Solution
Step 1: Understand correct option syntax
Coverage option requires--cov=srcwith equals sign; maxfail uses--maxfail=5.Step 2: Check for invalid or missing syntax
Options without '=' or extra unknown flags like--stopare incorrect.Final Answer:
[pytest]\naddopts = --cov=src --maxfail=5 -> Option DQuick Check:
Use '=' for options with values in addopts [OK]
- Omitting '=' in options with values
- Adding unsupported flags like --stop
- Switching order without '=' causing errors
Ready to prove it?
Pass the test and this pattern moves unknown
