Calmcode - pytest tricks: parallel xdist

Speed up Slow Pytest

1 2 3 4 5 6 7 8 9 10

When projects grow bigger, usually pytest becomes slower as a result. Most of the unit tests are independant of eachother though. That means that you could run most of them in parallel. There are a couple of plugins in pytest that make this easy but we're going to explore pytest-xdist.

You will first need to install it.

python -m pip install pytest-xdist

Now that it is installed you should be able to run pytest with an extra -n flag.

# This will tell pytest to use two CPU cores
pytest -n 2
# This will tell pytest to take all available CPU cores
pytest -n auto

Remember that when code runs in parallel, we should expect some overhead. So even if you have eight cores on your machine, you shouldn't expect an 8x speedup.

GitHub Actions

If you're using Github Actions you may also use this plugin to speed up your continuous integration pipelines. The standard Linux runners come with two CPUs so if your tests take a while; this can provide a decent speedup.