forked from jaychsu/algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_python.py
executable file
·72 lines (54 loc) · 1.6 KB
/
test_python.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import unittest
import doctest
def log(func):
def wrapper(dirname, *args, **kw):
print('[{}]: Starting to test...'.format(dirname))
res = func(dirname, *args, **kw)
print('[{}]: Finished to test.\n\n'.format(dirname))
return res
return wrapper
@log
def run_unittest(dirname):
ROOT_DIR = os.path.join(
os.path.dirname(__file__),
'../{dirname}'.format(dirname=dirname)
)
suite = unittest.TestSuite()
for path, _, files in os.walk(ROOT_DIR):
if ('pycache' in path or
'python' not in path or
'__init__.py' not in files):
continue
tests = unittest.defaultTestLoader.discover(
path,
pattern='*__test.py',
top_level_dir=ROOT_DIR
)
suite.addTests(tests)
unittest.TextTestRunner(verbosity=2).run(suite)
@log
def run_doctest(dirname):
ROOT_DIR = os.path.join(
os.path.dirname(__file__),
'../{dirname}'.format(dirname=dirname)
)
suite = unittest.TestSuite()
sys.path.append(ROOT_DIR)
for path, _, files in os.walk(ROOT_DIR):
if 'pycache' in path:
continue
for file in files:
if (file.startswith('_') or
not file.endswith('.py')):
continue
suite.addTest(doctest.DocTestSuite(file[:-3]))
sys.path.pop()
unittest.TextTestRunner(verbosity=2).run(suite)
if __name__ == '__main__':
run_unittest('topic')
run_doctest('leetcode')
run_doctest('other')