import os,sys
BASE_PATH =os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, BASE_PATH)
import unittest,time
import xmlrunner # 通过pip install xmlrunner安装
from conf.settings import CASE_PATH, HTML_PATH, XML_PATH, EMAIL_INFO
from lib.tools import get_new_report, remove_report
from lib.sendmail_new import SendMail
from BeautifulReport import BeautifulReport # 把BeautifulReport.zip压缩文件解压到C:\Miniconda3\Lib\site-packages目录下
user = EMAIL_INFO.get('user') # 从配置文件中获取邮件用户名
password = EMAIL_INFO.get('password') # 从配置文件中获取邮箱授权码
host = EMAIL_INFO.get('host') # 从配置文件中获取邮件服务器
to = EMAIL_INFO.get('to') # 从配置文件中获取邮件接收者
cc = EMAIL_INFO.get('cc') # 从配置文件中获取抄送邮箱
subject = EMAIL_INFO.get('subject') # 从配置文件中获取邮件标题
contents = EMAIL_INFO.get('contents') # 从配置文件中获取邮件内容
attachments = EMAIL_INFO.get('attachments') # 从配置文件中获取附件
def html_run():
suite = unittest.TestSuite()
all_cases = unittest.defaultTestLoader.discover(CASE_PATH, '*.py')
for case in all_cases:
suite.addTest(case)
# file_name = os.path.join(HTML_PATH,'%s_report.html' % time.strftime('%Y%m%d%H%M%S'))
# fw = open(file_name,'wb')
# runner = HTMLTestRunner.HTMLTestRunner(stream = fw,title = '国网电商')
# runner.run(suite)
run = BeautifulReport(suite)
run.report(filename="%s_report.html" % time.strftime('%Y%m%d%H%M%S'), description='金融科技港接口自动化测试报告',
log_path=HTML_PATH)
如果想执行某些接口,可以用下面的代码:
def html_run():
lis = ('a01*.py', 'a03*.py', 'a05*.py') # 换成list也ok
suite = unittest.TestSuite()
for i in lis:
all_cases = unittest.defaultTestLoader.discover(CASE_PATH, i)
for case in all_cases:
suite.addTest(case)
如果想执行某些接口且函数里有参数,可以用下面的代码:
def xml_run():
suite = unittest.TestSuite()
all_cases = unittest.defaultTestLoader.discover(CASE_PATH, '*.py')
for case in all_cases:
suite.addTest(case)
runner = xmlrunner.XMLTestRunner(output=XML_PATH)
runner.run(suite)
if __name__ == '__main__':
html_run()
filename = get_new_report() # 生成最新的报告作为附件
send = SendMail(user, password, host, to, cc, subject, contents, filename)
send.send_mail()
remove_report()
# xml_run()