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 from __future__ import print_function
36
37
38
39
40 import os
41 import sys
42 import time
43 import unittest
44 import logging
45
46 import roslaunch
47 import rospkg
48 import rosgraph.roslogging
49
50 from rostest.rostestutil import createXMLRunner, printRostestSummary, \
51 xmlResultsFile, rostest_name_from_path
52 from rostest.rostest_parent import ROSTestLaunchParent
53
54 import rostest.runner
55
56 _NAME = 'rostest'
57
65
67
68 results_file_dir = os.path.dirname(results_file)
69 if not os.path.isdir(results_file_dir):
70 os.makedirs(results_file_dir)
71 with open(results_file, 'w') as f:
72 d = {'test': outname, 'test_file': test_file }
73 f.write("""<?xml version="1.0" encoding="UTF-8"?>
74 <testsuite tests="1" failures="1" time="1" errors="0" name="%(test)s">
75 <testcase name="test_ran" status="run" time="1" classname="Results">
76 <failure message="rostest file [%(test_file)s] does not exist" type=""/>
77 </testcase>
78 </testsuite>"""%d)
79
81 import roslaunch.rlutil
82
83 from optparse import OptionParser
84 parser = OptionParser(usage="usage: %prog [options] [package] <filename>", prog=_NAME)
85 parser.add_option("-t", "--text",
86 action="store_true", dest="text_mode", default=False,
87 help="Run with stdout output instead of XML output")
88 parser.add_option("--pkgdir", metavar="PKG_DIR",
89 dest="pkg_dir", default=None,
90 help="package dir")
91 parser.add_option("--package", metavar="PACKAGE",
92 dest="package", default=None,
93 help="package")
94 (options, args) = parser.parse_args()
95 try:
96 args = roslaunch.rlutil.resolve_launch_arguments(args)
97 except roslaunch.core.RLException as e:
98 print(str(e), file=sys.stderr)
99 sys.exit(1)
100
101
102 logfile_name = configure_logging()
103 logger = logging.getLogger('rostest')
104 import roslaunch.core
105 roslaunch.core.add_printlog_handler(logger.info)
106 roslaunch.core.add_printerrlog_handler(logger.error)
107
108 logger.info('rostest starting with options %s, args %s'%(options, args))
109 if len(args) == 0:
110 parser.error("You must supply a test file argument to rostest.")
111 if len(args) != 1:
112 parser.error("rostest only accepts a single test file")
113
114
115 test_file = args[0]
116 if options.pkg_dir and options.package:
117
118
119 pkg_dir, pkg = options.pkg_dir, options.package
120 else:
121 pkg = rospkg.get_package_name(test_file)
122 r = rospkg.RosPack()
123 pkg_dir = r.get_path(pkg)
124
125 outname = rostest_name_from_path(pkg_dir, test_file)
126
127
128 if not os.path.isfile(test_file):
129 results_file = xmlResultsFile(pkg, outname, True)
130 write_bad_filename_failure(test_file, results_file, outname)
131 parser.error("test file is invalid. Generated failure case result file in %s"%results_file)
132
133 try:
134 testCase = rostest.runner.createUnitTest(pkg, test_file)
135 suite = unittest.TestLoader().loadTestsFromTestCase(testCase)
136
137 if options.text_mode:
138 rostest.runner.setTextMode(True)
139 result = unittest.TextTestRunner(verbosity=2).run(suite)
140 else:
141 is_rostest = True
142 results_file = xmlResultsFile(pkg, outname, is_rostest)
143 xml_runner = createXMLRunner(pkg, outname, \
144 results_file=results_file, \
145 is_rostest=is_rostest)
146 result = xml_runner.run(suite)
147 finally:
148
149 test_parents = rostest.runner.getRostestParents()
150 for r in test_parents:
151 logger.info("finally rostest parent tearDown [%s]", r)
152 r.tearDown()
153 del test_parents[:]
154 from roslaunch.pmon import pmon_shutdown
155 logger.info("calling pmon_shutdown")
156 pmon_shutdown()
157 logger.info("... done calling pmon_shutdown")
158
159
160 config = rostest.runner.getConfig()
161 if config:
162 if config.config_errors:
163 print("\n[ROSTEST WARNINGS]"+'-'*62+'\n', file=sys.stderr)
164 for err in config.config_errors:
165 print(" * %s"%err, file=sys.stderr)
166 print('')
167
168
169 subtest_results = rostest.runner.getResults()
170 if not options.text_mode:
171 printRostestSummary(result, subtest_results)
172 else:
173 print("WARNING: overall test result is not accurate when --text is enabled")
174
175 if logfile_name:
176 print("rostest log file is in %s"%logfile_name)
177
178 if not result.wasSuccessful():
179 sys.exit(1)
180 elif subtest_results.num_errors or subtest_results.num_failures:
181 sys.exit(2)
182
183 if __name__ == '__main__':
184 rostestmain()
185