88import argparse
99import serial
1010import subprocess
11+ import imp
1112from urlparse import urlparse
1213from junit_xml import TestSuite , TestCase
1314try :
1415 from cStringIO import StringIO
1516except :
1617 from StringIO import StringIO
18+ import mock_decorators
1719
1820debug = False
1921
22+ sys .path .append (os .path .abspath (__file__ ))
23+
2024def debug_print (* args , ** kwargs ):
2125 if not debug :
2226 return
@@ -29,17 +33,18 @@ class BSTestRunner(object):
2933 TIMEOUT = 2
3034 CRASH = 3
3135
32- def __init__ (self , spawn_obj , name ):
36+ def __init__ (self , spawn_obj , name , mocks ):
3337 self .sp = spawn_obj
3438 self .tests = []
3539 self .reset_timeout = 2
3640 self .name = name
41+ self .mocks = mocks
3742
3843 def get_test_list (self ):
3944 self .sp .sendline ('-1' )
4045 timeout = 10
4146 while timeout > 0 :
42- res = self .sp .expect (['>>>>>bs_test_menu_begin' , EOF ])
47+ res = self .sp .expect (['>>>>>bs_test_menu_begin' , EOF , TIMEOUT ])
4348 if res == 0 :
4449 break
4550 timeout -= 1
@@ -49,15 +54,15 @@ def get_test_list(self):
4954 while True :
5055 res = self .sp .expect (['>>>>>bs_test_item id\=(\d+) name\="([^\" ]*?)" desc="([^"]*?)"' ,
5156 '>>>>>bs_test_menu_end' ,
52- EOF ])
57+ EOF , TIMEOUT ])
5358 if res == 0 :
5459 m = self .sp .match
5560 t = {'id' : m .group (1 ), 'name' : m .group (2 ), 'desc' : m .group (3 )}
5661 self .tests .append (t )
5762 debug_print ('added test' , t )
5863 elif res == 1 :
5964 break
60- elif res = = 2 :
65+ elif res > = 2 :
6166 time .sleep (0.1 )
6267
6368 debug_print ('got {} tests' .format (len (self .tests )))
@@ -75,8 +80,14 @@ def run_tests(self):
7580 else :
7681 test_output = StringIO ()
7782 self .sp .logfile = test_output
83+ if name in self .mocks :
84+ print ('setting up mocks' )
85+ self .mocks [name ]['setup' ]()
7886 t_start = time .time ()
7987 result = self .run_test (index )
88+ if name in self .mocks :
89+ print ('tearing down mocks' )
90+ self .mocks [name ]['teardown' ]()
8091 t_stop = time .time ()
8192 self .sp .logfile = None
8293 test_case .elapsed_sec = t_stop - t_start
@@ -96,7 +107,7 @@ def run_test(self, index):
96107 self .sp .sendline ('{}' .format (index ))
97108 timeout = 10
98109 while timeout > 0 :
99- res = self .sp .expect (['>>>>>bs_test_start' , EOF ])
110+ res = self .sp .expect (['>>>>>bs_test_start' , EOF , TIMEOUT ])
100111 if res == 0 :
101112 break
102113 time .sleep (0.1 )
@@ -147,8 +158,8 @@ def spawn_port(port_name, baudrate=115200):
147158def spawn_exec (name ):
148159 return pexpect .spawn (name , timeout = 0 )
149160
150- def run_tests (spawn , name ):
151- tw = BSTestRunner (spawn , name )
161+ def run_tests (spawn , name , mocks ):
162+ tw = BSTestRunner (spawn , name , mocks )
152163 tw .get_test_list ()
153164 return tw .run_tests ()
154165
@@ -159,6 +170,7 @@ def parse_args():
159170 parser .add_argument ('-e' , '--executable' , help = 'Talk to the test executable' )
160171 parser .add_argument ('-n' , '--name' , help = 'Test run name' )
161172 parser .add_argument ('-o' , '--output' , help = 'Output JUnit format test report' )
173+ parser .add_argument ('-m' , '--mock' , help = 'Set python script to use for mocking purposes' )
162174 return parser .parse_args ()
163175
164176def main ():
@@ -178,8 +190,12 @@ def main():
178190 if spawn_func is None :
179191 debug_print ("Please specify port or executable" , file = sys .stderr )
180192 return 1
193+ mocks = {}
194+ if args .mock is not None :
195+ mocks_mod = imp .load_source ('mocks' , args .mock )
196+ mocks = mock_decorators .env
181197 with spawn_func (spawn_arg ) as sp :
182- ts = run_tests (sp , name )
198+ ts = run_tests (sp , name , mocks )
183199 if args .output :
184200 with open (args .output , "w" ) as f :
185201 TestSuite .to_file (f , [ts ])
0 commit comments