-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtestrun.py
More file actions
157 lines (128 loc) · 4.23 KB
/
testrun.py
File metadata and controls
157 lines (128 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""
A test runner for a NAOqi service (to test the service packaged in the app)
Edit it to set your robot's IP to run it (or use arv etc.)
So far it's pretty hard-coded
"""
__version__ = "0.0.1"
__copyright__ = "Copyright 2015, Aldebaran Robotics"
__author__ = 'ekroeger'
__email__ = 'ekroeger@aldebaran.com'
import subprocess
import time
import sys
import traceback
import qi
#########################################
# Helper base class
#########################################
class ServiceTester:
_app = None
_testresults = []
@classmethod
def configure(cls, robot):
sys.argv.extend(["--qi-url", robot])
cls.robot = robot
cls._app = qi.Application()
cls._app.start()
@classmethod
def recap(cls):
succeeded = 0
failed = []
for name, result in cls._testresults:
if result:
succeeded += 1
else:
failed.append(name)
print "================================================"
print "Successes: {0}/{1}".format(succeeded, len(cls._testresults))
if failed:
print "Failed tests:", ", ".join(failed)
else:
print "All OK :)"
def service(self, service_name):
return self._app.session.service(service_name)
def __init__(self):
self.name = self.__class__.__name__
self.start()
def start(self):
command = ["/usr/bin/python", self.script, "--qi-url", self.robot]
self.popen = subprocess.Popen(command, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
self.running = True
#print "Spawned", self.script, "with ID", self.popen.pid
#print
def finish_and_dump(self, verbose=True):
if self.running:
self.running = False
self.popen.terminate()
if verbose:
print
print "Killed. Output:"
lines_iterator = iter(self.popen.stdout.readline, b"")
for line in lines_iterator:
print ">", line.strip("\n") # yield line
print
def run_wrapped(self):
succeeded = False
try:
print
print "--------------------"
print "starting", self.name
succeeded = self.run()
print "finished", self.name
except Exception as e:
print "error in", self.name
print traceback.format_exc()
finally:
self.finish_and_dump(verbose=(not succeeded))
self._testresults.append((self.name, succeeded))
#########################################
# Specific test classes
#########################################
class SetGetTest(ServiceTester):
script = "app/scripts/pepperbeats.py"
def run(self):
time.sleep(1)
ALPepperBeats = self.service("ALPepperBeats")
ALPepperBeats.set(0)
assert ALPepperBeats.get() == 0
ALPepperBeats.set(2)
assert ALPepperBeats.get() == 2
return True
class ResetTest(ServiceTester):
script = "app/scripts/pepperbeats.py"
def run(self):
time.sleep(1)
ALPepperBeats = self.service("ALPepperBeats")
ALPepperBeats.reset()
assert ALPepperBeats.get() == 0
return True
class ExitTest(ServiceTester):
script = "app/scripts/pepperbeats.py"
def run(self):
time.sleep(1)
ALPepperBeats = self.service("ALPepperBeats")
goterror = False
try:
ALPepperBeats.exit()
ALPepperBeats.set(1)
except RuntimeError:
print "Got error after exit, as expected"
goterror = True
assert goterror, "Expected an exception after exit!"
return True
def run_tests(robotname, *testclasses):
ServiceTester.configure(robotname)
for cls in testclasses:
cls().run_wrapped()
ServiceTester.recap()
def test_all(robotname):
run_tests(robotname,
SetGetTest,
ResetTest,
ExitTest,
)
if __name__ == "__main__":
ROBOTNAME = "yourrobot.local" # Adapt this depending of your robot
ROBOTNAME = "citadelle.local"
test_all(ROBOTNAME)