source: rtos_arduino/trunk/arduino_lib/libraries/pubsubclient-2.6/tests/testsuite.py@ 209

Last change on this file since 209 was 209, checked in by ertl-honda, 8 years ago

BlueMix用のフィアルを追加

File size: 5.1 KB
Line 
1#!/usr/bin/env python
2import os
3import os.path
4import sys
5import shutil
6from subprocess import call
7import importlib
8import unittest
9import re
10
11from testcases import settings
12
13class Workspace(object):
14
15 def __init__(self):
16 self.root_dir = os.getcwd()
17 self.build_dir = os.path.join(self.root_dir,"tmpbin");
18 self.log_dir = os.path.join(self.root_dir,"logs");
19 self.tests_dir = os.path.join(self.root_dir,"testcases");
20 self.examples_dir = os.path.join(self.root_dir,"../PubSubClient/examples")
21 self.examples = []
22 self.tests = []
23 if not os.path.isdir("../PubSubClient"):
24 raise Exception("Cannot find PubSubClient library")
25 try:
26 import ino
27 except:
28 raise Exception("ino tool not installed")
29
30 def init(self):
31 if os.path.isdir(self.build_dir):
32 shutil.rmtree(self.build_dir)
33 os.mkdir(self.build_dir)
34 if os.path.isdir(self.log_dir):
35 shutil.rmtree(self.log_dir)
36 os.mkdir(self.log_dir)
37
38 os.chdir(self.build_dir)
39 call(["ino","init"])
40
41 shutil.copytree("../../PubSubClient","lib/PubSubClient")
42
43 filenames = []
44 for root, dirs, files in os.walk(self.examples_dir):
45 filenames += [os.path.join(root,f) for f in files if f.endswith(".ino")]
46 filenames.sort()
47 for e in filenames:
48 self.examples.append(Sketch(self,e))
49
50 filenames = []
51 for root, dirs, files in os.walk(self.tests_dir):
52 filenames += [os.path.join(root,f) for f in files if f.endswith(".ino")]
53 filenames.sort()
54 for e in filenames:
55 self.tests.append(Sketch(self,e))
56
57 def clean(self):
58 shutil.rmtree(self.build_dir)
59
60class Sketch(object):
61 def __init__(self,wksp,fn):
62 self.w = wksp
63 self.filename = fn
64 self.basename = os.path.basename(self.filename)
65 self.build_log = os.path.join(self.w.log_dir,"%s.log"%(os.path.basename(self.filename),))
66 self.build_err_log = os.path.join(self.w.log_dir,"%s.err.log"%(os.path.basename(self.filename),))
67 self.build_upload_log = os.path.join(self.w.log_dir,"%s.upload.log"%(os.path.basename(self.filename),))
68
69 def build(self):
70 sys.stdout.write(" Build: ")
71 sys.stdout.flush()
72
73 # Copy sketch over, replacing IP addresses as necessary
74 fin = open(self.filename,"r")
75 lines = fin.readlines()
76 fin.close()
77 fout = open(os.path.join(self.w.build_dir,"src","sketch.ino"),"w")
78 for l in lines:
79 if re.match(r"^byte server\[\] = {",l):
80 fout.write("byte server[] = { %s };\n"%(settings.server_ip.replace(".",", "),))
81 elif re.match(r"^byte ip\[\] = {",l):
82 fout.write("byte ip[] = { %s };\n"%(settings.arduino_ip.replace(".",", "),))
83 else:
84 fout.write(l)
85 fout.flush()
86 fout.close()
87
88 # Run build
89 fout = open(self.build_log, "w")
90 ferr = open(self.build_err_log, "w")
91 rc = call(["ino","build"],stdout=fout,stderr=ferr)
92 fout.close()
93 ferr.close()
94 if rc == 0:
95 sys.stdout.write("pass")
96 sys.stdout.write("\n")
97 return True
98 else:
99 sys.stdout.write("fail")
100 sys.stdout.write("\n")
101 with open(self.build_err_log) as f:
102 for line in f:
103 print " ",line,
104 return False
105
106 def upload(self):
107 sys.stdout.write(" Upload: ")
108 sys.stdout.flush()
109 fout = open(self.build_upload_log, "w")
110 rc = call(["ino","upload"],stdout=fout,stderr=fout)
111 fout.close()
112 if rc == 0:
113 sys.stdout.write("pass")
114 sys.stdout.write("\n")
115 return True
116 else:
117 sys.stdout.write("fail")
118 sys.stdout.write("\n")
119 with open(self.build_upload_log) as f:
120 for line in f:
121 print " ",line,
122 return False
123
124
125 def test(self):
126 # import the matching test case, if it exists
127 try:
128 basename = os.path.basename(self.filename)[:-4]
129 i = importlib.import_module("testcases."+basename)
130 except:
131 sys.stdout.write(" Test: no tests found")
132 sys.stdout.write("\n")
133 return
134 c = getattr(i,basename)
135
136 testmethods = [m for m in dir(c) if m.startswith("test_")]
137 testmethods.sort()
138 tests = []
139 for m in testmethods:
140 tests.append(c(m))
141
142 result = unittest.TestResult()
143 c.setUpClass()
144 if self.upload():
145 sys.stdout.write(" Test: ")
146 sys.stdout.flush()
147 for t in tests:
148 t.run(result)
149 print "%d/%d"%(result.testsRun-len(result.failures)-len(result.errors),result.testsRun)
150 if not result.wasSuccessful():
151 if len(result.failures) > 0:
152 for f in result.failures:
153 print "-- %s"%(str(f[0]),)
154 print f[1]
155 if len(result.errors) > 0:
156 print " Errors:"
157 for f in result.errors:
158 print "-- %s"%(str(f[0]),)
159 print f[1]
160 c.tearDownClass()
161
162if __name__ == '__main__':
163 run_tests = True
164
165 w = Workspace()
166 w.init()
167
168 for e in w.examples:
169 print "--------------------------------------"
170 print "[%s]"%(e.basename,)
171 if e.build() and run_tests:
172 e.test()
173 for e in w.tests:
174 print "--------------------------------------"
175 print "[%s]"%(e.basename,)
176 if e.build() and run_tests:
177 e.test()
178
179 w.clean()
Note: See TracBrowser for help on using the repository browser.