Package rosunit :: Module rosunit_main
[frames] | no frames]

Source Code for Module rosunit.rosunit_main

  1  # Software License Agreement (BSD License) 
  2  # 
  3  # Copyright (c) 2008, Willow Garage, Inc. 
  4  # All rights reserved. 
  5  # 
  6  # Redistribution and use in source and binary forms, with or without 
  7  # modification, are permitted provided that the following conditions 
  8  # are met: 
  9  # 
 10  #  * Redistributions of source code must retain the above copyright 
 11  #    notice, this list of conditions and the following disclaimer. 
 12  #  * Redistributions in binary form must reproduce the above 
 13  #    copyright notice, this list of conditions and the following 
 14  #    disclaimer in the documentation and/or other materials provided 
 15  #    with the distribution. 
 16  #  * Neither the name of Willow Garage, Inc. nor the names of its 
 17  #    contributors may be used to endorse or promote products derived 
 18  #    from this software without specific prior written permission. 
 19  # 
 20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 21  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 22  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 23  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 24  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 25  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 26  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 27  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 28  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 29  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 30  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  # POSSIBILITY OF SUCH DAMAGE. 
 32  # 
 33  # Revision $Id: rosunit_main.py 12110 2010-11-10 21:36:28Z kwc $ 
 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   
55 -def configure_logging(test_name):
56 logfile_basename = 'rosunit-%s.log'%(test_name) 57 logfile_name = roslib.roslogging.configure_logging('rosunit-%s'%(test_name), filename=logfile_basename) 58 if logfile_name: 59 print "... logging to %s"%logfile_name 60 return logfile_name
61
62 -def rosunitmain():
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 # compute some common names we'll be using to generate test names and files 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 # the is_rostest really just means "wrapper" 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 # summary is worthless if textMode is on as we cannot scrape .xml results 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