Test Overview
This test runs multiple simple tests in parallel using pytest-xdist. It verifies that tests execute concurrently and all pass successfully.
This test runs multiple simple tests in parallel using pytest-xdist. It verifies that tests execute concurrently and all pass successfully.
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
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test runner starts with pytest-xdist using 3 workers | pytest-xdist initializes 3 parallel workers | — | PASS |
| 2 | Worker 1 runs test_sleep_1, sleeps 1 second | test_sleep_1 is executing in worker 1 | — | PASS |
| 3 | Worker 2 runs test_sleep_2, sleeps 1 second | test_sleep_2 is executing in worker 2 | — | PASS |
| 4 | Worker 3 runs test_sleep_3, sleeps 1 second | test_sleep_3 is executing in worker 3 | — | PASS |
| 5 | Each test asserts True after sleep | All tests completed their sleep and assertion | assert True is True for all tests | PASS |
| 6 | pytest-xdist collects results from all workers | Test results aggregated | All tests passed | PASS |
pytest-xdist in test execution?-n.pytest -n 4 runs tests with 4 parallel workers.pytest -n 3 test_sample.py, what happens during test execution?-n 3 option tells pytest-xdist to use 3 workers for parallel execution.pytest -n 2 but get an error: ERROR: unknown option: -n. What is the likely cause?-n is unknown, meaning pytest does not recognize the option.-n option is provided by pytest-xdist plugin. If it's not installed, pytest won't recognize it.pytest -n auto. What is a good approach to fix this?@pytest.mark.serial (or similar) ensures they run one at a time, avoiding conflicts.@pytest.mark.serial to tests that share state to run them sequentially -> Option CPass the test and this pattern moves unknown