forked from kovidgoyal/kitty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·100 lines (76 loc) · 2.71 KB
/
test.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
#!/usr/bin/env python3
# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
import unittest
import os
import sys
import importlib
base = os.path.dirname(os.path.abspath(__file__))
def init_env():
sys.path.insert(0, base)
def itertests(suite):
stack = [suite]
while stack:
suite = stack.pop()
for test in suite:
if isinstance(test, unittest.TestSuite):
stack.append(test)
continue
if test.__class__.__name__ == 'ModuleImportFailure':
raise Exception('Failed to import a test module: %s' % test)
yield test
def find_tests_in_dir(path, excludes=('main.py',)):
package = os.path.relpath(path, base).replace(os.sep, '/').replace('/', '.')
items = os.listdir(path)
suits = []
for x in items:
if x.endswith('.py') and x not in excludes:
m = importlib.import_module(package + '.' + x.partition('.')[0])
suits.append(unittest.defaultTestLoader.loadTestsFromModule(m))
return unittest.TestSuite(suits)
def filter_tests(suite, test_ok):
ans = unittest.TestSuite()
added = set()
for test in itertests(suite):
if test_ok(test) and test not in added:
ans.addTest(test)
added.add(test)
return ans
def filter_tests_by_name(suite, *names):
names = {x if x.startswith('test_') else 'test_' + x for x in names}
def q(test):
return test._testMethodName in names
return filter_tests(suite, q)
def filter_tests_by_module(suite, *names):
names = frozenset(names)
def q(test):
m = test.__class__.__module__.rpartition('.')[-1]
return m in names
return filter_tests(suite, q)
def run_tests():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
'name', nargs='*', default=[],
help='The name of the test to run, for e.g. linebuf corresponds to test_linebuf. Can be specified multiple times')
parser.add_argument('--verbosity', default=4, type=int, help='Test verbosity')
args = parser.parse_args()
tests = find_tests_in_dir(os.path.join(base, 'kitty_tests'))
if args.name:
tests = filter_tests_by_name(tests, *args.name)
if not tests._tests:
raise SystemExit('No test named %s found' % args.name)
run_cli(tests, args.verbosity)
def run_cli(suite, verbosity=4):
r = unittest.TextTestRunner
r.resultclass = unittest.TextTestResult
init_env()
runner = r(verbosity=verbosity)
runner.tb_locals = True
result = runner.run(suite)
if not result.wasSuccessful():
raise SystemExit(1)
def main():
run_tests()
if __name__ == '__main__':
main()