-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·88 lines (63 loc) · 2.73 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
#!/usr/bin/env python
import sys
import logging
import unittest
import argparse
import fnmatch
logging.getLogger().setLevel(logging.WARN)
def filter_suite(suite, test_pattern):
def match_tests(suite_or_tests, pattern):
matched = []
for suite_or_test in suite_or_tests:
if hasattr(suite_or_test, "_tests"):
matched.extend(match_tests(suite_or_test._tests, pattern))
else:
if fnmatch.fnmatch(suite_or_test._testMethodName, pattern):
matched.append(suite_or_test)
return matched
filtered_suite = unittest.TestSuite()
filtered_suite.addTests(match_tests(suite, test_pattern))
return filtered_suite
def test(pattern, test_pattern):
print("======================================================================")
print("Running tests")
print("======================================================================")
suite = unittest.TestLoader().discover('backend/test', pattern=pattern)
if test_pattern:
suite = filter_suite(suite, test_pattern)
result = unittest.TextTestRunner(stream=sys.stdout).run(suite)
return len(result.failures) + len(result.errors)
def test_coverage(pattern, test_pattern, coverage):
if coverage:
try:
from coverage import coverage
cov = coverage(
include=["backend/*"],
omit=["backend/test/*"])
cov.start()
except ImportError:
print("coverage not installed, can't collect coverage data")
coverage = False
failures = test(pattern, test_pattern)
if coverage:
cov.stop()
print("======================================================================")
print("Coverage report")
print("======================================================================")
if hasattr(cov, 'set_option'):
cov.set_option("report:show_missing", True)
cov.report()
return failures
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--coverage", dest="coverage", action="store_true", default=False,
help="Collect and output coverage data")
parser.add_argument("-p", "--pattern", dest="pattern", default="test_*.py",
help="Run test files matching given pattern")
parser.add_argument("-t", "--test-pattern", dest="test_pattern",
help="Run test methods matching given pattern")
args = parser.parse_args()
failures = test_coverage(args.pattern, args.test_pattern, args.coverage)
print("======================================================================")
print("Done!")
exit(failures != 0)