Skip to content

Instantly share code, notes, and snippets.

@mbarde
Created October 3, 2019 12:56
Example Python script to concurrently run multiple parametrized Robot Framework tests
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Example Python script to concurrently run multiple parametrized Robot Framework tests
Expects following input files:
- suite.robot: Robot Framework test case file
- user_list.txt: List of usernames (one per line)
'''
import os
import random
import subprocess
import time
import webbrowser
# config
baseFolder = 'data_tests'
suiteFile = 'suite.robot'
usernamesFile = 'user_list.txt'
testUsersPassword = 'secret'
# clean up base folder
os.system('rm -rf ' + baseFolder)
os.mkdir(baseFolder)
# no output to console please
FNULL = open(os.devnull, 'w')
# load list of usernames
f = open(usernamesFile, 'r')
lines = f.readlines()
f.close()
# spawn subprocess for each user
userPathes = []
processes = set()
for line in lines:
user = line.strip()
userPath = baseFolder + '/' + user
userPathes.append((user, userPath))
os.mkdir(userPath)
cmdParts = [
'robot',
# abitrary amount of variables can be added here
'--variable', 'USER:' + user,
'--variable', 'PASSWORD:' + testUsersPassword,
# use -t argument to execute only the test named 'Login Test' from the suite
'-t', 'Login Test',
'../../' + suiteFile,
]
processes.add(subprocess.Popen(cmdParts, cwd=userPath, stdout=FNULL))
print('Spawned ' + user)
# random timeout to simulate more realistic user behavior
time.sleep(random.randint(0, 4))
# wait until all subprocesses finished
for proc in processes:
proc.wait()
# collect output and open log files of failed tests
for username, path in userPathes:
f = open(path + '/output.xml')
content = f.read()
f.close()
if 'status="FAIL"' in content:
print(username + '\t'*5 + '[\033[91mFailed\033[0m]')
webbrowser.open(path + '/report.html')
else:
print(username + '\t'*5 + '[\033[92mOK\033[0m]')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment