0
0
PyTesttesting~10 mins

pytest-xdist for parallel execution - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test runs multiple simple tests in parallel using pytest-xdist. It verifies that tests execute concurrently and all pass successfully.

Test Code - pytest
PyTest
import pytest
import time

def test_sleep_1():
    time.sleep(1)
    assert True

def test_sleep_2():
    time.sleep(1)
    assert True

def test_sleep_3():
    time.sleep(1)
    assert True

# Run this with: pytest -n 3 test_parallel.py
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test runner starts with pytest-xdist using 3 workerspytest-xdist initializes 3 parallel workers-PASS
2Worker 1 runs test_sleep_1, sleeps 1 secondtest_sleep_1 is executing in worker 1-PASS
3Worker 2 runs test_sleep_2, sleeps 1 secondtest_sleep_2 is executing in worker 2-PASS
4Worker 3 runs test_sleep_3, sleeps 1 secondtest_sleep_3 is executing in worker 3-PASS
5Each test asserts True after sleepAll tests completed their sleep and assertionassert True is True for all testsPASS
6pytest-xdist collects results from all workersTest results aggregatedAll tests passedPASS
Failure Scenario
Failing Condition: One or more tests fail their assertion or worker crashes
Execution Trace Quiz - 3 Questions
Test your understanding
What does pytest-xdist do in this test?
ASkips tests that take longer than 1 second
BRuns tests sequentially in one process
CRuns tests in parallel using multiple workers
DRuns tests only on a single CPU core
Key Result
Using pytest-xdist allows running tests in parallel, which speeds up test execution especially when tests are independent and involve waiting or delays.