0
0
PyTesttesting~8 mins

Parallel execution in CI in PyTest - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Parallel execution in CI
Folder Structure
test_project/
├── tests/
│   ├── test_login.py
│   ├── test_checkout.py
│   └── test_profile.py
├── utils/
│   └── helpers.py
├── conftest.py
├── pytest.ini
├── requirements.txt
└── ci/
    └── ci_pipeline.yml
Test Framework Layers
  • Tests: Located in tests/, contain test cases organized by feature.
  • Fixtures & Config: conftest.py holds shared setup and teardown code, fixtures for test data and browser sessions.
  • Utilities: Helper functions in utils/ to support tests (e.g., data generators).
  • Configuration: pytest.ini for pytest settings including markers and parallel execution options.
  • CI Pipeline: ci/ci_pipeline.yml defines Continuous Integration steps including parallel test runs.
Configuration Patterns
  • pytest.ini example to enable parallel runs with pytest-xdist:
    [pytest]
    addopts = -n auto --dist=loadscope
    markers =
        smoke: smoke tests
        regression: regression tests
    
  • Environment Variables: Use environment variables in conftest.py to switch environments (dev, staging, prod) and credentials securely.
  • CI Pipeline: In ci/ci_pipeline.yml, configure jobs to run tests in parallel using pytest-xdist and cache dependencies for speed.
Test Reporting and CI/CD Integration
  • Use pytest plugins like pytest-html or pytest-junitxml to generate readable reports after parallel runs.
  • Configure CI pipeline to collect and display test reports and fail the build if tests fail.
  • Integrate with CI tools (GitHub Actions, GitLab CI, Jenkins) to trigger tests on pull requests or merges.
  • Parallel execution reduces total test time, speeding feedback to developers.
Best Practices
  • Use pytest-xdist: This plugin enables easy parallel test execution with -n option.
  • Isolate Tests: Ensure tests do not share state or resources to avoid flaky failures during parallel runs.
  • Use Fixtures Wisely: Scope fixtures properly (function, class, module) to optimize setup time and avoid conflicts.
  • Configure CI for Parallelism: Set up CI jobs to run tests in parallel containers or runners for faster feedback.
  • Collect Reports: Merge parallel test reports for a single view of results.
Self Check

Where in this folder structure would you add a new fixture to provide a database connection for tests running in parallel?

Key Result
Use pytest-xdist plugin with proper fixture isolation to run tests in parallel within CI pipelines.