-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
129 lines (91 loc) · 3.04 KB
/
run.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import unittest
from lib.HTMLTestReportCN import HTMLTestRunner
from config.config import *
from lib.send_email import send_email
import pickle
import sys
from test.suite.test_suites import *
def discover():
return unittest.defaultTestLoader.discover(test_case_path)
def save_failures(result, file):
suite = unittest.TestSuite()
for case_result in result.failures:
suite.addTest(case_result[0])
with open(file, 'wb') as f:
pickle.dump(suite, f)
def collect():
suite = unittest.TestSuite()
def _collect(tests):
if isinstance(tests, unittest.TestSuite):
if tests.countTestCases() != 0:
for i in tests:
_collect(i)
else:
suite.addTest(tests)
_collect(discover())
return suite
def makesuite_by_testlist(testlist_file):
with open(testlist_file) as f:
testlist = f.readlines()
testlist = [i.strip() for i in testlist if not i.startswith("#")]
suite = unittest.TestSuite()
all_cases = collect()
for case in all_cases:
if case._testMethodName in testlist:
suite.addTest(case)
return suite
def makesuite_by_tag(tag):
suite = unittest.TestSuite()
for case in collect():
if case._testMethodDoc and tag in case._testMethodDoc:
suite.addTest(case)
return suite
def run(suite):
logging.info("================================== 测试开始 ==================================")
with open(report_file, 'wb') as f: # 从配置文件中读取
result = HTMLTestRunner(stream=f, title="Api Test", description="测试描述", tester="卡卡").run(suite)
if result.failures:
save_failures(result, last_fails_file)
if send_email_after_run:
send_email(report_file) # 从配置文件中读取
logging.info("================================== 测试结束 ==================================")
def collect_only():
t0 = time.time()
i = 0
for case in collect():
i += 1
print("{}.{}".format(str(i), case.id()))
print("----------------------------------------------------------------------")
print("Collect {} tests is {:.3f}s".format(str(i),time.time()-t0))
def run_all():
run(discover())
def run_suite(suite_name):
suite = get_suite(suite_name)
if isinstance(suite, unittest.TestSuite):
run(suite)
else:
print("TestSuite不存在")
def run_by_testlist():
run(makesuite_by_testlist(testlist_file))
def run_by_tag(tag):
run(makesuite_by_tag(tag))
def rerun_fails():
sys.path.append(test_case_path)
with open(last_fails_file, 'rb') as f:
suite = pickle.load(f)
run(suite)
def main():
if options.collect_only:
collect_only()
elif options.rerun_fails:
rerun_fails()
elif options.testlist:
run(makesuite_by_testlist(testlist_file))
elif options.testsuite:
run_suite(options.testsuite)
elif options.tag:
run(makesuite_by_tag(options.tag))
else:
run_all()
if __name__ == '__main__':
main()