-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.py
58 lines (45 loc) · 1.86 KB
/
tests.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
"""Script to manage unit testing"""
from __future__ import print_function
import glob
import logging
import os
import sys
from unittest import TestLoader, TextTestRunner, TestSuite
ROOT_DIR = os.path.join(os.path.dirname(__file__))
PACKAGES = ["parsers", "loaders", "utils"]
class UnittestFramework(object):
"""Framework to run unit testing"""
def run(self, buffer=False, pattern="test*.py", verbosity=2):
"""Main routine for running the test cases"""
tests = self.find_tests(ROOT_DIR, pattern=pattern)
if int(tests.countTestCases()) <= 0:
msg = 'Could not find any tests to run in directory: {0}'.format(ROOT_DIR) + os.linesep
sys.stderr.write(msg)
sys.exit(1)
logging.disable(logging.CRITICAL)
result = TextTestRunner(verbosity=verbosity, buffer=buffer).run(tests)
logging.disable(logging.NOTSET)
if result.wasSuccessful():
exit(0)
else:
exit(1)
def find_tests(self, directory, pattern="test*.py"):
"""Load a unittest test suite"""
search_pattern = os.path.join(directory, "*")
cases = [os.path.basename(folder) for folder in glob.iglob(search_pattern)
if os.path.isdir(folder) and os.path.basename(folder) in PACKAGES]
return self._load_suite(cases, pattern, directory)
def _load_suite(self, cases, pattern, directory):
suite = TestSuite()
for case in cases:
path = os.path.join(directory, case, "tests")
try:
_suite = TestLoader().discover(path, pattern=pattern, top_level_dir=directory)
suite.addTests(_suite)
del _suite
except ImportError:
print("*** not a package: {0} ***".format(path))
return suite
if __name__ == "__main__":
test = UnittestFramework()
test.run()