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$ 
 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   
54 -def rosunitmain():
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 # If the caller didn't tell us the package name, we'll try to infer it. 85 # compute some common names we'll be using to generate test names and files 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 # the is_rostest really just means "wrapper" 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 # summary is worthless if textMode is on as we cannot scrape .xml results 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