0
0
PyTesttesting~15 mins

Addopts for default options in PyTest - Build an Automation Script

Choose your learning style9 modes available
Verify pytest runs with default addopts options
Preconditions (2)
Step 1: Create a pytest.ini file with addopts set to '--maxfail=2 --disable-warnings'
Step 2: Create a test file test_sample.py with 3 test functions: two failing and one passing
Step 3: Run pytest without any command line options
Step 4: Observe the test run stops after 2 failures and warnings are not shown
✅ Expected Result: pytest stops after 2 failures and no warnings are displayed during the test run
Automation Requirements - pytest
Assertions Needed:
Verify pytest.ini file contains addopts with '--maxfail=2 --disable-warnings'
Verify pytest run stops after 2 failures
Verify warnings are not shown in the output
Best Practices:
Use subprocess module to run pytest command
Capture output and error streams
Use assertions on output text to verify behavior
Keep test files and config files isolated in temporary directories
Automated Solution
PyTest
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.

Common Mistakes - 4 Pitfalls
{'mistake': 'Not creating a pytest.ini file with addopts before running tests', 'why_bad': "Without the config file, pytest won't use the default options, so the test won't verify addopts behavior.", 'correct_approach': 'Always create or ensure pytest.ini with addopts is present in the test directory before running pytest.'}
{'mistake': 'Running pytest with command line options overriding addopts', 'why_bad': "Command line options override addopts, so the test won't check default options set in pytest.ini.", 'correct_approach': 'Run pytest without extra command line options to test addopts defaults.'}
Checking for warnings in stderr only
Not isolating test files and config in a temporary directory
Bonus Challenge

Now add data-driven testing with 3 different addopts configurations and verify pytest behavior for each

Show Hint