-else: # we're the main program
- # NOTE
- # Why do we do this in such a convoluted way? Because if you run
- # wvtest.py as a main program and it imports your test files, then
- # those test files will try to import the wvtest module recursively.
- # That actually *works* fine, because we don't run this main program
- # when we're imported as a module. But you end up with two separate
- # wvtest modules, the one that gets imported, and the one that's the
- # main program. Each of them would have duplicated global variables
- # (most importantly, wvtest._registered), and so screwy things could
- # happen. Thus, we make the main program module *totally* different
- # from the imported module. Then we import wvtest (the module) into
- # wvtest (the main program) here and make sure to refer to the right
- # versions of global variables.
- #
- # All this is done just so that wvtest.py can be a single file that's
- # easy to import into your own applications.
- import wvtest
-
- def _runtest(modname, fname, f):
- print
- print 'Testing "%s" in %s.py:' % (fname, modname)
- sys.stdout.flush()
- try:
- f()
- except Exception, e:
- print
- print traceback.format_exc()
- tb = sys.exc_info()[2]
- wvtest._result(e, traceback.extract_tb(tb)[1], 'EXCEPTION')
-
- # main code
- for modname in sys.argv[1:]:
+ wvstart = WVSTART
+ wvmsg = WVMSG
+ wvpass = WVPASS
+ wvfail = WVFAIL
+ wvpasseq = WVPASSEQ
+ wvpassne = WVPASSNE
+ wvpaslt = WVPASSLT
+ wvpassle = WVPASSLE
+ wvpassgt = WVPASSGT
+ wvpassge = WVPASSGE
+ wvexcept = WVEXCEPT
+
+ def wvfailure_count():
+ return _fails
+
+ def _check_unfinished():
+ if _registered:
+ for func in _registered:
+ print('WARNING: not run: %r' % (func,))
+ WVFAIL('wvtest_main() not called')
+ if _fails:
+ sys.exit(1)
+
+ atexit.register(_check_unfinished)
+
+
+def _run_in_chdir(path, func, *args, **kwargs):
+ oldwd = os.getcwd()
+ oldpath = sys.path
+ try:
+ os.chdir(path)
+ sys.path += [path, os.path.split(path)[0]]
+ return func(*args, **kwargs)
+ finally:
+ os.chdir(oldwd)
+ sys.path = oldpath
+
+
+def _runtest(fname, f):
+ mod = inspect.getmodule(f)
+ rpath = relpath(mod.__file__, os.getcwd()).replace('.pyc', '.py')
+ print()
+ print('Testing "%s" in %s:' % (fname, rpath))
+ sys.stdout.flush()
+ try:
+ _run_in_chdir(os.path.split(mod.__file__)[0], f)
+ except Exception as e:
+ print()
+ print(traceback.format_exc())
+ tb = sys.exc_info()[2]
+ wvtest._result(e, traceback.extract_tb(tb)[1], 'EXCEPTION')
+
+
+def _run_registered_tests():
+ import wvtest as _wvtestmod
+ while _wvtestmod._registered:
+ t = _wvtestmod._registered.pop(0)
+ _runtest(t.__name__, t)
+ print()
+
+
+def wvtest_main(extra_testfiles=tuple()):
+ import wvtest as _wvtestmod
+ _run_registered_tests()
+ for modname in extra_testfiles: