0
0
PyTesttesting~10 mins

Parallel execution in CI in PyTest - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test runs multiple simple tests in parallel using pytest-xdist in a Continuous Integration (CI) environment. It verifies that tests execute concurrently and all pass successfully.

Test Code - pytest
PyTest
import pytest

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

def test_subtraction():
    assert 5 - 3 == 2

def test_multiplication():
    assert 3 * 4 == 12

def test_division():
    assert 10 / 2 == 5

# To run in parallel in CI, use: pytest -n 4
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test runner starts with pytest and xdist plugin using '-n 4' to run 4 tests in parallelCI environment initializes pytest with parallel workers-PASS
2pytest discovers 4 test functions: test_addition, test_subtraction, test_multiplication, test_divisionTest functions are ready to be executed concurrently-PASS
3Each test runs in parallel worker process: test_addition runs and asserts 1 + 1 == 2test_addition executes in worker 1assert 1 + 1 == 2PASS
4test_subtraction runs in parallel worker process and asserts 5 - 3 == 2test_subtraction executes in worker 2assert 5 - 3 == 2PASS
5test_multiplication runs in parallel worker process and asserts 3 * 4 == 12test_multiplication executes in worker 3assert 3 * 4 == 12PASS
6test_division runs in parallel worker process and asserts 10 / 2 == 5test_division executes in worker 4assert 10 / 2 == 5PASS
7pytest collects results from all parallel workers and aggregates test reportCI system receives all test resultsAll tests passedPASS
Failure Scenario
Failing Condition: One or more tests fail due to incorrect assertion or test error
Execution Trace Quiz - 3 Questions
Test your understanding
What does the '-n 4' option do when running pytest in CI?
ARuns 4 tests in parallel using separate worker processes
BRuns tests sequentially 4 times
CLimits the test run to 4 tests total
DSkips 4 tests during execution
Key Result
Running tests in parallel in CI speeds up feedback but requires tests to be independent and free of shared state to avoid flaky failures.