Verify pytest runs with default addopts options
Preconditions (2)
✅ Expected Result: pytest stops after 2 failures and no warnings are displayed during the test run
import subprocess import os import tempfile import textwrap def test_pytest_addopts_behavior(): with tempfile.TemporaryDirectory() as tmpdir: # Create pytest.ini with addopts ini_content = textwrap.dedent(''' [pytest] addopts = --maxfail=2 --disable-warnings ''') ini_path = os.path.join(tmpdir, 'pytest.ini') with open(ini_path, 'w') as f: f.write(ini_content) # Create test_sample.py with 3 tests: 2 fail, 1 pass test_content = textwrap.dedent(''' import pytest def test_pass(): assert True def test_fail1(): assert False def test_fail2(): assert False def test_fail3(): assert False ''') test_path = os.path.join(tmpdir, 'test_sample.py') with open(test_path, 'w') as f: f.write(test_content) # Run pytest in tmpdir without extra args result = subprocess.run( ['pytest'], cwd=tmpdir, capture_output=True, text=True ) # Check pytest.ini content with open(ini_path) as f: ini_check = f.read() assert '--maxfail=2' in ini_check assert '--disable-warnings' in ini_check # Check output contains exactly 2 failures reported # The output summary line looks like: '== X failed, Y passed in Z seconds ==' # We expect 2 failures and 1 pass assert '2 failed' in result.stdout or '2 failed' in result.stderr assert '1 passed' in result.stdout or '1 passed' in result.stderr # Check that test run stopped after 2 failures (not 3) # So test_fail3 should not appear in output assert 'test_fail3' not in result.stdout # Check warnings are not shown # Warnings usually contain 'WARNING' or 'DeprecationWarning' assert 'WARNING' not in result.stdout assert 'DeprecationWarning' not in result.stdout # Assert pytest exit code is 1 (failures) assert result.returncode == 1
This test creates a temporary directory to isolate files.
It writes a pytest.ini file with addopts set to --maxfail=2 --disable-warnings.
It creates a test file with 3 failing tests and 1 passing test.
Running pytest in that directory without extra options uses the addopts defaults.
The test captures output and checks that only 2 failures are reported, meaning pytest stopped after 2 failures.
It also verifies warnings are not shown in the output.
Assertions confirm the expected behavior and the exit code.
Now add data-driven testing with 3 different addopts configurations and verify pytest behavior for each
addopts setting in a pytest.ini file?addopts role in pytest.iniaddopts is used to provide default command-line options so you don't have to type them every time.addopts.pytest.ini using addopts?pytest.ini, options are set as key = value pairs under [pytest] section.--maxfail=3 --verbose.pytest.ini content:[pytest] addopts = --maxfail=2 --tb=short
pytest tests/ without extra options?--maxfail=2 and --tb=short set limits on failures and traceback style by default.addopts = --maxfail=1 --verbose to pytest.ini but pytest still runs all tests without stopping early. What is the likely cause?pytest.ini if it is in the current directory or a parent directory of the test run.pytest.ini is misplaced, addopts settings are ignored and default pytest behavior occurs.addopts line in pytest.ini achieves this correctly?--cov=src with equals sign; maxfail uses --maxfail=5.--stop are incorrect.Pass the test and this pattern moves unknown