Running tests one by one can take a long time. pytest-xdist helps run tests at the same time to save time.
0
0
pytest-xdist for parallel execution
Introduction
You have many tests and want to finish them faster.
You want to use all your computer's CPU cores to run tests.
You want to run tests in parallel on different machines.
You want to reduce waiting time before seeing test results.
Syntax
PyTest
pytest -n <num_workers>
-n option tells pytest how many workers to use to run tests in parallel.
You can use auto to use all CPU cores automatically.
Examples
Run tests using 4 workers in parallel.
PyTest
pytest -n 4Run tests using all available CPU cores automatically.
PyTest
pytest -n auto
Sample Program
This example has 4 tests that each wait 1 second. Running with -n 4 runs all at the same time, so total time is about 1 second instead of 4.
PyTest
import time def test_one(): time.sleep(1) assert True def test_two(): time.sleep(1) assert True def test_three(): time.sleep(1) assert True def test_four(): time.sleep(1) assert True # Run this command in terminal: # pytest -n 4
OutputSuccess
Important Notes
Make sure your tests do not share or change the same data when running in parallel.
pytest-xdist must be installed with pip install pytest-xdist.
Some tests that depend on order or shared resources may fail when run in parallel.
Summary
pytest-xdist runs tests at the same time to save time.
Use pytest -n <num> to set how many tests run in parallel.
Good for speeding up large test suites on multi-core computers.