Package pypower :: Package t :: Module t_run_tests
[hide private]
[frames] | no frames]

Source Code for Module pypower.t.t_run_tests

  1  # Copyright (C) 2004-2011 Power System Engineering Research Center (PSERC) 
  2  # Copyright (C) 2011 Richard Lincoln 
  3  # 
  4  # PYPOWER is free software: you can redistribute it and/or modify 
  5  # it under the terms of the GNU General Public License as published 
  6  # by the Free Software Foundation, either version 3 of the License, 
  7  # or (at your option) any later version. 
  8  # 
  9  # PYPOWER is distributed in the hope that it will be useful, 
 10  # but WITHOUT ANY WARRANTY], without even the implied warranty of 
 11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 12  # GNU General Public License for more details. 
 13  # 
 14  # You should have received a copy of the GNU General Public License 
 15  # along with PYPOWER. If not, see <http://www.gnu.org/licenses/>. 
 16   
 17  """Run a series of tests. 
 18  """ 
 19   
 20  import sys 
 21   
 22  from time import time 
 23   
 24  from numpy import zeros 
 25   
 26  from pypower.t.t_globals import TestGlobals 
 27   
 28   
29 -def t_run_tests(test_names, verbose=False):
30 """Run a series of tests. 31 32 Runs a set of tests whose names 33 are given in the list C{test_names}. If the optional parameter 34 C{verbose} is true, it prints the details of the individual tests. 35 36 @author: Ray Zimmerman (PSERC Cornell) 37 @author: Richard Lincoln 38 """ 39 ## figure out padding for printing 40 if not verbose: 41 length = zeros(len(test_names), int) 42 for k in range(len(test_names)): 43 length[k] = len(test_names[k]) 44 maxlen = max(length) 45 46 ## initialize statistics 47 num_of_tests = 0 48 counter = 0 49 ok_cnt = 0 50 not_ok_cnt = 0 51 skip_cnt = 0 52 53 t0 = time() 54 for k in range(len(test_names)): 55 if verbose: 56 sys.stdout.write('\n---------- %s ----------\n' % test_names[k]) 57 else: 58 pad = maxlen + 4 - len(test_names[k]) 59 s = '%s' % test_names[k] 60 for _ in range(pad): s += '.' 61 sys.stdout.write(s) 62 63 64 tname = test_names[k] 65 __import__('pypower.t.'+tname) 66 mod = sys.modules['pypower.t.'+tname] #@PydevCodeAnalysisIgnore 67 eval('mod.%s(not verbose)' % tname) 68 69 num_of_tests = num_of_tests + TestGlobals.t_num_of_tests 70 counter = counter + TestGlobals.t_counter 71 ok_cnt = ok_cnt + TestGlobals.t_ok_cnt 72 not_ok_cnt = not_ok_cnt + TestGlobals.t_not_ok_cnt 73 skip_cnt = skip_cnt + TestGlobals.t_skip_cnt 74 75 s = '' 76 status = 0 77 if verbose: 78 s += '\n\n---------- Summary ----------\n' 79 80 if (counter == num_of_tests) and (counter == ok_cnt + skip_cnt) and (not_ok_cnt == 0): 81 if skip_cnt: 82 s += 'All tests successful (%d passed, %d skipped of %d)' % \ 83 (ok_cnt, skip_cnt, num_of_tests) 84 status = 0 85 else: 86 s += 'All tests successful (%d of %d)' % (ok_cnt, num_of_tests) 87 status = 0 88 else: 89 s += 'Ran %d of %d tests: %d passed, %d failed' % \ 90 (counter, num_of_tests, ok_cnt, not_ok_cnt) 91 if skip_cnt: 92 s += ', %d skipped' % skip_cnt 93 status = 1 94 95 s += '\nElapsed time %.2f seconds.\n' % (time() - t0) 96 sys.stdout.write(s) 97 98 return status
99