main.py
2.5 KB
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
# -*- coding: utf-8 -*-
# @Time : 2021/7/16 15:11
# @Author : Ljq
# @File : main.py
# @Software: PyCharm
"""
"""
import os, time
import sys
import unittest
import re
import HTMLTestRunner_cn
import commons.ConfigDB as db
from discover import DiscoveringTestLoader
from commons import SendEmail as em
from commons import common as com
from commons.MySession import my
from commons.scripts import delReport
def Create_Testcase_suite():
'''创建测试套件'''
testunit = unittest.TestSuite()
test_Loader = DiscoveringTestLoader()
discover = test_Loader.discover("./", pattern='test_*.py', top_level_dir=None)
print(discover)
for test_suite in discover:
testunit.addTests(test_suite)
# print(testunit)
return testunit
def Run_Testcase(testsuit):
'''运行测试用例并生成报告'''
now = time.strftime("%Y-%m-%d %H_%M_%S", time.localtime())
path = os.path.dirname(os.path.abspath(sys.argv[0]))
report_file = os.path.abspath(os.path.join(path, "../"))+ "/report/" + now + "_result.html"
# 创建报告文件
fp = open(report_file, 'wb')
runner = HTMLTestRunner_cn.HTMLTestRunner(
stream=fp,
title=u'重构项目接口测试报告',
description=u'用例简要执行情况如下:(注:报告详细信息需要下载report.html并用浏览器打开)',
verbosity=2)
# 执行用例
runner.run(testsuit)
# 关闭文件
fp.close()
delReport.delReport(path)
return report_file
def Send_email(filename):
'''判断邮件发送逻辑'''
l = eval(com.get_global_config("global_data", "email", "list").lower())
if type(l) != type([]):
raise Exception("error,pls input list type send-email address")
elif len(l) == 0:
print("\n list of send-email is null,won't send email!")
elif len(l) != 0:
for i in l:
print("\n check send-email format : {}".format(i))
if re.match(r'^[0-9a-zA-Z_]{1,19}@[0-9a-zA-Z]{1,13}\.[com,cn,net]{1,3}$', i):
pass
else:
raise Exception("error,pls check your send-email format")
# 发送邮件
em.send_email(filename, l)
else:
print("\n Haven't sent the email,pls check send-email address!")
if __name__ == "__main__":
# 检测数据库
db.mysql_conn_test()
# 检测登录接口
my.check_login("user01")
# 创建测试套,运行测试用例,生成报告
report = Run_Testcase(Create_Testcase_suite())
# 发送邮件
Send_email(report)