Bird
Raised Fist0
PyTesttesting~15 mins

Why configuration standardizes test behavior in PyTest - Automation Benefits in Action

Choose your learning style10 modes available
Verify pytest configuration standardizes test behavior
Preconditions (2)
Step 1: Create two test functions: one marked with @pytest.mark.smoke and one without any marker
Step 2: Run pytest with the option '-m smoke' to select only tests with the smoke marker
Step 3: Observe which tests run
Step 4: Run pytest without any marker option
Step 5: Observe which tests run
✅ Expected Result: When running pytest with '-m smoke', only the test marked with @pytest.mark.smoke runs. When running pytest without '-m smoke', all tests run. This shows configuration standardizes which tests run.
Automation Requirements - pytest
Assertions Needed:
Assert that only the smoke test runs when pytest is run with '-m smoke'
Assert that both tests run when pytest is run without '-m smoke'
Best Practices:
Use pytest markers properly
Use pytest's capsys or pytester plugin to capture test run output
Keep tests independent and clear
Use pytest.ini to define markers
Automated Solution
PyTest
import subprocess
import sys
import pytest

# Content of test_sample.py
TEST_CODE = '''
import pytest

@pytest.mark.smoke
 def test_smoke():
    assert True

def test_regular():
    assert True
'''

# Content of pytest.ini
PYTEST_INI = '''
[pytest]
markers =
    smoke: mark a test as smoke test
'''

def test_pytest_marker_behavior(tmp_path):
    # Write test file
    test_file = tmp_path / "test_sample.py"
    test_file.write_text(TEST_CODE)

    # Write pytest.ini
    ini_file = tmp_path / "pytest.ini"
    ini_file.write_text(PYTEST_INI)

    # Run pytest with -m smoke
    result_smoke = subprocess.run(
        [sys.executable, '-m', 'pytest', str(test_file), '-m', 'smoke', '-q', '--tb=no'],
        capture_output=True,
        text=True,
        cwd=tmp_path
    )

    # Run pytest without marker
    result_all = subprocess.run(
        [sys.executable, '-m', 'pytest', str(test_file), '-q', '--tb=no'],
        capture_output=True,
        text=True,
        cwd=tmp_path
    )

    # Check output for smoke run: only test_smoke should run
    assert 'test_smoke' in result_smoke.stdout
    assert 'test_regular' not in result_smoke.stdout

    # Check output for all tests run: both tests should run
    assert 'test_smoke' in result_all.stdout
    assert 'test_regular' in result_all.stdout

This test creates a temporary test file with two tests: one marked with @pytest.mark.smoke and one regular test.

It also creates a pytest.ini file defining the smoke marker to avoid warnings.

Then it runs pytest twice using subprocess: once with -m smoke to run only smoke tests, and once without any marker to run all tests.

The assertions check that when running with -m smoke, only the smoke test runs, and when running without it, both tests run.

This shows how configuration (markers and pytest.ini) standardizes which tests run, making test behavior predictable and manageable.

Common Mistakes - 3 Pitfalls
Not defining custom markers in pytest.ini
Using print statements instead of assertions to verify test selection
Running pytest commands without capturing output in automation
Bonus Challenge

Now add data-driven testing with 3 different inputs to the smoke test and verify all run with '-m smoke'

Show Hint

Practice

(1/5)
1. Why is using a pytest.ini configuration file important for test behavior?
easy
A. It sets consistent options so tests run the same way every time.
B. It automatically fixes all test failures.
C. It replaces the need to write test functions.
D. It speeds up test execution by skipping all tests.

Solution

  1. Step 1: Understand the role of configuration files

    Configuration files like pytest.ini define settings that control how tests run, such as markers, test paths, or warnings.
  2. Step 2: Recognize the benefit of standardization

    By setting options in one place, all tests behave consistently across different runs and environments.
  3. Final Answer:

    It sets consistent options so tests run the same way every time. -> Option A
  4. Quick Check:

    Configuration standardizes test behavior = A [OK]
Hint: Config files keep test runs consistent everywhere [OK]
Common Mistakes:
  • Thinking config files fix test code errors automatically
  • Believing config files replace writing tests
  • Assuming config files skip tests to speed up runs
2. Which of the following is the correct way to specify a marker in pytest.ini to standardize test selection?
easy
A. [pytest] mark = slow: marks tests as slow
B. [pytest] markers slow: marks tests as slow
C. [pytest] markers = slow: marks tests as slow
D. [pytest] markers = slow marks tests as slow

Solution

  1. Step 1: Recall the correct syntax for markers in pytest.ini

    The correct section header is [pytest] and the key is markers with an equals sign and description.
  2. Step 2: Identify the properly formatted option

    [pytest] markers = slow: marks tests as slow uses markers = slow: marks tests as slow, which matches pytest documentation syntax.
  3. Final Answer:

    [pytest] markers = slow: marks tests as slow -> Option C
  4. Quick Check:

    Correct marker syntax = B [OK]
Hint: Markers use 'markers =' under [pytest] section [OK]
Common Mistakes:
  • Using 'mark' instead of 'markers'
  • Missing equals sign after 'markers'
  • Not enclosing section header in brackets
3. Given this pytest.ini content:
[pytest]
addopts = -v --maxfail=2

What will happen when you run pytest on a test suite with 5 failing tests?
medium
A. Pytest will ignore failures and pass all tests.
B. Pytest will run all tests and show minimal output.
C. Pytest will stop after 5 failures and show verbose output.
D. Pytest will stop after 2 failures and show verbose output.

Solution

  1. Step 1: Interpret the addopts options

    The option -v means verbose output. The option --maxfail=2 means pytest stops after 2 failures.
  2. Step 2: Predict test run behavior

    When running tests with 5 failures, pytest will stop after the second failure and show detailed output because of -v.
  3. Final Answer:

    Pytest will stop after 2 failures and show verbose output. -> Option D
  4. Quick Check:

    addopts '-v --maxfail=2' = stop at 2 fails + verbose [OK]
Hint: maxfail limits failures; -v shows details [OK]
Common Mistakes:
  • Thinking pytest runs all tests despite maxfail
  • Confusing verbose with minimal output
  • Assuming maxfail=2 means stop after 5 failures
4. You added filterwarnings = ignore::DeprecationWarning in pytest.ini but still see deprecation warnings during test runs. What is the likely cause?
medium
A. You need to add --disable-warnings to the command line instead.
B. The filterwarnings option is misspelled or misplaced in the config file.
C. Deprecation warnings cannot be ignored in pytest.
D. Pytest does not support filtering warnings via configuration.

Solution

  1. Step 1: Verify correct spelling and placement of filterwarnings

    The option must be spelled exactly as filterwarnings under the [pytest] section in pytest.ini.
  2. Step 2: Understand pytest's support for warning filters

    Pytest supports filtering warnings via config. If warnings still appear, likely the option is misspelled or in wrong section.
  3. Final Answer:

    The filterwarnings option is misspelled or misplaced in the config file. -> Option B
  4. Quick Check:

    Correct spelling and placement = D [OK]
Hint: Check spelling and section for config options [OK]
Common Mistakes:
  • Assuming pytest ignores config filters
  • Using wrong option names
  • Not placing options under [pytest] section
5. You want to ensure all tests marked @pytest.mark.slow are skipped by default unless explicitly included. Which pytest.ini configuration achieves this standardized behavior?
hard
A. [pytest] markers = slow: marks slow tests addopts = -m 'not slow'
B. [pytest] markers = slow: marks slow tests addopts = -m slow
C. [pytest] markers = slow: marks slow tests addopts = --skip slow
D. [pytest] markers = slow: marks slow tests addopts = --disable slow

Solution

  1. Step 1: Understand marker usage and addopts filtering

    To skip tests marked slow by default, use -m 'not slow' to run tests not marked slow.
  2. Step 2: Identify correct addopts syntax

    [pytest] markers = slow: marks slow tests addopts = -m 'not slow' uses addopts = -m 'not slow', which tells pytest to exclude slow tests unless overridden.
  3. Final Answer:

    [pytest] markers = slow: marks slow tests addopts = -m 'not slow' -> Option A
  4. Quick Check:

    Exclude slow tests by default = C [OK]
Hint: Use '-m not marker' to skip marked tests by default [OK]
Common Mistakes:
  • Using '-m slow' runs only slow tests, not skips them
  • Trying unsupported options like --skip or --disable
  • Not defining the marker in pytest.ini
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