File: wvtest.py

package info (click to toggle)
bup 0.17b-2squeeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,416 kB
  • ctags: 1,420
  • sloc: python: 18,377; ansic: 311; sh: 284; perl: 160; makefile: 129
file content (156 lines) | stat: -rwxr-xr-x 5,400 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python
import traceback
import os
import re
import sys

if __name__ != "__main__":   # we're imported as a module
    _registered = []
    _tests = 0
    _fails = 0

    def wvtest(func):
        """ Use this decorator (@wvtest) in front of any function you want to run
            as part of the unit test suite.  Then run:
                python wvtest.py path/to/yourtest.py
            to run all the @wvtest functions in that file.
        """
        _registered.append(func)
        return func


    def _result(msg, tb, code):
        global _tests, _fails
        _tests += 1
        if code != 'ok':
            _fails += 1
        (filename, line, func, text) = tb
        filename = os.path.basename(filename)
        msg = re.sub(r'\s+', ' ', str(msg))
        sys.stderr.flush()
        print '! %-70s %s' % ('%s:%-4d %s' % (filename, line, msg),
                              code)
        sys.stdout.flush()


    def _check(cond, msg = 'unknown', tb = None):
        if tb == None: tb = traceback.extract_stack()[-3]
        if cond:
            _result(msg, tb, 'ok')
        else:
            _result(msg, tb, 'FAILED')
        return cond


    def _code():
        (filename, line, func, text) = traceback.extract_stack()[-3]
        text = re.sub(r'^\w+\((.*)\)(\s*#.*)?$', r'\1', text);
        return text


    def WVPASS(cond = True):
        ''' Counts a test failure unless cond is true. '''
        return _check(cond, _code())

    def WVFAIL(cond = True):
        ''' Counts a test failure  unless cond is false. '''
        return _check(not cond, 'NOT(%s)' % _code())

    def WVPASSEQ(a, b):
        ''' Counts a test failure unless a == b. '''
        return _check(a == b, '%s == %s' % (repr(a), repr(b)))

    def WVPASSNE(a, b):
        ''' Counts a test failure unless a != b. '''
        return _check(a != b, '%s != %s' % (repr(a), repr(b)))

    def WVPASSLT(a, b):
        ''' Counts a test failure unless a < b. '''
        return _check(a < b, '%s < %s' % (repr(a), repr(b)))

    def WVPASSLE(a, b):
        ''' Counts a test failure unless a <= b. '''
        return _check(a <= b, '%s <= %s' % (repr(a), repr(b)))

    def WVPASSGT(a, b):
        ''' Counts a test failure unless a > b. '''
        return _check(a > b, '%s > %s' % (repr(a), repr(b)))

    def WVPASSGE(a, b):
        ''' Counts a test failure unless a >= b. '''
        return _check(a >= b, '%s >= %s' % (repr(a), repr(b)))

    def WVEXCEPT(etype, func, *args, **kwargs):
        ''' Counts a test failure unless func throws an 'etype' exception.
            You have to spell out the function name and arguments, rather than
            calling the function yourself, so that WVEXCEPT can run before
            your test code throws an exception.
        '''
        try:
            func(*args, **kwargs)
        except etype, e:
            return _check(True, 'EXCEPT(%s)' % _code())
        except:
            _check(False, 'EXCEPT(%s)' % _code())
            raise
        else:
            return _check(False, 'EXCEPT(%s)' % _code())

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:]:
        if not os.path.exists(modname):
            print 'Skipping: %s' % modname
            continue
        if modname.endswith('.py'):
            modname = modname[:-3]
        print 'Importing: %s' % modname
        wvtest._registered = []
        oldwd = os.getcwd()
        oldpath = sys.path
        try:
            modpath = os.path.abspath(modname).split('/')[:-1]
            os.chdir('/'.join(modpath))
            sys.path += ['/'.join(modpath),
                         '/'.join(modpath[:-1])]
            mod = __import__(modname.replace('/', '.'), None, None, [])
            for t in wvtest._registered:
                _runtest(modname, t.func_name, t)
                print
        finally:
            os.chdir(oldwd)
            sys.path = oldpath

    print
    print 'WvTest: %d tests, %d failures.' % (wvtest._tests, wvtest._fails)