File: TestSuite.py

package info (click to toggle)
qm 1.1.3-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 8,628 kB
  • ctags: 10,249
  • sloc: python: 41,482; ansic: 20,611; xml: 12,837; sh: 485; makefile: 226
file content (173 lines) | stat: -rw-r--r-- 4,529 bytes parent folder | download | duplicates (5)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import sys, traceback, os

OK = 0
PASSED = 1
FAILED = -1

class TestItem:
    def __init__(self, suite, title):
        self.suite = suite
        self.title = title
        self.messages = []
        self.hasErrors = 0
        self.hasWarnings = 0


        if suite.useColor:
            #self.moveTo = '\033[%dG' % (suite.columns - 9)
            self.colorSuccess = '\033[1;32m'
            self.colorFailure = '\033[1;31m'
            self.colorWarning = '\033[1;33m'
            self.colorNormal = '\033[0;39m'
        else:
            self.colorSuccess = ''
            self.colorFailure = ''
            self.colorWarning = ''
            self.colorNormal = ''

    def finish(self):
        if self.hasErrors:
            msg = self._failure()
            retVal = FAILED
        elif self.hasWarnings:
            msg = self._passed()
            retVal = PASSED
        else:
            msg = self._success()
            retVal = OK

        spaces = self.suite.columns - 9
        spaces = spaces - len(self.title)
        title = self.title + ' '*spaces + msg

        for msg in self.messages:
            print msg[0]
        return retVal

    def debug(self, msg):
        self.messages.append(msg)

    def message(self, msg):
        self.messages.append(msg)

    def warning(self, msg):
        self.messages.append(msg)
        self.hasWarnings = 1

    def error(self, msg, saveTrace=0):
        if self.suite.stopOnError:
            raise msg
        if saveTrace:
            tb = sys.exc_info()[-1]
            ftb = traceback.format_list(traceback.extract_tb(tb))
            if ftb:
                msg = msg + '\n'
                for t in ftb:
                    msg = msg + t
        self.messages.append(msg)
        self.hasErrors = 1

    ### Internal Methods ###
    def _success(self):
        return '[%s  OK  %s]' %(self.colorSuccess, self.colorNormal)

    def _passed(self):
        return '[%sPASSED%s]' %(self.colorWarning, self.colorNormal)

    def _failure(self):
        return '[%sFAILED%s]' %(self.colorFailure, self.colorNormal)


class TestGroup:
    def __init__(self, suite, title=None):
        self.suite = suite
        self.title = title
        self.tests = []
        self.retVal = OK

        if title:
            msg = '********** ' + title + ' **********'
            print msg

    ### Methods ###

    def finish(self):
        for test in self.tests:
            self.retVal = self.retVal or test.finish()
        return self.retVal


    def startTest(self, title):
        test = TestItem(self.suite, title)
        self.tests.append(test)

    def testDone(self):
        if self.tests:
            self.retVal = self.retVal | self.tests[-1].finish()
            del self.tests[-1]
            return self.retVal


class TestSuite:
    def __init__(self, stopOnError=1, useColor=0, cols=80):
        if os.name == 'posix':
            self.useColor = 1
        else:
            self.useColor = useColor
        self.stopOnError = stopOnError
        self.useColor = useColor
        self.columns = cols
        self.groups = []
        self.retVal = OK

    def __del__(self):
        retVal = OK
        while len(self.groups):
            retVal = retVal or group[-1].finish()
        return retVal

    ### Methods ###

    def startGroup(self, title):
        group = TestGroup(self, title)
        self.groups.append(group)

    def groupDone(self):
        retVal = OK
        if self.groups:
            retVal = self.groups[-1].finish()
            del self.groups[-1]
        return retVal

    def startTest(self, title):
        if not self.groups:
            self.startGroup(self)
            print 'Added (null) group'
        self.groups[-1].startTest(title)

    def testDone(self):
        if self.groups:
            self.groups[-1].testDone()

    def testResults(self,expected,actual, done = 1):
        if expected != actual:
            self.error("Expected %s, got %s" % (expected,actual))
            return 0
        elif done:
            self.testDone()
        return 1
    def message(self, msg):
        if self.groups:
            if self.groups[-1].tests:
                self.groups[-1].tests[-1].message(msg)

    def warning(self, msg):
        if self.groups:
            if self.groups[-1].tests:
                self.groups[-1].tests[-1].warning(msg)

    def error(self, msg, saveTrace=0):
        if self.groups:
            if self.groups[-1].tests:
                self.groups[-1].tests[-1].error(msg, saveTrace)