home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / BitTorrent / testtest.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  2.3 KB  |  86 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. """
  5. A much simpler testing framework than PyUnit
  6.  
  7. tests a module by running all functions in it whose name starts with 'test'
  8.  
  9. a test fails if it raises an exception, otherwise it passes
  10.  
  11. functions are try_all and try_single
  12. """
  13. from traceback import print_exc
  14. from sys import modules
  15.  
  16. def try_all(excludes = [], excluded_paths = []):
  17.     '''
  18.     tests all imported modules
  19.  
  20.     takes an optional list of module names and/or module objects to skip over.
  21.     modules from files under under any of excluded_paths are also skipped.
  22.     '''
  23.     failed = []
  24.     for modulename, module in modules.items():
  25.         if not hasattr(module, '__file__'):
  26.             continue
  27.         
  28.         if modulename not in excludes and module not in excludes:
  29.             try_module(module, modulename, failed)
  30.             continue
  31.         None if _[1] else []
  32.     
  33.     print_failed(failed)
  34.  
  35.  
  36. def try_single(m):
  37.     '''
  38.     tests a single module
  39.     
  40.     accepts either a module object or a module name in string form
  41.     '''
  42.     if type(m) is str:
  43.         modulename = m
  44.         module = __import__(m)
  45.     else:
  46.         modulename = str(m)
  47.         module = m
  48.     failed = []
  49.     try_module(module, modulename, failed)
  50.     print_failed(failed)
  51.  
  52.  
  53. def try_module(module, modulename, failed):
  54.     if not hasattr(module, '__dict__'):
  55.         return None
  56.     
  57.     for n, func in module.__dict__.items():
  58.         if not callable(func) or n[:4] != 'test':
  59.             continue
  60.         
  61.         name = modulename + '.' + n
  62.         
  63.         try:
  64.             print 'trying ' + name
  65.             func()
  66.             print 'passed ' + name
  67.         continue
  68.         print_exc()
  69.         failed.append(name)
  70.         print 'failed ' + name
  71.         continue
  72.  
  73.     
  74.  
  75.  
  76. def print_failed(failed):
  77.     print 
  78.     if len(failed) == 0:
  79.         print 'everything passed'
  80.     else:
  81.         print 'the following tests failed:'
  82.         for i in failed:
  83.             print i
  84.         
  85.  
  86.