0
0
PyTesttesting~8 mins

Registering markers in pytest.ini - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Registering markers in pytest.ini
Folder Structure
project-root/
├── tests/
│   ├── test_login.py
│   ├── test_registration.py
│   └── test_profile.py
├── pytest.ini
├── conftest.py
└── utils/
    └── helpers.py
  
Test Framework Layers
  • Configuration Layer: pytest.ini file where markers are registered and test settings are defined.
  • Test Layer: tests/ folder containing test files using markers to categorize tests.
  • Fixtures and Hooks: conftest.py for shared fixtures and hooks.
  • Utilities Layer: utils/ folder for helper functions and reusable code.
Configuration Patterns

The pytest.ini file is used to register custom markers to avoid warnings and improve test organization.

[pytest]
markers =
    smoke: Quick smoke tests
    regression: Full regression tests
    slow: Tests that take longer time
  

This file also configures other pytest options like test paths or addopts.

Test Reporting and CI/CD Integration

Markers help filter tests in CI pipelines, e.g., run only smoke tests on every commit.

Example CI command to run smoke tests only:

pytest -m smoke --junitxml=reports/smoke-results.xml

Reports can be integrated with CI tools like Jenkins, GitHub Actions, or GitLab CI for pass/fail status and test details.

Best Practices
  • Always register custom markers in pytest.ini to avoid warnings and document their purpose.
  • Use meaningful marker names that clearly describe the test category or behavior.
  • Keep pytest.ini simple and focused on configuration only.
  • Use markers to selectively run tests in different environments or stages (smoke, regression, slow).
  • Combine markers with fixtures for flexible test setups.
Self Check

Where in this framework structure would you add a new marker called api for tests that target API endpoints?

Key Result
Register custom markers in pytest.ini to organize and filter tests effectively.