Running your Selenium tests in parallel: Python

Sauce AI for Test Authoring: Move from intent to execution in moment.|xBack to ResourcesBlogPosted September 2, 2009

Running your Selenium exam in latitude: Python

quote

This is the initiative post in our series& quot; Running your Selenium trial in parallel & quot;, in which we & # x27; re going to explain how to set up a concurrent execution surroundings and well reduce your testing times. The inaugural client language we & # x27; re going to direct, as the rubric says, is Python. To depart, let & # x27; s get a set of Selenium Python tests to use: The tests are store in a. This set of tests validates our situation. It checks canonical structure, our login form, our signup form, and our feedback tab (UserVoice powered). The tests are grouped into Python files base on the functionality they direct.

First approach: One by one performance

So, the regular way to run these would be to run each file from the command line, like so:

$ python testIntegrity.py
$ python testLogin.py
$ python testSignup.py
$ python testFeedback.py

As a inaugural approach it & # x27; s not that bad; your tests will run, and you & # x27; ll get decorous output with the results. Execution time:16.2 minutes16.2 minutes isn & # x27; tthatbad. The problem is that once your exam base starts growing, the clip and effort it will take you run these exam will increase considerably. We & # x27; re talking abouthoursin some cases. The future step to take will be to write a pocket-sized script that runs them mechanically, so the only thing the exploiter will get to do is to run a single Python handwriting that takes care of the respite:

$ python ordered_run.py

The code in this handwriting is:

import os
signification glob

examination = glob.glob (& # x27; test * .py & # x27;)
for test in tests:
os.system (& # x27; python % s & # x27; % test)

Easier to run, but not much faster ... Execution time:16 minutes

Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

Second approach: A process per each test set

Let & # x27; s improve this using subprocess, one of Python & # x27; s multiple procedure library.

from subprocess meaning Popen
import clod

tests = glob.glob (& # x27; test * .py & # x27;)
processes = []
for examination in tests:
processes.append (Popen (& # x27; python % s & # x27; % test, shell=True))

for summons in processes:
process.wait ()

Now our tryout run concurrently, apply a separate process per python file (4 operation total). Execution clip:8.3 minutesThis is much faster! It reduces the unhurt performance clip from the clip it lead to run all the tests one after another to the time it takes the longest of the four set of tests to end.

Note: One drawback to this method is that the output we receive isn & # x27; t in any particular order. You can change that by fix the stdout parameter in the Popen instantiation and then concatenating the output in order.

The definitive result: A process per test

We can farther trim the execution clip by running all 14 individual tryout method in parallel. The easiest way we & # x27; ve found to do that in Python is to usenose. First, instal it:

$ easy_install nose==0.11 multiprocessing

Now, place yourself where your tryout are, run nose and enjoy:

$ nosetests -- processes=14

Much easier to run (no helper script for this), light output ... Execution time?3 minutes! Do you have a better way to run your tests concurrently? Tell us about it in the comments.

Published:
Sep 2, 2009
Share this station
Copy Share Link
LinkedIn
© 2026 Sauce Labs Inc., all rights reserved. SAUCE and SAUCE LABS are registered hallmark owned by Sauce Labs Inc. in the United States, EU, and may be registered in other jurisdiction.
robot
quote

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free