0
0
PyTesttesting~8 mins

Marker registration in PyTest - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Marker registration
Folder Structure
test_project/
├── tests/
│   ├── test_login.py
│   ├── test_registration.py
│   └── test_payment.py
├── pytest.ini
├── conftest.py
└── utils/
    └── helpers.py
Test Framework Layers
  • Tests: Located in tests/ folder, contain test functions using pytest markers to categorize tests.
  • Configuration: pytest.ini file registers custom markers and sets pytest options.
  • Fixtures & Hooks: conftest.py defines fixtures and hooks used across tests.
  • Utilities: Helper functions and reusable code in utils/ folder.
Configuration Patterns

The pytest.ini file is used to register custom markers to avoid warnings and enable marker-based test selection.

[pytest]
markers =
    smoke: mark a test as a smoke test
    regression: mark a test as a regression test
    slow: mark a test as slow running

Use command line options to run tests by marker, e.g., pytest -m smoke.

conftest.py can define fixtures that tests with markers can use.

Test Reporting and CI/CD Integration
  • Use pytest's built-in reporting with --junitxml=report.xml to generate XML reports for CI tools.
  • Integrate with CI/CD pipelines (GitHub Actions, Jenkins, GitLab CI) to run tests automatically on commits.
  • Use markers to selectively run tests in CI, e.g., run only smoke tests on pull requests.
  • Combine with pytest-html plugin for readable HTML reports.
Best Practices
  1. Always register custom markers in pytest.ini to avoid warnings and improve clarity.
  2. Use descriptive marker names that clearly indicate test purpose (e.g., smoke, regression, slow).
  3. Keep marker usage consistent across tests to enable easy filtering and selective runs.
  4. Document marker meanings in project README or test documentation for team clarity.
  5. Use markers to group tests logically and speed up test runs during development or CI.
Self Check

Where in this folder structure would you add a new marker called api to categorize API tests?

Key Result
Register custom pytest markers in pytest.ini to organize and selectively run tests efficiently.