📄 testtest.py
字号:
"""A much simpler testing framework than PyUnittests a module by running all functions in it whose name starts with 'test'a test fails if it raises an exception, otherwise it passesfunctions are try_all and try_single"""# Written by Bram Cohen# see LICENSE.txt for license informationimport tracebackimport sysimport typesdef try_all(excludes = [], excluded_paths=[]): """ tests all imported modules takes an optional list of module names and/or module objects to skip over. modules from files under under any of excluded_paths are also skipped. """ failed = [] for modulename, module in sys.modules.items(): # skip builtins if not hasattr(module, '__file__'): continue # skip modules under any of excluded_paths if [p for p in excluded_paths if module.__file__.startswith(p)]: continue if modulename not in excludes and module not in excludes: try_module(module, modulename, failed) print_failed(failed)def try_single(m): """ tests a single module accepts either a module object or a module name in string form """ if type(m) == types.StringType: modulename = m module = __import__(m) else: modulename = str(m) module = m failed = [] try_module(module, modulename, failed) print_failed(failed)def try_module(module, modulename, failed): if not hasattr(module, '__dict__'): return for n, func in module.__dict__.items(): if not callable(func) or n[:4] != 'test': continue name = modulename + '.' + n try: print 'trying ' + name func() print 'passed ' + name except: traceback.print_exc() failed.append(name) print 'failed ' + namedef print_failed(failed): print if len(failed) == 0: print 'everything passed' else: print 'the following tests failed:' for i in failed: print i
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -