Bird
Raised Fist0
PyTesttesting~15 mins

Addopts for default options in PyTest - Build an Automation Script

Choose your learning style10 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

Practice

(1/5)
1. What is the main purpose of the addopts setting in a pytest.ini file?
easy
A. To set default command-line options for pytest runs
B. To define test case names automatically
C. To specify the Python version for pytest
D. To list all test files explicitly

Solution

  1. Step 1: Understand addopts role in pytest.ini

    addopts is used to provide default command-line options so you don't have to type them every time.
  2. Step 2: Compare other options

    Other options like naming tests or Python version are not controlled by addopts.
  3. Final Answer:

    To set default command-line options for pytest runs -> Option A
  4. Quick Check:

    Default pytest options = addopts [OK]
Hint: Remember: addopts sets default pytest command options [OK]
Common Mistakes:
  • Thinking addopts defines test names
  • Confusing addopts with Python version settings
  • Assuming addopts lists test files
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

  1. Step 1: Check correct syntax for ini files

    In pytest.ini, options are set as key = value pairs under [pytest] section.
  2. Step 2: Validate option format

    Options must be space-separated strings with dashes, e.g., --maxfail=3 --verbose.
  3. Final Answer:

    [pytest]\naddopts = --maxfail=3 --verbose -> Option C
  4. 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

  1. Step 1: Understand addopts effect on pytest run

    The options --maxfail=2 and --tb=short set limits on failures and traceback style by default.
  2. Step 2: Predict behavior without extra command-line options

    Since addopts sets defaults, pytest will stop after 2 failures and show short tracebacks automatically.
  3. Final Answer:

    Pytest stops after 2 failures and shows short tracebacks -> Option B
  4. 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

  1. 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.
  2. Step 2: Understand addopts behavior

    If pytest.ini is misplaced, addopts settings are ignored and default pytest behavior occurs.
  3. Final Answer:

    The pytest.ini file is not in the current directory or pytest root -> Option A
  4. 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

  1. Step 1: Understand correct option syntax

    Coverage option requires --cov=src with equals sign; maxfail uses --maxfail=5.
  2. Step 2: Check for invalid or missing syntax

    Options without '=' or extra unknown flags like --stop are incorrect.
  3. Final Answer:

    [pytest]\naddopts = --cov=src --maxfail=5 -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard