Categories
Automated Testing Python Selenium

How to run parallel test with pytest

Parallel test execution involves running multiple tests simultaneously, significantly reducing the total testing duration.

This method spreads tests across various resources, like multiple CPU cores, threads, or different machines, to execute them simultaneously.

Setup the environment

First, let’s install the following 2 plugins:

pip install pytest-xdist

and

pip install pytest-forked

Check if the plugins were added to your interpreter

How to run multiple tests in parallel

Go to Run/Debug Configurations and add ‘-n 2’ to the Additional Arguments input. In my example, I also have the argument for the final test report (–html=report.html)

In the above example, when I run my test cases the system will create 2 workers. You can imagine a queue, where you have an X number of test scenarios, and each worker will pick the next available one. Change the value after -n to setup a different number of workers. (e.g -n 3 for 3 workers and so on)

How it works

The total number of test cases is based on the count of test_*.py files and the number of data sets in your list.

For instance, if there’s only one test_*.py file and you provide a list with 3 data sets, the total number of tests will be 3. You can then assign 3 workers to run these tests concurrently.