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