-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathtest_python.py
executable file
·78 lines (60 loc) · 1.7 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
73
74
75
76
77
78
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import unittest
import doctest
sys.path.append(os.path.join(
os.path.dirname(__file__),
'../topic'
))
from _test.python.test_base import TaskTimer
def get_unittests(dirname):
ROOT_DIR = os.path.join(
os.path.dirname(__file__),
'../{dirname}'.format(dirname=dirname)
)
tests = []
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.append(unittest.defaultTestLoader.discover(
path,
pattern='*__test.py',
top_level_dir=ROOT_DIR
))
return tests
def get_doctests(dirname):
ROOT_DIR = os.path.join(
os.path.dirname(__file__),
'../{dirname}'.format(dirname=dirname)
)
tests = []
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
timer = TaskTimer()
tests.append(doctest.DocTestSuite(
file[:-3],
setUp=lambda globs: timer.reset_time(),
tearDown=lambda globs: timer.print_duration()
))
sys.path.pop()
return tests
if __name__ == '__main__':
suite = unittest.TestSuite()
for tests in (
get_unittests('topic'),
get_doctests('leetcode'),
get_doctests('pramp'),
get_doctests('other'),
):
suite.addTests(tests)
unittest.TextTestRunner(verbosity=2).run(suite)