This repository has been archived by the owner on May 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
utest.py
executable file
·155 lines (112 loc) · 4.3 KB
/
utest.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import os
import logging
import unittest
from datetime import datetime
from termcolor import colored
logging.basicConfig(level=logging.FATAL)
class TestResult(unittest.TextTestResult):
def __init__(self, stream, descriptions, verbosity, log):
super(TestResult, self).__init__(stream, descriptions, verbosity)
self.log = log
def startTest(self, test):
unittest.result.TestResult.startTest(self, test)
if self.showAll:
# self.stream.write("%-120s " % self.getDescription(test))
self.stream.write("%-120s " % str(test))
self.stream.flush()
def addError(self, test, err):
unittest.result.TestResult.addError(self, test, err)
error = "[ERROR]"
if self.showAll:
self.stream.writeln(colored(error, 'red'))
elif self.dots:
self.stream.write('E')
self.stream.flush()
self.log.error("%-120s %s" % (test, error))
self.log.exception(err[1])
def addFailure(self, test, err):
unittest.result.TestResult.addFailure(self, test, err)
fail = "[FAIL]"
if self.showAll:
self.stream.writeln(colored(fail, 'red'))
elif self.dots:
self.stream.write('F')
self.stream.flush()
self.log.error("%-120s %s" % (test, fail))
# self.log.error(self.separator1)
# self.log.error("%s: %s" % ('FAIL', self.getDescription(test)))
# self.log.error(self.separator2)
err_str = self._exc_info_to_string(err, test)
for line in err_str.splitlines():
self.log.error(line)
self.log.error('')
def addSkip(self, test, reason):
unittest.result.TestResult.addSkip(self, test, reason)
skipped = "[SKIPPED] (%s)" % reason
if self.showAll:
self.stream.writeln(colored(skipped, 'yellow'))
elif self.dots:
self.stream.write("s")
self.stream.flush()
self.log.info("%-120s %s" % (test, skipped))
def addSuccess(self, test):
unittest.result.TestResult.addSuccess(self, test)
ok = "[OK]"
if self.showAll:
self.stream.writeln(colored(ok, 'green'))
elif self.dots:
self.stream.write('.')
self.stream.flush()
self.log.info("%-75s %s" % (test, ok))
class TestRunner(unittest.TextTestRunner):
resultclass = TestResult
def __init__(self, log, stream=sys.stderr, descriptions=True, verbosity=1,
failfast=True, buffer=False, resultclass=None):
super(TestRunner, self).__init__(stream, descriptions, verbosity, failfast,
buffer, resultclass)
self.log = log
def _makeResult(self):
return self.resultclass(self.stream, self.descriptions,
self.verbosity, self.log)
def find_test_packages(suite, retval=set()):
for item in suite:
if isinstance(item, unittest.TestSuite):
find_test_packages(item, retval)
else:
retval.add(item.__module__)
return retval
def find_tests(path=None):
# Find test cases
if path is None:
path = os.path.abspath(os.path.dirname(__file__))
loader = unittest.TestLoader()
suites = loader.discover(path)
packages = find_test_packages(suites)
log = logging.getLogger('utest')
log.info("Found the following packages with tests:")
for p in packages:
log.info(" * %s" % p)
return suites
def run_tests(suites):
log = logging.getLogger('utest')
print('-' * 135)
print('Started: ' + datetime.now().strftime("%d-%m-%Y %H:%M:%S"))
print('-' * 135)
# Setting verbosity=1 will display dots instead.
result = TestRunner(log, verbosity=2).run(suites)
log.info(result)
sys.exit(not result.wasSuccessful())
# ------------------------------------------------------------------------------
# run
# ------------------------------------------------------------------------------
def run():
suites = find_tests()
run_tests(suites)
# ------------------------------------------------------------------------------
# __main__
# ------------------------------------------------------------------------------
if __name__ == '__main__':
run()