0
0
PyTesttesting~15 mins

pytest.ini configuration - Build an Automation Script

Choose your learning style9 modes available
Configure pytest.ini to set test markers and default options
Preconditions (2)
Step 1: Create a pytest.ini file in the project root directory
Step 2: Add a marker named 'smoke' with a description
Step 3: Set the default command line option to show detailed test output (-v)
Step 4: Run pytest without any command line arguments
Step 5: Verify that tests run with verbose output
Step 6: Verify that the 'smoke' marker is recognized by pytest
✅ Expected Result: pytest runs tests with verbose output by default and recognizes the 'smoke' marker without errors
Automation Requirements - pytest
Assertions Needed:
Verify pytest runs with verbose output by default
Verify pytest recognizes the 'smoke' marker without warnings
Best Practices:
Use a pytest.ini file in the root directory for configuration
Define markers under [pytest] section with clear descriptions
Avoid hardcoding command line options in test scripts
Use pytest's built-in capabilities to check for warnings
Automated Solution
PyTest
[pytest]
markers =
    smoke: mark a test as a smoke test
addopts = -v

# test_sample.py
import pytest

@pytest.mark.smoke
def test_example():
    assert 1 + 1 == 2

# test_pytest_ini.py
import subprocess
import sys
import re

def test_pytest_ini_configuration():
    # Run pytest in the current directory without extra args
    result = subprocess.run([sys.executable, '-m', 'pytest'], capture_output=True, text=True)
    output = result.stdout + result.stderr

    # Check verbose output: look for 'test_example' in output and '-v' effect (e.g., test names shown)
    assert 'test_example' in output
    assert re.search(r'collected \d+ items', output)

    # Check no warnings about unknown markers
    assert 'PytestUnknownMarkWarning' not in output
    assert 'WARNING' not in output or 'smoke' in output

The pytest.ini file sets the smoke marker and adds -v to addopts for verbose output by default.

The test_sample.py file contains a simple test marked with @pytest.mark.smoke.

The automation script test_pytest_ini.py runs pytest as a subprocess without extra arguments to verify the configuration.

It asserts that the test name appears in the output, indicating verbose mode is active.

It also checks that no warnings about unknown markers appear, confirming the marker is recognized.

This approach tests the pytest.ini configuration end-to-end without modifying test runner commands.

Common Mistakes - 4 Pitfalls
{'mistake': 'Not placing pytest.ini in the project root directory', 'why_bad': "pytest will not find the configuration file and default options or markers won't be applied", 'correct_approach': 'Always place pytest.ini in the root directory where pytest is run'}
Defining markers without descriptions or under wrong section
Hardcoding command line options in test scripts instead of pytest.ini
Ignoring warnings about unknown markers during test runs
Bonus Challenge

Now add a second marker named 'regression' and verify both markers are recognized without warnings

Show Hint