What is the purpose of the addopts setting in pytest?
The addopts setting in pytest allows you to specify default command-line options that pytest will use every time it runs. This helps avoid typing common options repeatedly.
Click to reveal answer
beginner
Where do you add the addopts setting to configure default pytest options?
You add the addopts setting in the pytest.ini file or other pytest configuration files like tox.ini or setup.cfg.
Click to reveal answer
beginner
Example: How to set pytest to always show detailed test output by default using addopts?
In pytest.ini, add:
[pytest]
addopts = -v
This makes pytest run with verbose output every time without typing -v.
Click to reveal answer
intermediate
Can command-line options override addopts settings?
Yes. Options given directly on the command line when running pytest will override the defaults set in addopts.
Click to reveal answer
intermediate
Why is using addopts helpful in a team environment?
Using addopts ensures everyone runs pytest with the same default options, leading to consistent test runs and easier debugging.
Click to reveal answer
Where is the addopts setting typically placed to set default pytest options?
AIn the <code>pytest.ini</code> file
BInside each test function
CIn the Python environment variables
DIn the test script comments
✗ Incorrect
The addopts setting is placed in configuration files like pytest.ini to apply default options globally.
What happens if you run pytest with a command-line option that conflicts with addopts?
2. Which of the following is the correct way to add default options in pytest.ini using addopts?
easy
A. [pytest]
addopts = maxfail=3 verbose
B. [pytest]
addopts: --maxfail=3 --verbose
C. [pytest]
addopts = --maxfail=3 --verbose
D. [pytest]
addopts --maxfail=3 --verbose
Solution
Step 1: Check correct syntax for ini files
In pytest.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 C
Quick Check:
Ini syntax uses = and spaces for addopts [OK]
Hint: Use '=' after addopts and space-separated options [OK]
Common Mistakes:
Using ':' instead of '='
Omitting dashes before options
Not placing options under [pytest] section
3. Given this pytest.ini content:
[pytest]
addopts = --maxfail=2 --tb=short
What will happen when you run pytest tests/ without extra options?
medium
A. Pytest runs all tests and shows full tracebacks
B. Pytest stops after 2 failures and shows short tracebacks
C. Pytest ignores addopts and runs normally
D. Pytest throws a syntax error due to addopts
Solution
Step 1: Understand addopts effect on pytest run
The options --maxfail=2 and --tb=short set 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 B
Quick Check:
addopts options apply by default [OK]
Hint: Default options run automatically without typing them [OK]
Common Mistakes:
Assuming addopts is ignored
Thinking full tracebacks show by default
Expecting all tests to run despite maxfail
4. You added addopts = --maxfail=1 --verbose to pytest.ini but pytest still runs all tests without stopping early. What is the likely cause?
medium
A. The pytest.ini file is not in the current directory or pytest root
B. The --maxfail option does not work with addopts
C. You must run pytest with --use-addopts flag
D. Verbose mode disables maxfail stopping
Solution
Step 1: Check pytest.ini location
Pytest only reads pytest.ini if it is in the current directory or a parent directory of the test run.
Step 2: Understand addopts behavior
If pytest.ini is 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 A
Quick Check:
pytest.ini location affects addopts usage [OK]
Hint: Ensure pytest.ini is in the test root folder [OK]
Common Mistakes:
Believing maxfail needs special flags
Thinking verbose disables maxfail
Ignoring pytest.ini file location
5. You want pytest to always run tests with coverage report and stop after 5 failures by default. Which addopts line in pytest.ini achieves this correctly?
hard
A. [pytest]
addopts: --cov src --maxfail=5
B. [pytest]
addopts = maxfail=5 --cov src
C. [pytest]
addopts = --maxfail=5 --cov=src --stop
D. [pytest]
addopts = --cov=src --maxfail=5
Solution
Step 1: Understand correct option syntax
Coverage option requires --cov=src with equals sign; maxfail uses --maxfail=5.
Step 2: Check for invalid or missing syntax
Options without '=' or extra unknown flags like --stop are incorrect.
Final Answer:
[pytest]\naddopts = --cov=src --maxfail=5 -> Option D
Quick Check:
Use '=' for options with values in addopts [OK]
Hint: Use '=' for options with values like --cov=src [OK]
Common Mistakes:
Omitting '=' in options with values
Adding unsupported flags like --stop
Switching order without '=' causing errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.