2
A much simpler testing framework than PyUnit
4
tests a module by running all functions in it whose name starts with 'test'
6
a test fails if it raises an exception, otherwise it passes
8
functions are try_all and try_single
11
# Written by Bram Cohen
12
# see LICENSE.txt for license information
14
from traceback import print_exc
15
from sys import modules
17
def try_all(excludes = [], excluded_paths=[]):
19
tests all imported modules
21
takes an optional list of module names and/or module objects to skip over.
22
modules from files under under any of excluded_paths are also skipped.
25
for modulename, module in modules.items():
27
if not hasattr(module, '__file__'):
29
# skip modules under any of excluded_paths
30
if [p for p in excluded_paths if module.__file__.startswith(p)]:
32
if modulename not in excludes and module not in excludes:
33
try_module(module, modulename, failed)
40
accepts either a module object or a module name in string form
44
module = __import__(m)
49
try_module(module, modulename, failed)
52
def try_module(module, modulename, failed):
53
if not hasattr(module, '__dict__'):
55
for n, func in module.__dict__.items():
56
if not callable(func) or n[:4] != 'test':
58
name = modulename + '.' + n
60
print 'trying ' + name
62
print 'passed ' + name
66
print 'failed ' + name
68
def print_failed(failed):
71
print 'everything passed'
73
print 'the following tests failed:'