Package pytilities :: Module testing
[hide private]
[frames] | no frames]

Source Code for Module pytilities.testing

 1  # Copyright (C) 2010 Tim Diels <limyreth@users.sourceforge.net> 
 2  #  
 3  # This file is part of pytilities. 
 4  #  
 5  # pytilities is free software: you can redistribute it and/or modify 
 6  # it under the terms of the GNU General Public License as published by 
 7  # the Free Software Foundation, either version 3 of the License, or 
 8  # (at your option) any later version. 
 9  #  
10  # pytilities is distributed in the hope that it will be useful, 
11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
13  # GNU General Public License for more details. 
14  #  
15  # You should have received a copy of the GNU General Public License 
16  # along with pytilities.  If not, see <http://www.gnu.org/licenses/>. 
17  # 
18   
19  """ 
20  Test utilities 
21  """ 
22   
23  __docformat__ = 'reStructuredText' 
24   
25  import unittest 
26  import os 
27  import logging 
28   
29  _logger = logging.getLogger("pytilities.testing") 
30   
31 -def get_recursive_package_test(root, test_package):
32 """ 33 Get a test suite of all tests inside a package and its child packages 34 35 Parameters: 36 37 `root`:: string 38 absolute path of the top package's directory of this python program 39 40 `test_package`:: string 41 name of package to search in for tests, in dotted format, e.g. 42 'project.test' 43 44 See the wiki for a usage example. 45 46 Returns ::unittest.TestSuite 47 """ 48 test_suite = unittest.TestSuite() 49 50 for parent, dirs, files in os.walk( 51 os.path.join(root, test_package.replace('.', os.path.sep))): 52 53 if parent == '.svn' or '__init__.py' not in files: 54 continue 55 56 for file_ in files: 57 if os.path.splitext(file_)[1] != '.py': 58 continue 59 60 file_path = os.path.join(parent[len(root)+1:], 61 file_[:-3]) 62 63 module_full_name = file_path.replace(os.sep, '.') 64 65 _logger.debug('Loading: %s' % module_full_name) 66 67 test_suite.addTest( 68 unittest.defaultTestLoader.loadTestsFromName(module_full_name)) 69 70 return test_suite
71
72 -def run(test):
73 """ 74 Run test and print results to stderr 75 76 Parameters: 77 78 `test` 79 the TestCase to run 80 """ 81 runner = unittest.TextTestRunner() 82 return runner.run(test)
83