pytest-xdist

Introduction

By default, PyTest executes test cases one after another in a single process. While this works well for small projects, large Selenium automation suites can take a long time to complete.

The pytest-xdist plugin enables parallel test execution by running multiple tests simultaneously using multiple worker processes. This can dramatically reduce execution time, making it an essential tool for regression testing and CI/CD pipelines.

In this tutorial, you’ll learn what pytest-xdist is, how it works, how to install it, and how to execute Selenium tests in parallel.


What is pytest-xdist?

pytest-xdist is an official PyTest plugin that allows multiple test cases to run in parallel.

Instead of executing one test after another, PyTest distributes the tests across multiple worker processes.

For example:

Without pytest-xdist

Test 1
   │
   ▼
Test 2
   │
   ▼
Test 3

----------------------------

With pytest-xdist

Worker 1        Worker 2

Test 1          Test 2
   │               │
   ▼               ▼

         Test 3

Each worker executes tests independently, reducing the total execution time.


Why Use pytest-xdist?

Using pytest-xdist helps you:

  • Reduce test execution time.

  • Run multiple Selenium tests simultaneously.

  • Utilize multiple CPU cores.

  • Speed up regression testing.

  • Improve CI/CD pipeline performance.


Installing pytest-xdist

Install the plugin using pip:

py -3 -m pip install pytest-xdist

or

pip install pytest-xdist

After installation, PyTest recognizes the -n option.


Running Tests with pytest-xdist

Run tests using two worker processes:

pytest -s -n 2 42_examples/test_02_pytest_xdist.py

Run tests using all available CPU cores:

pytest -s -n auto

Here:

  • -s displays console output.

  • -n 2 creates two worker processes.

  • -n auto automatically chooses the number of workers based on your system’s CPU cores.


Example

# Topic: 42. Parallel Execution - pytest-xdist
# Practice site: n/a (project-level example)
# Run: pytest -s -n 2 42_examples/test_02_pytest_xdist.py
#
# pytest-xdist enables parallel workers with -n <count> or -n auto.
# Install with: py -3 -m pip install pytest-xdist


def test_pytest_xdist_worker_a():
    assert True


def test_pytest_xdist_worker_b():
    assert True

Understanding the Code

Simple Test Functions

def test_pytest_xdist_worker_a():

and

def test_pytest_xdist_worker_b():

These are two independent PyTest test functions.

When pytest-xdist is used, PyTest can assign each test to a different worker process, allowing them to execute simultaneously.


Verify the Test Result

assert True

The assertion always evaluates to True, so the test passes successfully.

The purpose of this example is not to test Selenium functionality but to demonstrate that multiple tests can run in parallel using pytest-xdist.


How pytest-xdist Works

Suppose you execute:

pytest -s -n 2

PyTest creates two worker processes.

For example:

Worker 1

✓ test_pytest_xdist_worker_a()

-----------------------

Worker 2

✓ test_pytest_xdist_worker_b()

Since both workers execute at the same time, the overall execution finishes faster than sequential execution.


Practical Example

Suppose your Selenium automation suite contains 300 test cases.

Instead of executing them sequentially, you run:

pytest -n auto

PyTest distributes the tests across multiple worker processes, reducing execution time significantly.


Automation Testing Example

Consider an e-commerce application with separate Selenium tests for:

  • Login

  • Registration

  • Product Search

  • Shopping Cart

  • Checkout

  • Payment

Using pytest-xdist, these independent tests can execute simultaneously on different workers, making the regression suite much faster.


Real-World Example

pytest-xdist is commonly used in:

  • Selenium automation frameworks

  • Large regression test suites

  • CI/CD pipelines

  • Jenkins

  • GitHub Actions

  • Azure DevOps

  • Cross-browser testing

  • Enterprise automation projects

Automation teams use it to shorten execution time and receive faster feedback after every build.


Advantages of pytest-xdist

  • Executes tests in parallel.

  • Reduces regression execution time.

  • Utilizes multiple CPU cores.

  • Improves CI/CD efficiency.

  • Works seamlessly with PyTest.

  • Easy to install and configure.


Common Mistakes Beginners Make

Forgetting to Install pytest-xdist

The -n option works only after installing the pytest-xdist plugin.

Without the plugin, PyTest reports an unknown command-line option.


Sharing a Single WebDriver Instance

When running Selenium tests in parallel, each test should create its own browser instance.

Sharing a single WebDriver between workers can cause unpredictable failures.


Assuming Every Test Can Run in Parallel

Tests that depend on shared files, shared databases, or shared global variables may interfere with one another.

Keep parallel tests independent.


Using Too Many Worker Processes

Creating more workers than your system can efficiently handle may reduce performance instead of improving it.


Best Practices

  • Install pytest-xdist before using the -n option.

  • Use -n auto to automatically select an appropriate number of workers.

  • Ensure each Selenium test creates its own WebDriver instance.

  • Keep tests independent and free from shared mutable state.

  • Use parallel execution for large regression suites and CI/CD pipelines.


Conclusion

pytest-xdist is one of the most useful PyTest plugins for Selenium automation. It enables parallel test execution by distributing tests across multiple worker processes, significantly reducing execution time. When combined with independent test design and separate browser sessions, pytest-xdist helps build faster, scalable, and more efficient automation frameworks.


Frequently Asked Questions (FAQs)

What is pytest-xdist?

pytest-xdist is a PyTest plugin that enables parallel execution of test cases using multiple worker processes.


How do I install pytest-xdist?

Install it using:

py -3 -m pip install pytest-xdist

How do I run tests using two workers?

Use:

pytest -s -n 2

What does -n auto do?

It automatically creates worker processes based on the number of available CPU cores.


Can Selenium tests run with pytest-xdist?

Yes.

Each Selenium test should create its own WebDriver instance so that multiple browser sessions can run safely in parallel.


Key Takeaways

  • pytest-xdist enables parallel execution of PyTest test cases.

  • Install it using py -3 -m pip install pytest-xdist.

  • Use -n <count> to specify the number of worker processes.

  • Use -n auto to let PyTest automatically determine the number of workers.

  • Each Selenium test should use its own WebDriver instance.

  • pytest-xdist is widely used to speed up regression testing and CI/CD pipelines.