Parallel Test Execution with Pytest

In this short but useful tutorial, we'll learn how to install and configure pytest-xdist plugin to execute our test scripts in parallel.

Web UI tests are notoriously slow. When compared to typical tests in the industry, our DuckDuckGo search test is a bit of an anomaly for being so short. It’s common for companies to have hundreds to thousands of web UI tests that average about a minute per test. Consider the scale: 100 tests would take about 1 hour 40 minutes to run. 1000 tests would take over 16 hours to run! That’s too much time for a continuous integration / continuous delivery pipeline.

The only way to achieve truly continuous testing with web UI tests is to run them in parallel. Thankfully, that’s just a pytest plugin away. pytest-xdist lets you scale up by increasing the test thread count and scale out by distributing test execution to remote machines. Just be careful to avoid collisions in both the code and the system under test.

Installing pytest-html

Installing pytest-xdist is a very simple task, we just use the command below:

pipenv install pytest-xdist --dev

That's all for the installation! It comes with a very simple yet useful out-of-the-box setup.

Executing test in parallel with pytest-xdist

To run our test in parallel, we just include the -n flag in the execution command with the number of threads we want to use for our execution.

Executing the test in parallel:

pipenv run python -m pytest tests/test_web.py -n 4

There are additional configurations that we can use to manipulate our report, which can be found in the official User Guide