-
Notifications
You must be signed in to change notification settings - Fork 6
/
runtests.py
291 lines (245 loc) · 9.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
"""Run asyncnsq unittests.
Usage:
python3 runtests.py [flags] [pattern] ...
Patterns are matched against the fully qualified name of the test,
including package, module, class and method,
e.g. 'tests.test_events.PolicyTests.testPolicy'.
For full help, try --help.
runtests.py --coverage is equivalent of:
$(COVERAGE) run --branch runtests.py -v
$(COVERAGE) html $(list of files)
$(COVERAGE) report -m $(list of files)
"""
# Originally written by Beech Horn (for NDB).
import argparse
import gc
import logging
import os
import re
import shutil
import sys
import unittest
import traceback
import textwrap
import importlib.machinery
try:
import coverage
except ImportError:
coverage = None
from unittest.signals import installHandler
assert sys.version >= '3.3', 'Please use Python 3.3 or higher.'
ARGS = argparse.ArgumentParser(description="Run all unittests.")
ARGS.add_argument(
'-v', action="store", dest='verbose',
nargs='?', const=1, type=int, default=0, help='verbose')
ARGS.add_argument(
'-x', action="store_true", dest='exclude', help='exclude tests')
ARGS.add_argument(
'-f', '--failfast', action="store_true", default=False,
dest='failfast', help='Stop on first fail or error')
ARGS.add_argument(
'-c', '--catch', action="store_true", default=False,
dest='catchbreak', help='Catch control-C and display results')
ARGS.add_argument(
'--forever', action="store_true", dest='forever', default=False,
help='run tests forever to catch sporadic errors')
ARGS.add_argument(
'--findleaks', action='store_true', dest='findleaks',
help='detect tests that leak memory')
ARGS.add_argument(
'-q', action="store_true", dest='quiet', help='quiet')
ARGS.add_argument(
'--tests', action="store", dest='testsdir', default='tests',
help='tests directory')
ARGS.add_argument(
'--coverage', action="store_true", dest='coverage',
help='enable html coverage report')
ARGS.add_argument(
'pattern', action="store", nargs="*",
help='optional regex patterns to match test ids (default all tests)')
COV_ARGS = argparse.ArgumentParser(description="Run all unittests.")
COV_ARGS.add_argument(
'--coverage', action="store", dest='coverage', nargs='?', const='',
help='enable coverage report and provide python files directory')
def load_modules(basedir, suffix='.py', *, verbose=False):
def list_dir(prefix, dir):
files = []
modpath = os.path.join(dir, '__init__.py')
if os.path.isfile(modpath):
mod = os.path.split(dir)[-1]
files.append(('{}{}'.format(prefix, mod), modpath))
prefix = '{}{}.'.format(prefix, mod)
for name in os.listdir(dir):
path = os.path.join(dir, name)
if os.path.isdir(path):
files.extend(list_dir('{}{}.'.format(prefix, name), path))
else:
if (name != '__init__.py' and
name.endswith(suffix) and
not name.startswith(('.', '_'))):
files.append(('{}{}'.format(prefix, name[:-3]), path))
return files
mods = []
for modname, sourcefile in list_dir('', basedir):
if modname == 'runtests':
continue
try:
loader = importlib.machinery.SourceFileLoader(modname, sourcefile)
mods.append((loader.load_module(), sourcefile))
except SyntaxError:
raise
except Exception as err:
print("Skipping '{}': {}".format(modname, err), file=sys.stderr)
if verbose:
try:
traceback.print_exc()
except Exception:
pass
return mods
class TestsFinder:
def __init__(self, testsdir, includes=(), excludes=(), *,
verbose=False):
self._testsdir = testsdir
self._includes = includes
self._excludes = excludes
self._verbose = verbose
self.find_available_tests()
def find_available_tests(self):
"""
Find available test classes without instantiating them.
"""
self._test_factories = []
mods = [mod for mod, _ in load_modules(self._testsdir,
verbose=self._verbose)]
for mod in mods:
for name in set(dir(mod)):
if name.endswith('Test'):
self._test_factories.append(getattr(mod, name))
def load_tests(self):
"""
Load test cases from the available test classes and apply
optional include / exclude filters.
"""
loader = unittest.TestLoader()
suite = unittest.TestSuite()
for test_factory in self._test_factories:
tests = loader.loadTestsFromTestCase(test_factory)
if self._includes:
tests = [test
for test in tests
if any(re.search(pat, test.id())
for pat in self._includes)]
if self._excludes:
tests = [test
for test in tests
if not any(re.search(pat, test.id())
for pat in self._excludes)]
suite.addTests(tests)
return suite
class TestResult(unittest.TextTestResult):
def __init__(self, stream, descriptions, verbosity):
super().__init__(stream, descriptions, verbosity)
self.leaks = []
def startTest(self, test):
super().startTest(test)
gc.collect()
def addSuccess(self, test):
super().addSuccess(test)
gc.collect()
if gc.garbage:
if self.showAll:
self.stream.writeln(
" Warning: test created {} uncollectable "
"object(s).".format(len(gc.garbage)))
# move the uncollectable objects somewhere so we don't see
# them again
self.leaks.append((self.getDescription(test), gc.garbage[:]))
del gc.garbage[:]
class TestRunner(unittest.TextTestRunner):
resultclass = TestResult
def run(self, test):
result = super().run(test)
if result.leaks:
self.stream.writeln("{} tests leaks:".format(len(result.leaks)))
for name, leaks in result.leaks:
self.stream.writeln(' '*4 + name + ':')
for leak in leaks:
self.stream.writeln(' '*8 + repr(leak))
return result
def runtests():
args = ARGS.parse_args()
if args.coverage and coverage is None:
URL = "bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py"
print(textwrap.dedent("""
coverage package is not installed.
To install coverage3 for Python 3, you need:
- Setuptools (https://pypi.python.org/pypi/setuptools)
What worked for me:
- download {0}
* curl -O https://{0}
- python3 ez_setup.py
- python3 -m easy_install coverage
""".format(URL)).strip())
sys.exit(1)
testsdir = os.path.abspath(args.testsdir)
if not os.path.isdir(testsdir):
print("Tests directory is not found: {}\n".format(testsdir))
ARGS.print_help()
return
excludes = includes = []
if args.exclude:
excludes = args.pattern
else:
includes = args.pattern
v = 0 if args.quiet else args.verbose + 1
failfast = args.failfast
catchbreak = args.catchbreak
findleaks = args.findleaks
runner_factory = TestRunner if findleaks else unittest.TextTestRunner
if args.coverage:
cov = coverage.coverage(branch=True,
source=['asyncnsq'],
)
cov.start()
finder = TestsFinder(args.testsdir, includes, excludes,
verbose=args.verbose)
self.logger = logging.getself.logger()
if v == 0:
self.logger.setLevel(logging.CRITICAL)
elif v == 1:
self.logger.setLevel(logging.ERROR)
elif v == 2:
self.logger.setLevel(logging.WARNING)
elif v == 3:
self.logger.setLevel(logging.INFO)
elif v >= 4:
self.logger.setLevel(logging.DEBUG)
if catchbreak:
installHandler()
try:
if args.forever:
while True:
tests = finder.load_tests()
result = runner_factory(verbosity=v,
failfast=failfast).run(tests)
if not result.wasSuccessful():
sys.exit(1)
else:
tests = finder.load_tests()
result = runner_factory(verbosity=v,
failfast=failfast).run(tests)
sys.exit(not result.wasSuccessful())
finally:
if args.coverage:
cov.stop()
cov.save()
if os.path.exists('htmlcov'):
shutil.rmtree('htmlcov')
cov.html_report(directory='htmlcov')
print("\nCoverage report:")
cov.report(show_missing=False)
here = os.path.dirname(os.path.abspath(__file__))
print("\nFor html report:")
print("open file://{}/htmlcov/index.html".format(here))
if __name__ == '__main__':
runtests()