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 from __future__ import with_statement
37
38 import os
39 import sys
40 import unittest
41
42 import rospkg
43
44 from . import pmon
45 from .baretest import BareTestCase
46 from .baretest import print_runner_summary
47 from . core import create_xml_runner
48 from . core import xml_results_file
49 from .junitxml import Result
50 from .junitxml import print_summary
51
52 _NAME = 'rosunit'
53
54
56 from optparse import OptionParser
57 parser = OptionParser(usage='usage: %prog [options] <file> [test args...]', prog=_NAME)
58 parser.add_option('-t', '--text',
59 action='store_true', dest='text_mode', default=False,
60 help='Run with stdout output instead of XML output')
61 parser.add_option('--time-limit', metavar='TIME_LIMIT',
62 dest='time_limit', default=60,
63 help='Set time limit for test')
64 parser.add_option('--name', metavar='TEST_NAME',
65 dest='test_name', default=None,
66 help='Test name')
67 parser.add_option('--package', metavar='PACKAGE_NAME',
68 dest='pkg', default=None,
69 help='Package name (optional)')
70 (options, args) = parser.parse_args()
71
72 if len(args) < 1:
73 parser.error('You must supply a test file.')
74
75 test_file = args[0]
76
77 if options.test_name:
78 test_name = options.test_name
79 else:
80 test_name = os.path.basename(test_file)
81 if '.' in test_name:
82 test_name = test_name[:test_name.rfind('.')]
83 time_limit = float(options.time_limit) if options.time_limit else None
84
85
86
87 pkg = options.pkg
88 if not pkg:
89 pkg = rospkg.get_package_name(test_file)
90 if not pkg:
91 print("Error: failed to determine package name for file '%s'; maybe you should supply the --package argument to rosunit?" % (test_file))
92 sys.exit(1)
93
94 try:
95 runner_result = None
96 results = Result('rosunit', 0, 0, 0)
97
98 test_case = BareTestCase(test_file, args[1:],
99 retry=0, time_limit=time_limit,
100 test_name=test_name, text_mode=options.text_mode, package_name=pkg)
101 suite = unittest.TestSuite()
102 suite.addTest(test_case)
103
104 if options.text_mode:
105 result = unittest.TextTestRunner(stream=sys.stdout, verbosity=2).run(suite)
106 else:
107 results_file = xml_results_file(pkg, test_name, True)
108
109 xml_runner = create_xml_runner(pkg, test_name,
110 results_file=results_file,
111 is_rostest=True)
112 runner_result = xml_runner.run(suite)
113 finally:
114 pmon.pmon_shutdown()
115
116
117 results = test_case.results
118 if not options.text_mode:
119 print_runner_summary(runner_result, results)
120 else:
121 print('WARNING: overall test result is not accurate when --text is enabled')
122
123 if runner_result is not None and not runner_result.wasSuccessful():
124 sys.exit(1)
125 elif results.num_errors or results.num_failures:
126 sys.exit(2)
127
128
129 if __name__ == '__main__':
130 rosunitmain()
131