Running tests one by one can take a long time. pytest-xdist helps run tests at the same time to save time.
pytest-xdist for parallel execution
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.
pytest -n 4pytest -n auto
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.
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
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.
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.
Practice
pytest-xdist in test execution?Solution
Step 1: Understand pytest-xdist functionality
pytest-xdist allows running tests simultaneously on multiple CPU cores.Step 2: Identify the benefit of parallel execution
Running tests in parallel reduces the total time needed to complete all tests.Final Answer:
To run tests in parallel and reduce total test time -> Option AQuick Check:
pytest-xdist = parallel test execution [OK]
- Confusing xdist with report generation tools
- Thinking xdist fixes test code automatically
- Assuming xdist runs tests sequentially
Solution
Step 1: Recall pytest-xdist syntax for parallelism
The correct flag to specify number of parallel workers is-n.Step 2: Match the correct command
pytest -n 4runs tests with 4 parallel workers.Final Answer:
pytest -n 4 -> Option AQuick Check:
-n sets workers = C [OK]
- Using incorrect flags like --workers or --parallel
- Confusing -p (plugin) with -n
- Omitting the number after -n
pytest -n 3 test_sample.py, what happens during test execution?Solution
Step 1: Analyze the command options
The-n 3option tells pytest-xdist to use 3 workers for parallel execution.Step 2: Understand effect on test_sample.py
All tests in test_sample.py are distributed across 3 CPU cores and run simultaneously.Final Answer:
Tests in test_sample.py run in parallel using 3 CPU cores -> Option DQuick Check:
-n 3 means 3 parallel workers = A [OK]
- Thinking only 3 tests run instead of all tests
- Assuming tests run sequentially despite -n
- Confusing number of workers with number of tests
pytest -n 2 but get an error: ERROR: unknown option: -n. What is the likely cause?Solution
Step 1: Understand the error message
The error says-nis unknown, meaning pytest does not recognize the option.Step 2: Identify cause related to pytest-xdist
The-noption is provided by pytest-xdist plugin. If it's not installed, pytest won't recognize it.Final Answer:
pytest-xdist plugin is not installed -> Option BQuick Check:
Unknown -n means missing xdist plugin = B [OK]
- Assuming -n flag is built-in pytest option
- Trying to run with sudo unnecessarily
- Ignoring plugin installation step
pytest -n auto. What is a good approach to fix this?Solution
Step 1: Understand cause of flaky failures
Tests sharing state or resources can fail when run in parallel due to conflicts.Step 2: Use markers to run conflicting tests sequentially
Marking such tests with@pytest.mark.serial(or similar) ensures they run one at a time, avoiding conflicts.Step 3: Keep parallelism for other tests
This approach balances speed and stability by running safe tests in parallel and conflicting ones sequentially.Final Answer:
Add@pytest.mark.serialto tests that share state to run them sequentially -> Option CQuick Check:
Mark shared-state tests serial to fix flakes = A [OK]
- Blindly increasing workers causing more flakes
- Disabling parallelism losing speed benefits
- Using -n 1 which is just sequential run
Ready to prove it?
Pass the test and this pattern moves unknown
