Skip to content

Instantly share code, notes, and snippets.

@mrchilds
Created September 18, 2013 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrchilds/6611158 to your computer and use it in GitHub Desktop.
Save mrchilds/6611158 to your computer and use it in GitHub Desktop.
Test Runner
import argparse
import multiprocessing
import os
import sys
import unittest
class RunTests():
"""
Test wrapper to allow for local or remote (via saucelabs) tests to be run in
parallel.
Also provides ability to specify browser and test run name.
"""
def start_tests(self, test_name=False, remote=False, browser=False,
name=False):
self.test_name = test_name
self.browser = browser
if name:
self.name = name
else:
self.name = 'Dev-run'
if remote:
self.run_tests_remotely(remote)
else:
self.run_test()
def run_tests_remotely(self, remote):
"""
Runs tests via Saucelabs
"""
if remote:
os.environ['remote_saucelabs'] = 'Remote'
self.setup_and_run_parallel_tests()
def run_test(self, reference_id=False):
"""
Start tests using nose
"""
test_args = self.create_saucelab_options(\
self.browser_list[reference_id])
if self.test_name:
os.system('nosetests %s -s --tc=%s' % (self.test_name,
test_args))
else:
os.system('nosetests placeholder --tc=' % test_args)
def setup_and_run_parallel_tests(self):
processes = []
self.work_out_required_browsers()
queue = multiprocessing.Queue(len(self.browser_list))
for i in range(len(self.browser_list)):
process = multiprocessing.Process(target=self.run_test,
args=(i-1,))
process.start()
processes += [process]
for process in processes:
process.join()
def create_saucelab_options(self, browser_name):
"""
Returns a string to be passed to nose as commandline arguments
"""
cmd_args = ""
# Add name for SauceLabs identification
cmd_args += "--tc=name:%s " % self.name
# Add browser
cmd_args += "--tc=browser:%s " % browser_name
return cmd_args
def work_out_required_browsers(self):
"""
Work out the require browers for remote tests
This is only required for remote tests as multilpe browsers is not
supported by this script at the moment
"""
if self.browser:
self.browser_list = (self.browser,)
else:
# Current, supported default browsers
self.browser_list = ('CHROME', 'FIREFOX')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("--all", help="Run all tests", action="store_true")
parser.add_argument("--test", help="Run specific test. \
Example Format: maker.change_password_test:MakerChangePassword.\
test_change_password_edit_and_cancel_link")
parser.add_argument("--remote", help="Run tests in SauceLabs",
action="store_true")
parser.add_argument("--browser",
help="Choose a specific browser for remote tests - \
Available Browsers: chrome, firefox",
choices=['CHROME', 'FIREFOX'])
parser.add_argument("--name",
help="Name for SauceLabs - e.g. wchilds-feature-x. \
Spaces are not allowed",
action="store_true")
args = parser.parse_args()
tests = RunTests()
remote_run = args.remote
selected_browser = args.browser
if args.all:
test_name=False
elif args.test:
test_name = args.test
else:
test_name=False
tests.start_tests(test_name=test_name,
remote=remote_run,
browser=selected_browser)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment