0
0
PyTesttesting~8 mins

Fixture request object in PyTest - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Fixture request object
Folder Structure
tests/
├── test_example.py          # Test files using fixtures
├── conftest.py              # Shared fixtures and hooks
utilities/
├── helpers.py               # Helper functions
configs/
├── config.yaml              # Environment and test data configs
Test Framework Layers
  • Fixtures Layer: Defined in conftest.py, provides reusable setup and teardown logic. Uses the request object to access test context and parameters.
  • Test Layer: Test files in tests/ folder use fixtures by declaring them as function arguments.
  • Utilities Layer: Helper functions and common utilities to support tests and fixtures.
  • Configuration Layer: External config files (e.g., YAML) to manage environment variables, credentials, and test data.
Configuration Patterns
  • Use config.yaml or similar file to store environment URLs, credentials, and other settings.
  • Load config in conftest.py fixture to provide environment-specific data.
  • Use request.config.getoption() to access command line options like --env or --browser.
  • Pass parameters dynamically to fixtures using request.param for data-driven tests.
Test Reporting and CI/CD Integration
  • Use pytest built-in reporting with pytest --junitxml=report.xml for XML reports.
  • Integrate with CI/CD tools (GitHub Actions, Jenkins) to run tests automatically on code push.
  • Use plugins like pytest-html for readable HTML reports.
  • Log fixture setup and teardown steps using logging for easier debugging.
Best Practices
  • Define fixtures in conftest.py to share them across multiple test files.
  • Use the request object to access test context, parameters, and dynamically control fixture behavior.
  • Keep fixtures small and focused on a single responsibility (setup or teardown).
  • Use scope parameter (function, module, session) to optimize fixture reuse.
  • Use parametrized fixtures with request.param for flexible, data-driven testing.
Self Check

Where in this framework structure would you add a new fixture that needs to access the current test name and parameters?

Key Result
Use pytest's fixture request object in conftest.py to access test context and parameters for flexible, reusable setup.