0
0
PyTesttesting~8 mins

pytest-xdist for parallel execution - Framework Patterns

Choose your learning style9 modes available
Framework Mode - pytest-xdist for parallel execution
Folder Structure
project-root/
├── tests/
│   ├── test_login.py
│   ├── test_checkout.py
│   └── test_profile.py
├── src/
│   └── app_code.py
├── utils/
│   └── helpers.py
├── conftest.py
├── pytest.ini
└── requirements.txt
    
Test Framework Layers
  • Test Cases: Located in tests/, contain test functions using pytest syntax.
  • Fixtures: Defined in conftest.py for setup and teardown, shared across tests.
  • Utilities: Helper functions and reusable code in utils/.
  • Configuration: pytest.ini for pytest settings including xdist options.
  • Application Code: Source code under src/.
Configuration Patterns

Use pytest.ini to configure pytest and xdist options:

[pytest]
addopts = -n auto  # Run tests in parallel using all CPU cores

# You can also specify markers, test paths, or other options here
markers =
    smoke: quick smoke tests
    regression: full regression suite
    

Manage environments and credentials using environment variables or separate config files loaded in fixtures.

Test Reporting and CI/CD Integration
  • Use pytest built-in reports or plugins like pytest-html for readable HTML reports.
  • Integrate with CI/CD tools (GitHub Actions, Jenkins, GitLab CI) to run tests with pytest -n auto for parallel execution.
  • Ensure test logs and reports are saved as artifacts for review.
  • Configure CI to install dependencies from requirements.txt before running tests.
Best Practices
  1. Use pytest-xdist to speed up test runs by running tests in parallel across CPU cores.
  2. Keep tests independent: Avoid shared state or dependencies between tests to prevent flaky results in parallel runs.
  3. Use fixtures wisely: Scope fixtures properly (function, module, session) to optimize setup time without conflicts.
  4. Configure pytest.ini for parallel options: Use -n auto to automatically use all cores or specify number of workers.
  5. Monitor resource usage: Parallel tests consume more CPU and memory; balance number of workers accordingly.
Self Check

Where in this framework structure would you add a new fixture that sets up a database connection for tests?

Key Result
Use pytest-xdist to run tests in parallel by configuring pytest.ini and keeping tests independent for faster, reliable test execution.