blob: 754636f0faa5f385bb19cea09fa05cab1c2d3b8c [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001#!/usr/bin/python
2
3# Copyright
4
5# DESCRIPTION
6# This is script for running all selected toaster cases on
7# selected web browsers manifested in toaster_test.cfg.
8
9# 1. How to start toaster in yocto:
10# $ source poky/oe-init-build-env
11# $ source toaster start
12# $ bitbake core-image-minimal
13
14# 2. How to install selenium on Ubuntu:
15# $ sudo apt-get install scrot python-pip
16# $ sudo pip install selenium
17
18# 3. How to install selenium addon in firefox:
19# Download the lastest firefox addon from http://release.seleniumhq.org/selenium-ide/
20# Then install it. You can also install firebug and firepath addon
21
22# 4. How to start writing a new case:
23# All you need to do is to implement the function test_xxx() and pile it on.
24
25# 5. How to test with Chrome browser
26# Download/install chrome on host
27# Download chromedriver from https://code.google.com/p/chromedriver/downloads/list according to your host type
28# put chromedriver in PATH, (e.g. /usr/bin/, bear in mind to chmod)
29# For windows host, you may put chromedriver.exe in the same directory as chrome.exe
30
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050031import unittest, sys, os, platform
Patrick Williamsc124f4f2015-09-15 14:41:29 -050032import ConfigParser
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050033import argparse
34from toaster_automation_test import toaster_cases
Patrick Williamsc124f4f2015-09-15 14:41:29 -050035
36
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050037def get_args_parser():
38 description = "Script that runs toaster auto tests."
39 parser = argparse.ArgumentParser(description=description)
40 parser.add_argument('--run-all-tests', required=False, action="store_true", dest="run_all_tests", default=False,
41 help='Run all tests.')
42 parser.add_argument('--run-suite', required=False, dest='run_suite', default=False,
43 help='run suite (defined in cfg file)')
Patrick Williamsc124f4f2015-09-15 14:41:29 -050044
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050045 return parser
Patrick Williamsc124f4f2015-09-15 14:41:29 -050046
47
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050048def get_tests():
49 testslist = []
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050051 prefix = 'toaster_automation_test.toaster_cases'
52
53 for t in dir(toaster_cases):
54 if t.startswith('test_'):
55 testslist.append('.'.join((prefix, t)))
56
57 return testslist
58
59
60def get_tests_from_cfg(suite=None):
61
62 testslist = []
63 config = ConfigParser.SafeConfigParser()
64 config.read('toaster_test.cfg')
65
66 if suite is not None:
67 target_suite = suite.lower()
68
69 # TODO: if suite is valid suite
70
71 else:
72 target_suite = platform.system().lower()
73
74 try:
75 tests_from_cfg = eval(config.get('toaster_test_' + target_suite, 'test_cases'))
76 except:
77 print 'Failed to get test cases from cfg file. Make sure the format is correct.'
78 return None
79
80 prefix = 'toaster_automation_test.toaster_cases.test_'
81 for t in tests_from_cfg:
82 testslist.append(prefix + str(t))
83
84 return testslist
85
86def main():
87
88 # In case this script is called from other directory
89 os.chdir(os.path.abspath(sys.path[0]))
90
91 parser = get_args_parser()
92 args = parser.parse_args()
93
94 if args.run_all_tests:
95 testslist = get_tests()
96 elif args.run_suite:
97 testslist = get_tests_from_cfg(args.run_suite)
98 os.environ['TOASTER_SUITE'] = args.run_suite
99 else:
100 testslist = get_tests_from_cfg()
101
102 if not testslist:
103 print 'Failed to get test cases.'
104 exit(1)
105
106 suite = unittest.TestSuite()
107 loader = unittest.TestLoader()
108 loader.sortTestMethodsUsing = None
109 runner = unittest.TextTestRunner(verbosity=2, resultclass=buildResultClass(args))
110
111 for test in testslist:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500112 try:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500113 suite.addTests(loader.loadTestsFromName(test))
114 except:
115 return 1
116
117 result = runner.run(suite)
118
119 if result.wasSuccessful():
120 return 0
121 else:
122 return 1
123
124
125def buildResultClass(args):
126 """Build a Result Class to use in the testcase execution"""
127
128 class StampedResult(unittest.TextTestResult):
129 """
130 Custom TestResult that prints the time when a test starts. As toaster-auto
131 can take a long time (ie a few hours) to run, timestamps help us understand
132 what tests are taking a long time to execute.
133 """
134 def startTest(self, test):
135 import time
136 self.stream.write(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + " - ")
137 super(StampedResult, self).startTest(test)
138
139 return StampedResult
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500140
141
142if __name__ == "__main__":
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500143
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500144 try:
145 ret = main()
146 except:
147 ret = 1
148 import traceback
149 traceback.print_exc()
150 finally:
151 if os.getenv('TOASTER_SUITE'):
152 del os.environ['TOASTER_SUITE']
153 sys.exit(ret)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500154
155