8
8
import argparse
9
9
import serial
10
10
import subprocess
11
+ import imp
11
12
from urlparse import urlparse
12
13
from junit_xml import TestSuite , TestCase
13
14
try :
14
15
from cStringIO import StringIO
15
16
except :
16
17
from StringIO import StringIO
18
+ import mock_decorators
17
19
18
20
debug = False
19
21
22
+ sys .path .append (os .path .abspath (__file__ ))
23
+
20
24
def debug_print (* args , ** kwargs ):
21
25
if not debug :
22
26
return
@@ -29,17 +33,18 @@ class BSTestRunner(object):
29
33
TIMEOUT = 2
30
34
CRASH = 3
31
35
32
- def __init__ (self , spawn_obj , name ):
36
+ def __init__ (self , spawn_obj , name , mocks ):
33
37
self .sp = spawn_obj
34
38
self .tests = []
35
39
self .reset_timeout = 2
36
40
self .name = name
41
+ self .mocks = mocks
37
42
38
43
def get_test_list (self ):
39
44
self .sp .sendline ('-1' )
40
45
timeout = 10
41
46
while timeout > 0 :
42
- res = self .sp .expect (['>>>>>bs_test_menu_begin' , EOF ])
47
+ res = self .sp .expect (['>>>>>bs_test_menu_begin' , EOF , TIMEOUT ])
43
48
if res == 0 :
44
49
break
45
50
timeout -= 1
@@ -49,15 +54,15 @@ def get_test_list(self):
49
54
while True :
50
55
res = self .sp .expect (['>>>>>bs_test_item id\=(\d+) name\="([^\" ]*?)" desc="([^"]*?)"' ,
51
56
'>>>>>bs_test_menu_end' ,
52
- EOF ])
57
+ EOF , TIMEOUT ])
53
58
if res == 0 :
54
59
m = self .sp .match
55
60
t = {'id' : m .group (1 ), 'name' : m .group (2 ), 'desc' : m .group (3 )}
56
61
self .tests .append (t )
57
62
debug_print ('added test' , t )
58
63
elif res == 1 :
59
64
break
60
- elif res = = 2 :
65
+ elif res > = 2 :
61
66
time .sleep (0.1 )
62
67
63
68
debug_print ('got {} tests' .format (len (self .tests )))
@@ -75,8 +80,14 @@ def run_tests(self):
75
80
else :
76
81
test_output = StringIO ()
77
82
self .sp .logfile = test_output
83
+ if name in self .mocks :
84
+ print ('setting up mocks' )
85
+ self .mocks [name ]['setup' ]()
78
86
t_start = time .time ()
79
87
result = self .run_test (index )
88
+ if name in self .mocks :
89
+ print ('tearing down mocks' )
90
+ self .mocks [name ]['teardown' ]()
80
91
t_stop = time .time ()
81
92
self .sp .logfile = None
82
93
test_case .elapsed_sec = t_stop - t_start
@@ -96,7 +107,7 @@ def run_test(self, index):
96
107
self .sp .sendline ('{}' .format (index ))
97
108
timeout = 10
98
109
while timeout > 0 :
99
- res = self .sp .expect (['>>>>>bs_test_start' , EOF ])
110
+ res = self .sp .expect (['>>>>>bs_test_start' , EOF , TIMEOUT ])
100
111
if res == 0 :
101
112
break
102
113
time .sleep (0.1 )
@@ -147,8 +158,8 @@ def spawn_port(port_name, baudrate=115200):
147
158
def spawn_exec (name ):
148
159
return pexpect .spawn (name , timeout = 0 )
149
160
150
- def run_tests (spawn , name ):
151
- tw = BSTestRunner (spawn , name )
161
+ def run_tests (spawn , name , mocks ):
162
+ tw = BSTestRunner (spawn , name , mocks )
152
163
tw .get_test_list ()
153
164
return tw .run_tests ()
154
165
@@ -159,6 +170,7 @@ def parse_args():
159
170
parser .add_argument ('-e' , '--executable' , help = 'Talk to the test executable' )
160
171
parser .add_argument ('-n' , '--name' , help = 'Test run name' )
161
172
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' )
162
174
return parser .parse_args ()
163
175
164
176
def main ():
@@ -178,8 +190,12 @@ def main():
178
190
if spawn_func is None :
179
191
debug_print ("Please specify port or executable" , file = sys .stderr )
180
192
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
181
197
with spawn_func (spawn_arg ) as sp :
182
- ts = run_tests (sp , name )
198
+ ts = run_tests (sp , name , mocks )
183
199
if args .output :
184
200
with open (args .output , "w" ) as f :
185
201
TestSuite .to_file (f , [ts ])
0 commit comments