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

Source Code for Module rosunit.core

  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: core.py 12792 2011-01-11 23:46:51Z kwc $ 
 34   
 35  import os 
 36  import sys 
 37  import logging 
 38  import roslib.rosenv 
 39   
 40  from .xmlrunner import XMLTestRunner 
 41   
 42  XML_OUTPUT_FLAG = '--gtest_output=xml:' #use gtest-compatible flag 
 43   
 44  _logger = logging.getLogger('runner') 
45 -def printlog(msg, *args):
46 if args: 47 msg = msg%args 48 _logger.info(msg) 49 print "[ROSUNIT]"+msg
50
51 -def printlog_bold(msg, *args):
52 if args: 53 msg = msg%args 54 _logger.info(msg) 55 print '\033[1m[ROSUNIT]%s\033[0m'%msg
56
57 -def printerrlog(msg, *args):
58 if args: 59 msg = msg%args 60 _logger.error(msg) 61 print >> sys.stderr, "[ROSUNIT]"+msg
62
63 -def xml_results_file(test_pkg, test_name, is_rostest=False):
64 """ 65 @param test_pkg: name of test's package 66 @type test_pkg: str 67 @param test_name str: name of test 68 @type test_name: str 69 @param is_rostest: True if the results file is for a rostest-generated unit instance 70 @type is_rostest: bool 71 @return: name of xml results file for specified test 72 @rtype: str 73 """ 74 test_dir = os.path.join(roslib.rosenv.get_test_results_dir(), test_pkg) 75 if not os.path.exists(test_dir): 76 try: 77 roslib.rosenv.makedirs_with_parent_perms(test_dir) 78 except OSError: 79 raise IOError("cannot create test results directory [%s]. Please check permissions."%(test_dir)) 80 81 # #576: strip out chars that would bork the filename 82 # this is fairly primitive, but for now just trying to catch some common cases 83 for c in ' "\'&$!`/\\': 84 if c in test_name: 85 test_name = test_name.replace(c, '_') 86 if is_rostest: 87 return os.path.join(test_dir, 'TEST-rostest__%s.xml'%test_name) 88 else: 89 return os.path.join(test_dir, 'TEST-%s.xml'%test_name)
90
91 -def rostest_name_from_path(pkg_dir, test_file):
92 """ 93 Derive name of rostest based on file name/path. rostest follows a 94 certain convention defined above. 95 96 @return: name of test 97 @rtype: str 98 """ 99 test_file_abs = os.path.abspath(test_file) 100 if test_file_abs.startswith(pkg_dir): 101 # compute package-relative path 102 test_file = test_file_abs[len(pkg_dir):] 103 if test_file[0] == os.sep: 104 test_file = test_file[1:] 105 outname = test_file.replace(os.sep, '_') 106 if '.' in outname: 107 outname = outname[:outname.rfind('.')] 108 return outname
109
110 -def create_xml_runner(test_pkg, test_name, results_file=None, is_rostest=False):
111 """ 112 Create the unittest test runner with XML output 113 @param test_pkg: package name 114 @type test_pkg: str 115 @param test_name: test name 116 @type test_name: str 117 @param is_rostest: if True, use naming scheme for rostest itself instead of individual unit test naming 118 @type is_rostest: bool 119 """ 120 test_name = os.path.basename(test_name) 121 # determine output xml file name 122 if not results_file: 123 results_file = xml_results_file(test_pkg, test_name, is_rostest) 124 test_dir = os.path.abspath(os.path.dirname(results_file)) 125 if not os.path.exists(test_dir): 126 try: 127 roslib.rosenv.makedirs_with_parent_perms(test_dir) #NOTE: this will pass up an error exception if it fails 128 except OSError: 129 raise IOError("cannot create test results directory [%s]. Please check permissions."%(test_dir)) 130 131 elif os.path.isfile(test_dir): 132 raise Exception("ERROR: cannot run test suite, file is preventing creation of test dir: %s"%test_dir) 133 134 print "[ROSUNIT] Outputting test results to %s"%results_file 135 outstream = open(results_file, 'w') 136 return XMLTestRunner(stream=outstream)
137