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.