0
0
PyTesttesting~8 mins

Why markers categorize and control tests in PyTest - Framework Benefits

Choose your learning style9 modes available
Framework Mode - Why markers categorize and control tests
Folder Structure of a Pytest Project Using Markers
tests/
├── test_login.py
├── test_shopping_cart.py
├── test_checkout.py
├── conftest.py
└── pytest.ini
Test Framework Layers in Pytest with Markers
  • Tests: Test files inside tests/ folder, e.g., test_login.py. Markers categorize tests by feature or type.
  • Fixtures: Setup and teardown code in conftest.py to prepare test environments.
  • Markers: Labels added to tests to group or control execution, e.g., @pytest.mark.smoke.
  • Configuration: Marker definitions and test options in pytest.ini.
  • Utilities: Helper functions or modules to support tests.
Configuration Patterns for Markers in Pytest

Markers must be declared in pytest.ini or markers.ini to avoid warnings and to document their purpose.

# pytest.ini
[pytest]
markers =
    smoke: quick tests to check basic functionality
    regression: full tests for all features
    login: tests related to login functionality

Run tests by marker using command line options:

pytest -m smoke
pytest -m "not regression"

Use environment variables or config files to control which markers run in different environments.

Test Reporting and CI/CD Integration
  • Reports show which markers ran and their results, helping track test coverage by category.
  • CI/CD pipelines can run specific marker groups, e.g., smoke tests on every commit, full regression nightly.
  • Use plugins like pytest-html or pytest-junitxml to generate readable reports including marker info.
  • Markers help control test execution time and focus, improving pipeline speed and feedback.
Best Practices for Using Markers in Pytest
  1. Define markers clearly: Always declare markers in pytest.ini with descriptions.
  2. Use markers to group tests logically: By feature, priority, or test type (smoke, regression, slow).
  3. Control test runs: Use markers to select or exclude tests easily from command line or CI.
  4. Keep markers consistent: Avoid overlapping or ambiguous marker names.
  5. Document marker usage: Help team members understand test categories and execution control.
Self-Check Question

Where in this framework structure would you add a new marker for tests related to the "payment" feature?

Key Result
Markers in pytest categorize tests to organize and control which tests run, improving test management and execution.