Why configuration standardizes test behavior in PyTest - Automation Benefits in Action
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.
Now add data-driven testing with 3 different inputs to the smoke test and verify all run with '-m smoke'
Practice
pytest.ini configuration file important for test behavior?Solution
Step 1: Understand the role of configuration files
Configuration files likepytest.inidefine settings that control how tests run, such as markers, test paths, or warnings.Step 2: Recognize the benefit of standardization
By setting options in one place, all tests behave consistently across different runs and environments.Final Answer:
It sets consistent options so tests run the same way every time. -> Option AQuick Check:
Configuration standardizes test behavior = A [OK]
- Thinking config files fix test code errors automatically
- Believing config files replace writing tests
- Assuming config files skip tests to speed up runs
pytest.ini to standardize test selection?Solution
Step 1: Recall the correct syntax for markers in pytest.ini
The correct section header is[pytest]and the key ismarkerswith an equals sign and description.Step 2: Identify the properly formatted option
[pytest] markers = slow: marks tests as slow usesmarkers = slow: marks tests as slow, which matches pytest documentation syntax.Final Answer:
[pytest] markers = slow: marks tests as slow -> Option CQuick Check:
Correct marker syntax = B [OK]
- Using 'mark' instead of 'markers'
- Missing equals sign after 'markers'
- Not enclosing section header in brackets
pytest.ini content:[pytest] addopts = -v --maxfail=2
What will happen when you run
pytest on a test suite with 5 failing tests?Solution
Step 1: Interpret the
The optionaddoptsoptions-vmeans verbose output. The option--maxfail=2means pytest stops after 2 failures.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.Final Answer:
Pytest will stop after 2 failures and show verbose output. -> Option DQuick Check:
addopts '-v --maxfail=2' = stop at 2 fails + verbose [OK]
- Thinking pytest runs all tests despite maxfail
- Confusing verbose with minimal output
- Assuming maxfail=2 means stop after 5 failures
filterwarnings = ignore::DeprecationWarning in pytest.ini but still see deprecation warnings during test runs. What is the likely cause?Solution
Step 1: Verify correct spelling and placement of
The option must be spelled exactly asfilterwarningsfilterwarningsunder the[pytest]section inpytest.ini.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.Final Answer:
Thefilterwarningsoption is misspelled or misplaced in the config file. -> Option BQuick Check:
Correct spelling and placement = D [OK]
- Assuming pytest ignores config filters
- Using wrong option names
- Not placing options under [pytest] section
@pytest.mark.slow are skipped by default unless explicitly included. Which pytest.ini configuration achieves this standardized behavior?Solution
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.Step 2: Identify correct addopts syntax
[pytest] markers = slow: marks slow tests addopts = -m 'not slow' usesaddopts = -m 'not slow', which tells pytest to exclude slow tests unless overridden.Final Answer:
[pytest] markers = slow: marks slow tests addopts = -m 'not slow' -> Option AQuick Check:
Exclude slow tests by default = C [OK]
- Using '-m slow' runs only slow tests, not skips them
- Trying unsupported options like --skip or --disable
- Not defining the marker in pytest.ini
Ready to prove it?
Pass the test and this pattern moves unknown
