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 import os
36 import sys
37 import logging
38 import roslib.rosenv
39
40 from .xmlrunner import XMLTestRunner
41
42 XML_OUTPUT_FLAG = '--gtest_output=xml:'
43
44 _logger = logging.getLogger('runner')
46 if args:
47 msg = msg%args
48 _logger.info(msg)
49 print "[ROSUNIT]"+msg
50
52 if args:
53 msg = msg%args
54 _logger.info(msg)
55 print '\033[1m[ROSUNIT]%s\033[0m'%msg
56
58 if args:
59 msg = msg%args
60 _logger.error(msg)
61 print >> sys.stderr, "[ROSUNIT]"+msg
62
64 """
65 @param test_pkg: name of test's package
66 @type test_pkg: str
67 @param test_name str: name of test
68 @type test_name: str
69 @param is_rostest: True if the results file is for a rostest-generated unit instance
70 @type is_rostest: bool
71 @return: name of xml results file for specified test
72 @rtype: str
73 """
74 test_dir = os.path.join(roslib.rosenv.get_test_results_dir(), test_pkg)
75 if not os.path.exists(test_dir):
76 try:
77 roslib.rosenv.makedirs_with_parent_perms(test_dir)
78 except OSError:
79 raise IOError("cannot create test results directory [%s]. Please check permissions."%(test_dir))
80
81
82
83 for c in ' "\'&$!`/\\':
84 if c in test_name:
85 test_name = test_name.replace(c, '_')
86 if is_rostest:
87 return os.path.join(test_dir, 'TEST-rostest__%s.xml'%test_name)
88 else:
89 return os.path.join(test_dir, 'TEST-%s.xml'%test_name)
90
92 """
93 Derive name of rostest based on file name/path. rostest follows a
94 certain convention defined above.
95
96 @return: name of test
97 @rtype: str
98 """
99 test_file_abs = os.path.abspath(test_file)
100 if test_file_abs.startswith(pkg_dir):
101
102 test_file = test_file_abs[len(pkg_dir):]
103 if test_file[0] == os.sep:
104 test_file = test_file[1:]
105 outname = test_file.replace(os.sep, '_')
106 if '.' in outname:
107 outname = outname[:outname.rfind('.')]
108 return outname
109
111 """
112 Create the unittest test runner with XML output
113 @param test_pkg: package name
114 @type test_pkg: str
115 @param test_name: test name
116 @type test_name: str
117 @param is_rostest: if True, use naming scheme for rostest itself instead of individual unit test naming
118 @type is_rostest: bool
119 """
120 test_name = os.path.basename(test_name)
121
122 if not results_file:
123 results_file = xml_results_file(test_pkg, test_name, is_rostest)
124 test_dir = os.path.abspath(os.path.dirname(results_file))
125 if not os.path.exists(test_dir):
126 try:
127 roslib.rosenv.makedirs_with_parent_perms(test_dir)
128 except OSError:
129 raise IOError("cannot create test results directory [%s]. Please check permissions."%(test_dir))
130
131 elif os.path.isfile(test_dir):
132 raise Exception("ERROR: cannot run test suite, file is preventing creation of test dir: %s"%test_dir)
133
134 print "[ROSUNIT] Outputting test results to %s"%results_file
135 outstream = open(results_file, 'w')
136 return XMLTestRunner(stream=outstream)
137