/
runtests.py
executable file
·164 lines (134 loc) · 4.19 KB
/
runtests.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
156
157
158
159
160
161
162
163
164
# -*- coding: latin-1 -*-
# Runs module tests.
#
# example: python runtests.py pynotebook/pyclient.py
#
# TODO:
# - Commandline arguments --silent, --redirect
from __future__ import absolute_import
from __future__ import print_function
import sys
sys.path.insert(0, ".")
import types
import traceback
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
import inspect
def get_tb(skip=0):
type, value, tb = sys.exc_info()
entries = traceback.format_tb(tb)[skip:]
msg = traceback.format_exc().splitlines()[-1]
return ''.join(entries)+msg
class Tester:
bkstdout = sys.stdout
bkstderr = sys.stderr
def __init__(self, silent=False, redirect=True, profile=False):
self.redirect = redirect
self.silent = silent
self.profile = profile
self.reset_buffer()
def reset_buffer(self):
self.stdoutbuffer = StringIO()
self.stderrbuffer = StringIO()
def redirect_io(self):
#print "stdout:", sys.stdout
assert sys.stdout is self.bkstdout
assert sys.stderr is self.bkstderr
sys.stdout = self.stdoutbuffer
sys.stderr = self.stderrbuffer
def restore_io(self):
#assert sys.stdout is self.stdoutbuffer
#assert sys.stderr is self.stderrbuffer
sys.stdout = self.bkstdout
sys.stderr = self.bkstderr
#self.bkstdout.write("stdout: %s\n" % repr(sys.stdout))
def test_callable(self, name, obj):
doc = getattr(obj, '__doc__', '') or ''
text = '> %s: %s' % (name, doc)
text += '.'*(60-len(text))
print(text, end=' ')
if self.redirect:
self.reset_buffer()
self.redirect_io()
ok = True
if self.profile:
from cProfile import runctx
try:
runctx("obj()", globals(), locals())
except:
ok = False
tb = get_tb(1)
finally:
self.restore_io()
else:
try:
obj()
except:
ok = False
tb = get_tb(1)
finally:
self.restore_io()
if ok:
print("ok")
if not self.redirect:
return
if self.silent:
return
if self.stdoutbuffer.getvalue():
print("stdout:")
print(self.stdoutbuffer.getvalue())
if self.stderrbuffer.getvalue():
print("stderr:")
print(self.stderrbuffer.getvalue())
else:
if self.stdoutbuffer.getvalue():
print("stdout:")
print(self.stdoutbuffer.getvalue())
if self.stderrbuffer.getvalue():
print("stderr:")
print(self.stderrbuffer.getvalue())
print("error")
print(tb)
return ok
def test_library(modulname, silent=False, profile=False, names=()):
import importlib
library = importlib.import_module(modulname)
if not names:
names = []
for name in dir(library):
obj = getattr(library, name)
if name.startswith('test_') and callable(obj):
names.append(name)
names.sort()
hashes = '#' * int((60-len(filename))/2)
print(" %s %s %s" % (hashes, filename, hashes))
tester = Tester(silent=silent, profile=profile)
n_ok = 0
n = 0
for name in names:
obj = getattr(library, name)
n += 1
ok = tester.test_callable(name, obj)
if ok:
n_ok += 1
print("Number of tests:\t%i" % n)
print("Tests failed: \t%i" % (n-n_ok))
import sys
profile = False
silent = False
for name in sys.argv:
if name == '--silent':
silent = True
sys.argv.remove(name)
elif name == '--profile':
profile = True
sys.argv.remove(name)
name = sys.argv[1]
if name.lower().endswith('.py'):
name = name[:-3]
filename = name.replace('/', '.')
fun_names = sys.argv[2:]
print("testing:", filename)
test_library(filename, names=fun_names, silent=silent, profile=profile)