Pytest - Run Tests in Parallel



By default, pytest runs tests in sequential order. In a real scenario, a test suite will have a number of test files and each file will have a bunch of tests. This will lead to a large execution time. To overcome this, pytest provides us with an option to run tests in parallel.

For this, we need to first install the pytest-xdist plugin.

Install pytest-xdist by running the following command −

pip install pytest-xdist

Now, we can run tests by using the syntax pytest -n <num>

pytest -n 3

-n <num> runs the tests by using multiple workers, here it is 3.

We will not be having much time difference when there is only a few tests to run. However, it matters when the test suite is large.

Advertisements