0
0
PyTesttesting~8 mins

Fixture factories in PyTest - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Fixture factories
Folder Structure
tests/
├── test_user.py
├── test_order.py
├── conftest.py
utils/
├── data_factories.py
├── helpers.py

Test Framework Layers
  • Fixture Factories Layer: Located in utils/data_factories.py, this layer contains factory functions that create test data objects or test fixtures dynamically.
  • Fixtures Layer: Defined in conftest.py, it uses fixture factories to provide reusable test data or setup for tests.
  • Test Layer: Test files inside tests/ folder use fixtures to run test cases.
  • Utilities Layer: Helper functions and common utilities in utils/helpers.py support test logic and data creation.
Configuration Patterns
  • Environment Handling: Use environment variables or pytest.ini to select test environment (dev, staging, prod).
  • Fixture Parameters: Fixture factories accept parameters to customize created data for different test scenarios.
  • Credentials: Store sensitive data securely outside code, inject via fixtures or environment variables.
  • Browser or External Service Config: Use pytest command line options or config files to select browsers or service endpoints.
Test Reporting and CI/CD Integration
  • Use pytest built-in reporting with options like --junitxml=report.xml for CI systems.
  • Integrate with CI tools (GitHub Actions, Jenkins) to run tests automatically on code changes.
  • Use plugins like pytest-html for readable HTML reports.
  • Ensure fixture factories produce consistent data to avoid flaky tests and improve report reliability.
Best Practices
  • Keep Fixture Factories Simple: Each factory creates one type of test data object with clear parameters.
  • Reuse Factories in Fixtures: Use fixture factories inside conftest.py fixtures to avoid duplication.
  • Parameterize Factories: Allow customization of test data to cover multiple scenarios without rewriting code.
  • Isolate Test Data: Factories should create fresh data for each test to prevent side effects.
  • Document Factories: Clearly explain what data each factory produces and its parameters.
Self Check

Where in this framework structure would you add a new fixture factory for creating Product test data objects?

Key Result
Use fixture factories in a utilities layer to create reusable, customizable test data for pytest fixtures.