How to Run Pytest Tests in Parallel?

Better Stack Team
Updated on July 18, 2024

To run Pytest tests in parallel, install the pytest-xdist plugin:

 
pip install pytest-xdist

Use the following command to run tests in parallel:

 
pytest -n auto

The -n auto option tells Pytest to distribute the tests across as many CPUs as your machine has available.

If you want more control and wish to specify the number of CPUs yourself, you can pass a number instead of auto:

 
pytest -n <NUM>