main.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # License: BSD
00004 #   https://raw.github.com/robotics-in-concert/rocon_multimaster/license/LICENSE
00005 #
00006 
00007 ##############################################################################
00008 # Imports
00009 ##############################################################################
00010 
00011 from __future__ import print_function
00012 
00013 import os
00014 import sys
00015 import argparse
00016 from argparse import RawTextHelpFormatter
00017 import unittest
00018 
00019 import rostest.runner
00020 import rospkg
00021 from rostest.rostestutil import printRostestSummary
00022 import rosunit
00023 from roslaunch.pmon import pmon_shutdown
00024 import rocon_python_utils
00025 import rocon_launch
00026 
00027 from . import loggers
00028 from . import runner
00029 
00030 ##############################################################################
00031 # Methods
00032 ##############################################################################
00033 
00034 
00035 def help_string():
00036     overview = 'Launches a rocon multi-master test.\n\n'
00037     return overview
00038 
00039 
00040 def _parse_arguments():
00041     '''
00042     @raise IOError : if no package name is given and the rocon launcher cannot be found on the filesystem.
00043     '''
00044     parser = argparse.ArgumentParser(description=help_string(), formatter_class=RawTextHelpFormatter)
00045     parser.add_argument('package', nargs='?', default=None, help='name of the package in which to find the test configuration')
00046     parser.add_argument('test', nargs=1, help='name of the test configuration (xml) file')
00047     parser.add_argument('-l', '--launch', action='store_true', help='launch each component with rocon_launch [false]')
00048     parser.add_argument('-p', '--pause', action='store_true', help='pause before tearing down so you can introspect easily [false]')
00049     parser.add_argument('-s', '--screen', action='store_true', help='run each roslaunch with the --screen option')
00050     parser.add_argument('-t', '--text-mode', action='store_true', help='log the rostest output to screen rather than log file.')
00051     parser.add_argument("--results-filename", action='store', type=str, default=None, help="results_filename")
00052     args = parser.parse_args()
00053     # Stop it from being a list (happens when nargs is an integer)
00054     args.test = args.test[0]
00055     if args.screen:
00056         args.screen = "--screen"
00057     else:
00058         args.screen = ""
00059     if not args.package:
00060         if not os.path.isfile(args.test):
00061             raise IOError("Test launcher file does not exist [%s]." % args.test)
00062         else:
00063             args.package = rospkg.get_package_name(args.test)
00064     return (args.package, args.test, args.screen, args.pause, args.text_mode, args.results_filename)
00065 
00066 
00067 def test_main():
00068     (package, name, launch_arguments, pause, text_mode, results_filename) = _parse_arguments()
00069 
00070     if os.path.isabs(name):
00071         if os.path.exists(name):
00072             rocon_launcher = name
00073         else:
00074             raise IOError("cannot locate [%s]" % name)
00075     else:
00076         rocon_launcher = rocon_python_utils.ros.find_resource(package, name)  # raises an IO error if there is a problem.
00077 
00078     if results_filename:
00079         results_log_name = results_filename
00080         if '.' in results_log_name:
00081             results_log_name = results_log_name[:results_log_name.rfind('.')]
00082         results_log_file = loggers.xml_results_file(package, results_log_name)
00083     else:
00084         results_log_name, results_log_file = loggers.configure_logging(package, rocon_launcher)
00085 
00086     launchers = rocon_launch.parse_rocon_launcher(rocon_launcher, launch_arguments, args_mappings={})
00087 
00088     try:
00089         test_case = runner.create_unit_rocon_test(rocon_launcher, launchers)
00090         print("Unit test loader")
00091         suite = unittest.TestLoader().loadTestsFromTestCase(test_case)
00092         if pause:
00093             runner.set_pause_mode(True)
00094         if text_mode:
00095             runner.set_text_mode(True)
00096             result = unittest.TextTestRunner(verbosity=2).run(suite)
00097         else:
00098             xml_runner = rosunit.create_xml_runner(package, results_log_name, \
00099                                          results_file=results_log_file, \
00100                                          is_rostest=True)
00101             result = xml_runner.run(suite)
00102     finally:
00103         # really make sure that all of our processes have been killed (should be automatic though)
00104         test_parents = runner.get_rocon_test_parents()
00105         for r in test_parents:
00106             r.tearDown()
00107         del test_parents[:]
00108         pmon_shutdown()
00109     subtest_results = runner.get_results()
00110 
00111     ################################
00112     # Post stuff
00113     ################################
00114     config = rostest.runner.getConfig()
00115     if config:
00116         if config.config_errors:
00117             print("\n[ROCON_TEST WARNINGS]" + '-' * 62 + '\n', file=sys.stderr)
00118         for err in config.config_errors:
00119             print(" * %s" % err, file=sys.stderr)
00120         print('')
00121 
00122     if not text_mode:
00123         printRostestSummary(result, subtest_results)
00124         loggers.printlog("results log file is in %s" % results_log_file)
00125 
00126     # This is not really a useful log, so dont worry about showing it.
00127     # if log_name:
00128     #     loggers.printlog("rocon_test log file is in %s" % log_name)
00129 
00130     if not result.wasSuccessful():
00131         sys.exit(1)
00132     elif subtest_results.num_errors or subtest_results.num_failures:
00133         sys.exit(2)


rocon_test
Author(s): Daniel Stonier
autogenerated on Sat Jun 8 2019 18:48:47