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