7 if __name__ != "__main__": # we're imported as a module
13 """ Use this decorator (@wvtest) in front of any function you want to run
14 as part of the unit test suite. Then run:
15 python wvtest.py path/to/yourtest.py
16 to run all the @wvtest functions in that file.
18 _registered.append(func)
22 def _result(msg, tb, code):
27 (filename, line, func, text) = tb
28 filename = os.path.basename(filename)
29 msg = re.sub(r'\s+', ' ', str(msg))
31 print '! %-70s %s' % ('%s:%-4d %s' % (filename, line, msg),
36 def _check(cond, msg = 'unknown', tb = None):
37 if tb == None: tb = traceback.extract_stack()[-3]
39 _result(msg, tb, 'ok')
41 _result(msg, tb, 'FAILED')
46 (filename, line, func, text) = traceback.extract_stack()[-3]
47 text = re.sub(r'^\w+\((.*)\)$', r'\1', unicode(text));
51 def WVPASS(cond = True):
52 ''' Throws an exception unless cond is true. '''
53 return _check(cond, _code())
55 def WVFAIL(cond = True):
56 ''' Throws an exception unless cond is false. '''
57 return _check(not cond, 'NOT(%s)' % _code())
60 ''' Throws an exception unless a == b. '''
61 return _check(a == b, '%s == %s' % (repr(a), repr(b)))
64 ''' Throws an exception unless a != b. '''
65 return _check(a != b, '%s != %s' % (repr(a), repr(b)))
68 ''' Throws an exception unless a < b. '''
69 return _check(a < b, '%s < %s' % (repr(a), repr(b)))
72 ''' Throws an exception unless a <= b. '''
73 return _check(a <= b, '%s <= %s' % (repr(a), repr(b)))
76 ''' Throws an exception unless a > b. '''
77 return _check(a > b, '%s > %s' % (repr(a), repr(b)))
80 ''' Throws an exception unless a >= b. '''
81 return _check(a >= b, '%s >= %s' % (repr(a), repr(b)))
83 else: # we're the main program
85 # Why do we do this in such convoluted way? Because if you run
86 # wvtest.py as a main program and it imports your test files, then
87 # those test files will try to import the wvtest module recursively.
88 # That actually *works* fine, because we don't run this main program
89 # when we're imported as a module. But you end up with two separate
90 # wvtest modules, the one that gets imported, and the one that's the
91 # main program. Each of them would have duplicated global variables
92 # (most importantly, wvtest._registered), and so screwy things could
93 # happen. Thus, we make the main program module *totally* different
94 # from the imported module. Then we import wvtest (the module) into
95 # wvtest (the main program) here and make sure to refer to the right
96 # versions of global variables.
98 # All this is done just so that wvtest.py can be a single file that's
99 # easy to import into your own applications.
102 def _runtest(modname, fname, f):
104 print 'Testing "%s" in %s.py:' % (fname, modname)
110 print traceback.format_exc()
111 tb = sys.exc_info()[2]
112 wvtest._result(e, traceback.extract_tb(tb)[-1],
116 for modname in sys.argv[1:]:
117 if not os.path.exists(modname):
118 print 'Skipping: %s' % modname
120 if modname.endswith('.py'):
121 modname = modname[:-3]
122 print 'Importing: %s' % modname
123 wvtest._registered = []
124 mod = __import__(modname.replace('/', '.'), None, None, [])
126 for t in wvtest._registered:
127 _runtest(modname, t.func_name, t)
131 print 'WvTest: %d tests, %d failures.' % (wvtest._tests, wvtest._fails)