Commit f1f467ef3c5ac9dcb72de6a31b0684ca4e0c5acb

Authored by liujiqiang
1 parent 745ca6b7

更新

Showing 1 changed file with 84 additions and 0 deletions
main.py 0 → 100644
  1 +# -*- coding: utf-8 -*-
  2 +
  3 +# @Time : 2021/7/16 15:11
  4 +# @Author : Ljq
  5 +# @File : main.py
  6 +# @Software: PyCharm
  7 +
  8 +"""
  9 +
  10 +"""
  11 +
  12 +import os, time
  13 +import sys
  14 +import unittest
  15 +import re
  16 +import HTMLTestRunner_cn
  17 +import commons.ConfigDB as db
  18 +from discover import DiscoveringTestLoader
  19 +from commons import SendEmail as em
  20 +from commons import common as com
  21 +from commons.MySession import my
  22 +
  23 +
  24 +def Create_Testcase_suite():
  25 + '''创建测试套件'''
  26 + testunit = unittest.TestSuite()
  27 + test_Loader = DiscoveringTestLoader()
  28 + discover = test_Loader.discover("./testCase", pattern='test_*.py', top_level_dir=None)
  29 + print(discover)
  30 + for test_suite in discover:
  31 + testunit.addTests(test_suite)
  32 + # print(testunit)
  33 + return testunit
  34 +
  35 +
  36 +def Run_Testcase(testsuit):
  37 + '''运行测试用例并生成报告'''
  38 + now = time.strftime("%Y-%m-%d %H_%M_%S", time.localtime())
  39 + path = os.path.dirname(os.path.abspath(sys.argv[0]))
  40 + report_file = os.path.abspath(os.path.join(path))+ "/report/" + now + "_result.html"
  41 + # 创建报告文件
  42 + fp = open(report_file, 'wb')
  43 +
  44 + runner = HTMLTestRunner_cn.HTMLTestRunner(
  45 + stream=fp,
  46 + title=u'重构项目接口测试报告',
  47 + description=u'用例简要执行情况如下:(注:报告详细信息需要下载report.html并用浏览器打开)',
  48 + verbosity=2)
  49 + # 执行用例
  50 + runner.run(testsuit)
  51 + # 关闭文件
  52 + fp.close()
  53 + return report_file
  54 +
  55 +
  56 +def Send_email(filename):
  57 + '''判断邮件发送逻辑'''
  58 + l = eval(com.get_global_config("global_data", "email", "list").lower())
  59 + if type(l) != type([]):
  60 + raise Exception("error,pls input list type send-email address")
  61 + elif len(l) == 0:
  62 + print("\n list of send-email is null,won't send email!")
  63 + elif len(l) != 0:
  64 + for i in l:
  65 + print("\n check send-email format : {}".format(i))
  66 + if re.match(r'^[0-9a-zA-Z_]{1,19}@[0-9a-zA-Z]{1,13}\.[com,cn,net]{1,3}$', i):
  67 + pass
  68 + else:
  69 + raise Exception("error,pls check your send-email format")
  70 + # 发送邮件
  71 + em.send_email(filename, l)
  72 + else:
  73 + print("\n Haven't sent the email,pls check send-email address!")
  74 +
  75 +
  76 +if __name__ == "__main__":
  77 + # 检测数据库
  78 + db.mysql_conn_test()
  79 + # 检测登录接口
  80 + my.check_login("user01")
  81 + # 创建测试套,运行测试用例,生成报告
  82 + report = Run_Testcase(Create_Testcase_suite())
  83 + # 发送邮件
  84 + Send_email(report)
0 \ No newline at end of file 85 \ No newline at end of file