0
0
PyTesttesting~15 mins

Parallel execution in CI in PyTest - Build an Automation Script

Choose your learning style9 modes available
Run pytest tests in parallel on CI pipeline
Preconditions (3)
Step 1: Configure the CI pipeline to install dependencies including pytest and pytest-xdist
Step 2: Run pytest with the -n option to enable parallel execution, e.g., pytest -n 4
Step 3: Observe the test execution running tests in parallel
Step 4: Verify that all tests pass successfully
Step 5: Check the CI test report for correct test results
✅ Expected Result: Tests run in parallel on 4 workers, all tests pass, and CI shows a successful test report
Automation Requirements - pytest
Assertions Needed:
All tests pass without errors or failures
Test execution time is reduced compared to sequential run
Best Practices:
Use pytest-xdist plugin for parallel execution
Avoid shared state or use fixtures with proper scope to prevent conflicts
Use explicit waits or synchronization if tests interact with shared resources
Configure CI pipeline to install dependencies and run tests with parallel flag
Automated Solution
PyTest
import pytest

# Sample test file: test_sample.py

def test_one():
    assert 1 + 1 == 2

def test_two():
    assert 'hello'.upper() == 'HELLO'

def test_three():
    assert len([1, 2, 3]) == 3

# To run in CI, use the command:
# pytest -n 4 --dist=loadscope

# This requires pytest-xdist installed:
# pip install pytest-xdist

# In CI pipeline script (example):
# python -m pip install -r requirements.txt
# pytest -n 4 --dist=loadscope

The code defines simple pytest tests to demonstrate parallel execution.

We use the -n 4 option with pytest to run tests on 4 parallel workers.

The --dist=loadscope option helps distribute tests by scope to avoid conflicts.

In the CI pipeline, dependencies including pytest and pytest-xdist must be installed before running tests.

This setup reduces total test time by running tests concurrently.

Common Mistakes - 3 Pitfalls
Not installing pytest-xdist plugin in CI environment
Running tests in parallel that share state without isolation
Hardcoding number of workers without considering CI environment resources
Bonus Challenge

Now add data-driven testing with 3 different inputs and run them in parallel in CI

Show Hint