0
0
PyTesttesting~8 mins

Parametrize with indirect fixtures in PyTest - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Parametrize with indirect fixtures
Folder Structure
tests/
├── test_example.py          # Test files using indirect fixtures
├── conftest.py              # Fixtures including indirect fixtures
utils/
├── helpers.py               # Helper functions for tests
configs/
├── config.yaml              # Environment and test data configurations
reports/
├── latest_report.html       # Generated test reports
Test Framework Layers
  • Fixtures Layer: Defined in conftest.py, includes indirect fixtures that accept parameters to setup test data or environment dynamically.
  • Test Layer: Test files in tests/ use @pytest.mark.parametrize with indirect=True to pass parameters to fixtures.
  • Utilities Layer: Helper functions in utils/helpers.py support data preparation or common operations.
  • Configuration Layer: Configuration files like configs/config.yaml store environment variables, credentials, or test data references.
Configuration Patterns
  • Use pytest.ini or config.yaml to define environment-specific settings.
  • Pass parameters to indirect fixtures via @pytest.mark.parametrize with indirect=True to dynamically control fixture behavior.
  • Keep sensitive data like credentials in environment variables or secured config files, accessed by fixtures.
  • Use conftest.py to centralize fixture definitions and parameter handling.
Test Reporting and CI/CD Integration
  • Generate HTML or XML reports using pytest --html=reports/latest_report.html or pytest --junitxml=reports/results.xml.
  • Integrate with CI/CD pipelines (GitHub Actions, Jenkins, GitLab CI) to run tests automatically on code changes.
  • Use test reports to verify which parameter sets passed or failed, especially useful with indirect fixtures handling multiple scenarios.
  • Configure CI to fail builds on test failures to maintain quality.
Best Practices
  • Keep fixtures simple and reusable: Design indirect fixtures to accept parameters cleanly for different test scenarios.
  • Use descriptive parameter names: Helps understand what each test case covers when using parametrize.
  • Centralize fixture logic: Put fixtures in conftest.py to share across tests and avoid duplication.
  • Separate test data from test code: Use config files or constants to manage test inputs passed to indirect fixtures.
  • Use explicit indirect=True only when needed: Avoid confusion by only marking parameters indirect if they should be passed to fixtures.
Self Check

Where in this framework structure would you add a new indirect fixture to handle user login data for multiple test scenarios?

Key Result
Use indirect fixtures in conftest.py with @pytest.mark.parametrize(indirect=True) to pass parameters dynamically for flexible test setups.