Pytest runs tests in parallel
Pytest Running Tests in Parallel
By default, pytest runs tests sequentially. In real-world scenarios, a test suite might have many test files, each with a number of tests. This can lead to significant execution time. To overcome this, pytest provides an option to run tests in parallel.
To do this, we first need to install the pytest-xdist plugin.
Install pytest-xdist by running the following command —
pip install pytest-xdist
Now, we can run tests using the syntax pytest -n <num>
.
pytest -n 3
-n <num>
...
When there are only a few tests to run, we won’t see much of a difference in time. However, when the test suite is large, it becomes significant.