接口测试3
- 2019 年 10 月 8 日
- 筆記
上篇讲解到了一次性运行多个测试用例和运行结果的情况,这边继续说下测试报告的内容输出和可视化显示以及邮件抄送等
一、增加测试报告输出
1、首先在代码目录下新建一个文件夹test_report用来保存测试结果

2、导入测试报告库文件HTMLTestRunner_PY3(这个文件在网上可以下载后[https://blog.csdn.net/cjh365047871/article/details/80181530]直接存放在python的库文件目录下,然后直接导入即可使用—这里我存放的目录是D:pythonpython3.6.1Lib)

3、定义测试用例和测试报告存放路径、读取测试用例方法和测试报告格式
#!/usr/bin/env python # -*- coding: utf-8 -*- # @File Name: interface_python.py # @Time : 2019/8/17 # @Author : zhong # @Blog : https://www.jianshu.com/search?q=keitwo&page=1&type=note # @QQ交流 : 3227456102 import unittest,time import HTMLTestRunner_PY3 if __name__=='__main__': # 定义测试用例的目录为当前目录 testcase_dir = 'E:/study/InterfaceTest/testcase/' testreport_dir = 'E:/study/InterfaceTest/test_report' # 加载测试用例 discover = unittest.defaultTestLoader.discover(testcase_dir, pattern='*.py') # 定义测试报告的文件格式 now = time.strftime("%y-%m-%d %H_%M_%S") # 对时间格式化 report_name = testreport_dir + '/' + now + '_test_report.html' # 报告的名称规则 # 运行测试用例,并生成测试报告 with open(report_name, 'wb') as f: runner = HTMLTestRunner_PY3.HTMLTestRunner(stream=f, title="Interface API Test Report", description="Test Report") runner.run(discover)
运行结果

我们可以看到在test_report文件夹下有个以时间命名的测试报告,双击打开即可查看

二、增加抄送邮件功能
上面输出了测试报告。那么,如何把测试报告发送到项目的相关人员呢?这里我们引入以邮件的方式抄送报告给相关人员
1、创建common公共文件,导入send_email.py文件

2、send_email.py文件:
#!/usr/bin/env python # -*- coding: utf-8 -*- # @File Name: interface_python.py # @Time : 2019/8/17 # @Author : zhong # @Blog : https://www.jianshu.com/search?q=keitwo&page=1&type=note # @QQ交流 : 3227456102 import os import smtplib from email.header import Header from email.mime.multipart import MIMEMultipart from email.mime.image import MIMEImage from email.mime.text import MIMEText # 定义发件人和收件人 mail_from = "xxxxx" mail_password = "xxxxx" mail_to = ["[email protected]"] def email_content(): # 邮件主题 message = MIMEMultipart() message['From'] = Header(u"姓名" + "<" + mail_from + ">", 'utf-8') message['To'] = ";".join(mail_to) message['Subject'] = Header(u"接口自动化测试报告", 'utf-8') # 邮件内容1 emailContent1 = MIMEText('这是测试报告,详情请查看附件', 'plain', 'utf-8') message.attach(emailContent1) # 获取最新的html报告 result_dir = "D:/work/Selenium-Demo/InterfaceTest/test_report" #result_dir = os.path.join(os.getcwd(), 'report/') lists = os.listdir(result_dir) # lists.sort(key=lambda fn: os.path.getmtime(result_dir + "\" + fn) if not # os.path.isdir(result_dir + "\" + fn) else 0) print("最新的文件为:" + lists[-1]) file = os.path.join(result_dir, lists[-1]) # # 构造图片链接 # sendimagefile = open(r'D:pythontesttestimage.png', 'rb').read() # image = MIMEImage(sendimagefile) # image.add_header('Content-ID', '<image1>') # image["Content-Disposition"] = 'attachment; filename="testimage.png"' # message.attach(image) # 邮件内容2:report.html中内容 emailContent2 = MIMEText(open(file, 'rb').read(), 'html', 'utf-8') message.attach(emailContent2) # 添加邮件附件 enclosure1 = MIMEText(open(file, 'rb').read(), 'html', 'utf-8') enclosure1["Content-Type"] = 'application/octet-stream' enclosure1["Content-Disposition"] = 'attachment; filename=report.html' message.attach(enclosure1) return message def sendreport(): # 发送邮件 try: smtp = smtplib.SMTP_SSL("smtp.exmail.qq.com", 465) smtp.login(mail_from, mail_password) message = email_content() smtp.sendmail(mail_from, mail_to, message.as_string()) smtp.quit() print("email has send out!") except smtplib.SMTPException: print("email send error!")
3、导入发送邮件模块

4、运行结果
