0
0
PyTesttesting~8 mins

Shared expensive resource patterns in PyTest - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Shared expensive resource patterns
Folder Structure
tests/
├── test_feature_a.py
├── test_feature_b.py
├── conftest.py          # Shared fixtures and resource setup
utilities/
├── db_helper.py         # Database connection helpers
├── api_client.py        # API client utilities
configs/
├── config.yaml          # Environment and credentials
reports/
├── latest_report.html
pytest.ini               # Pytest configuration file
Test Framework Layers
  • Fixtures Layer: Defined in conftest.py to manage shared expensive resources like database connections or web servers.
  • Test Layer: Test files in tests/ use fixtures to access shared resources without recreating them.
  • Utilities Layer: Helper modules in utilities/ provide reusable functions for resource management.
  • Configuration Layer: configs/config.yaml holds environment-specific settings and credentials.
Configuration Patterns
  • Use pytest.ini to set default options and markers.
  • Store environment variables and credentials securely in configs/config.yaml or environment variables.
  • Use pytest command line options or environment variables to select environments (e.g., dev, staging, prod).
  • Define fixtures with appropriate scopes (session, module, function) to control resource lifetime and sharing.
Test Reporting and CI/CD Integration
  • Use pytest-html plugin to generate HTML reports saved in reports/.
  • Integrate with CI/CD pipelines (GitHub Actions, Jenkins, GitLab CI) to run tests on each commit or pull request.
  • Configure CI to cache expensive resources or reuse containers to speed up tests.
  • Fail fast on critical resource setup failures to save time.
Best Practices for Shared Expensive Resource Patterns
  1. Use session scoped fixtures for resources that are costly to create and can be shared across all tests in a run.
  2. Clean up resources properly using finalizers or yield fixtures to avoid side effects between tests.
  3. Isolate tests logically by using module or function scoped fixtures when resource sharing is not safe.
  4. Parameterize fixtures to support different configurations without duplicating resource setup code.
  5. Keep fixtures simple and reusable to improve maintainability and readability.
Self Check

Where in this framework structure would you add a new fixture to share a web server instance across all tests?

Key Result
Use pytest session-scoped fixtures in conftest.py to share expensive resources efficiently across tests.