logo logo

Parallel Test Execution with Pytest

Parallel test

This tutorial will make web UI testing easy. We will build a simple yet robust web UI test solution using Python, pytest, and Selenium WebDriver. We will learn strategies for good test design as well as patterns for good automation code. By the end of the tutorial, you’ll know to improve your web UI with python with a parallel test and be a web test automation champ! Your Python test project can be the foundation for your own test cases, too.

📍 If you are looking for a single Python Package for Android, iOS and Web Testing – there is also an easy open source solution provided by TestProject. With a single executable, zero configurations, and familiar Selenium APIs, you can develop and execute robust Python tests and get automatic HTML test reports as a bonus! All you need is: pip install testproject-python-sdk. Simply follow this Github link to learn more about it, or read through this great tutorial to get started. 

Tutorial Chapters

  1. Web UI Testing Made Easy with Python, Pytest and Selenium WebDriver (Overview)
  2. Set Your Test Automation Goals (Chapter 1)
  3. Create A Python Test Automation Project Using Pytest (Chapter 2)
  4. Installing Selenium WebDriver Using Python and Chrome (Chapter 3)
  5. Write Your First Web Test Using Selenium WebDriver, Python and Chrome (Chapter 4)
  6. Develop Page Object Selenium Tests Using Python (Chapter 5)
  7. How to Read Config Files in Python Selenium Tests (Chapter 6)
  8. Take Your Python Test Automation To The Next Level (Chapter 7)

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.

Here’s how to run the example project with 4 test threads:

$ pipenv run python -m pytest -n 4
============================= test session starts ==============================
platform darwin -- Python 3.7.3, pytest-4.6.2, py-1.8.0, pluggy-0.12.0
rootdir: /Users/andylpk247/Programming/automation-panda/python-webui-testing
plugins: forked-1.0.2, html-1.20.0, xdist-1.28.0, metadata-1.8.0
gw0 [9] / gw1 [9] / gw2 [9] / gw3 [9]
.........                                                                [100%]
=========================== 9 passed in 6.33 seconds ===========================


Go ahead and check out the next tip for taking your web UI testing with Python to the next level.

About the author


Andy Knight is the “Automation Panda” – an engineer, consultant, and international speaker who loves all things software. He specializes in building robust test automation systems from the ground up. Read his tech blog at AutomationPanda.com, and follow him on Twitter at @AutomationPanda.

Leave a Reply