0
0
PyTesttesting~8 mins

Running tests by marker (-m) in PyTest - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Running tests by marker (-m)
Folder Structure
project-root/
├── tests/
│   ├── test_login.py
│   ├── test_signup.py
│   ├── test_profile.py
│   └── __init__.py
├── pytest.ini
├── conftest.py
└── utils/
    └── helpers.py
  
Test Framework Layers
  • Tests Layer: Contains test files with test functions or classes. Markers are applied here to categorize tests (e.g., @pytest.mark.smoke).
  • Fixtures & Config Layer: conftest.py holds fixtures and hooks shared across tests.
  • Utilities Layer: Helper functions and reusable code in utils/helpers.py.
  • Configuration Layer: pytest.ini defines markers and test run options.
Configuration Patterns

Use pytest.ini to declare custom markers to avoid warnings and organize tests:

[pytest]
markers =
    smoke: quick smoke tests
    regression: full regression tests
    slow: tests that take longer time

Run tests by marker using the -m option:

pytest -m smoke

This runs only tests marked with @pytest.mark.smoke.

Combine markers with and, or, and not:

pytest -m "smoke and not slow"

Use environment variables or command line options to select markers dynamically if needed.

Test Reporting and CI/CD Integration
  • Use pytest's built-in reporting to see which tests ran and their results.
  • Integrate with CI tools (GitHub Actions, Jenkins) to run marker-based tests selectively, e.g., smoke tests on pull requests.
  • Generate reports (JUnit XML, HTML) to visualize test outcomes filtered by markers.
  • Example GitHub Action step to run smoke tests:
    pytest -m smoke --junitxml=reports/smoke-results.xml
Best Practices
  • Always declare custom markers in pytest.ini to keep the framework clean and avoid warnings.
  • Use markers to group tests by purpose (smoke, regression, slow) for flexible test runs.
  • Keep marker names simple and meaningful for easy understanding by all team members.
  • Combine markers logically with and, or, and not to fine-tune test selection.
  • Integrate marker-based runs into CI pipelines to optimize test execution time and feedback speed.
Self Check

Where in this folder structure would you add a new test file for API tests that you want to mark as regression?

Key Result
Use pytest markers declared in pytest.ini to categorize and run tests selectively with the -m option.