main.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # License: BSD
4 # https://raw.github.com/robotics-in-concert/rocon_multimaster/license/LICENSE
5 #
6 
7 ##############################################################################
8 # Imports
9 ##############################################################################
10 
11 from __future__ import print_function
12 
13 import os
14 import sys
15 import argparse
16 from argparse import RawTextHelpFormatter
17 import unittest
18 
19 import rostest.runner
20 import rospkg
21 from rospkg.environment import ROS_TEST_RESULTS_DIR
22 from rostest.rostestutil import printRostestSummary, xmlResultsFile
23 import rosunit
24 from roslaunch.pmon import pmon_shutdown
25 import rocon_python_utils
26 import rocon_launch
27 
28 from . import loggers
29 from . import runner
30 
31 ##############################################################################
32 # Methods
33 ##############################################################################
34 
35 
37  overview = 'Launches a rocon multi-master test.\n\n'
38  return overview
39 
40 
42  '''
43  @raise IOError : if no package name is given and the rocon launcher cannot be found on the filesystem.
44  '''
45  parser = argparse.ArgumentParser(description=help_string(), formatter_class=RawTextHelpFormatter)
46  parser.add_argument('package', nargs='?', default=None, help='name of the package in which to find the test configuration')
47  parser.add_argument('test', nargs=1, help='name of the test configuration (xml) file')
48  # not yet supported
49  # parser.add_argument('-l', '--launch', action='store_true', help='launch each component with rocon_launch [false]')
50  parser.add_argument('-p', '--pause', action='store_true', help='pause before tearing down so you can introspect easily [false]')
51  parser.add_argument('-n', '--no-screen', action='store_true', help='do run each roslaunch with the --screen option [true]')
52  parser.add_argument('-t', '--text-mode', action='store_true', help='log the rostest output to screen rather than log file.')
53  parser.add_argument("--results-filename", action='store', type=str, default=None, help="results_filename")
54  parser.add_argument('--results-base-dir', action='store', default='', help="The base directory of the test results. The test result file is created in a subfolder name PKG_DIR.")
55  args = parser.parse_args()
56  # Stop it from being a list (happens when nargs is an integer)
57  args.test = args.test[0]
58  launch_arguments = ""
59  if not args.no_screen:
60  launch_arguments = "--screen"
61  if not args.package:
62  if not os.path.isfile(args.test):
63  raise IOError("Test launcher file does not exist [%s]." % args.test)
64  else:
65  args.package = rospkg.get_package_name(args.test)
66  return (args.package, args.test, launch_arguments, args.pause, args.text_mode,
67  args.results_filename, args.results_base_dir)
68 
69 
70 def test_main():
71  (package, name, launch_arguments, pause, text_mode, results_filename, results_base_dir) = _parse_arguments()
72 
73  if os.path.isabs(name):
74  if os.path.exists(name):
75  rocon_launcher = name
76  else:
77  raise IOError("cannot locate [%s]" % name)
78  else:
79  rocon_launcher = rocon_python_utils.ros.find_resource(package, name) # raises an IO error if there is a problem.
80 
81  env = None
82  if results_base_dir:
83  env = {ROS_TEST_RESULTS_DIR: results_base_dir}
84 
85  if results_filename:
86  results_log_name = results_filename
87  if '.' in results_log_name:
88  results_log_name = results_log_name[:results_log_name.rfind('.')]
89  results_log_file = xmlResultsFile(package, results_log_name, is_rostest=True, env=env)
90  results_log_file = results_log_file.replace("rostest", "rocon_test")
91  else:
92  results_log_name, results_log_file = loggers.configure_logging(package, rocon_launcher)
93 
94  # launchers is of type rocon_launch.RosLaunchConfiguration[]
95  launchers = rocon_launch.parse_rocon_launcher(rocon_launcher, launch_arguments, args_mappings={})
96 
97  try:
98  test_case = runner.create_unit_rocon_test(rocon_launcher, launchers)
99  suite = unittest.TestLoader().loadTestsFromTestCase(test_case)
100  if pause:
101  runner.set_pause_mode(True)
102  if text_mode:
103  runner.set_text_mode(True)
104  result = unittest.TextTestRunner(verbosity=2).run(suite)
105  else:
106  xml_runner = rosunit.create_xml_runner(package,
107  results_log_name,
108  results_file=results_log_file,
109  is_rostest=True
110  )
111  result = xml_runner.run(suite)
112  finally:
113  # really make sure that all of our processes have been killed (should be automatic though)
114  test_parents = runner.get_rocon_test_parents()
115  for r in test_parents:
116  r.tearDown()
117  del test_parents[:]
118  pmon_shutdown()
119  subtest_results = runner.get_results()
120 
121  ################################
122  # Post stuff
123  ################################
124  config = rostest.runner.getConfig()
125  if config:
126  if config.config_errors:
127  print("\n[ROCON_TEST WARNINGS]" + '-' * 62 + '\n', file=sys.stderr)
128  for err in config.config_errors:
129  print(" * %s" % err, file=sys.stderr)
130  print('')
131 
132  if not text_mode:
133  printRostestSummary(result, subtest_results)
134  loggers.printlog("results log file is in %s" % results_log_file)
135 
136  # This is not really a useful log, so dont worry about showing it.
137  # if log_name:
138  # loggers.printlog("rocon_test log file is in %s" % log_name)
139 
140  if not result.wasSuccessful():
141  sys.exit(1)
142  elif subtest_results.num_errors or subtest_results.num_failures:
143  sys.exit(2)
def help_string()
Methods.
Definition: main.py:36
def _parse_arguments()
Definition: main.py:41
def test_main()
Definition: main.py:70


rocon_test
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 14:40:14