博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python3接口自动化-run_all_case
阅读量:4538 次
发布时间:2019-06-08

本文共 3654 字,大约阅读时间需要 12 分钟。

第一步:用discover方法加载所有的测试用例

1.cur_path这个参数是读取当前这个脚本的真实路径,也就是run_main.py的真实路径

2.caseName="case"这个case是存放测试用例的文件夹,如果没有的话,自动创建。如果想运行其它文件夹的用例,就改下caseName这个参数值

3.rule="test*.py"这个是匹配用例脚本名称的规则,默认匹配test开头的所有用例

 
1 import unittest 2 import os 3 import time 4 import HTMLTestRunner 5 import smtplib 6 from email.mime.multipart import MIMEMultipart 7 from email.mime.text import MIMEText 8  9 10 #当前脚本所在文件真实路径11 cur_path = os.path.dirname(os.path.abspath(__file__))12 13 def add_case(caseName="case",rule="test*.py"):14     '''第一步:加载所有的测试用例'''15     case_path = os.path.join(cur_path,caseName)  #用例文件夹16     #定义discover方法的参数17     discover = unittest.defaultTestLoader.discover(case_path,18                                                    pattern=rule,19                                                    top_level_dir=None)20     return discover
 

 

 

第二步:生成HTML报告

1.把上一步加载到用例的参数传入这个函数,测试报告的文件名称默认report文件夹:reportName="report

2.如果没有这个report文件夹也没关系,可以自动创建的

1 def run_case(all_case,reportName="report"): 2     '''第二步:执行所有的用例,并把结果写入HTML测试报告''' 3     now = time.strftime("%Y_%m_%d_%H_%M_%S") 4     report_path = os.path.join(cur_path,reportName)   #报告文件夹 5     #如果不存在就创建 6     if not os.path.exists(report_path): 7         os.mkdir(report_path) 8          9     report_abspath = os.path.join(report_path,now+"result.html")10     fp = open(report_abspath,"wb")11     runner = HTMLTestRunner.HTMLTestRunner(stream=fp,12                                            title="自动化测试报告",13                                            description="用例执行情况")14 15     #调用add_case返回值16     runner.run(all_case)17     fp.close()

第三步:获取最新的测试报告

1.如果第二步生成的测试报告加了时间戳,想找到最新的文件就用第三步

2.如果第二步不加时间戳,只是生成result.html,那这一步其实没卵用,可以忽略

(个人觉得报告用一个名称result.html就行,新的自动覆盖旧的)

1 def get_report_file(report_path):2     '''第三步:获取最新的测试报告'''3     lists = os.listdir(report_path)4     lists.sort(key=lambda fn:os.path.getmtime(os.path.join(report_path,fn)))5     print("最新测试报告: "+lists[-1])6     #找到最新生成的测试报告7     report_file = os.path.join(report_path,lists[-1])8     return report_file

第四步:发送测试报告到邮箱

1.像QQ邮箱这种ssl加密的就走SMTP_SSL,用授权码登录

2.其它邮箱就正常账号密码登录,走SMTP

1 def send_mail(sender,psw,receover,smtpserver,report_file,port): 2     '''第四步:发送最新的测试报告''' 3     with open(report_file,"rb") as f: 4         mail_body = f.read() 5  6     #定义邮件内容 7     msg = MIMEMultipart() 8     body =MIMEText(mail_body,_subtype="html",_charset="utf-8") 9     msg["Subject"] = "自动化测试报告"10     msg["from"] = sender11     msg["to"] = psw12     msg.attach(body)13     14     #添加附件15     att = MIMEText(open(report_file,"rb").read(),"base64","utf-8")16     att["Content-Type"] = "application/octet-stream"17     att["Content-Disposition"] = "attachment; filename = 'report.html'"18     msg.attach(att)19     try:20         smtp = smtplib.SMTP_SSL(smtpserver,port)21     except:22         smtp = smtplib.SMTP()23         smtp.connect(smtpserver,port)24         25     #用户名密码26     smtp.login(sender,psw)27     smtp.sendmail(sender,receover,msg.as_string())28     smtp.quit()29     print("test report email has send out")

最后执行代码

1.这里邮箱的内容读的配置文件

1 if __name__ == "__main__": 2     all_case = add_case()   # 1 加载用例 3     # 生成测试报告路径 4     run_case(all_case)      # 2 执行用例 5     report_path = os.path.join(cur_path,"report")   # 用例文件 6     report_file = get_report_file(report_path)    # 3 获取最新测试报告 7     #邮箱配置 8     from config import readConfig 9     sender = readConfig.sender10     psw = readConfig.psw11     smtp_server = readConfig.smtp_server12     port = readConfig.port13     receiver = readConfig.receiver14     send_mail(sender,psw,receiver,smtp_server,report_file,port) # 4 最后一步发送报告

 

转载于:https://www.cnblogs.com/jayson-0425/p/9809666.html

你可能感兴趣的文章
Android Activity接收Service发送的广播
查看>>
[Leetcode] Spiral Matrix | 把一个2D matrix用螺旋方式打印
查看>>
加速和监控国际网络
查看>>
【Flex】读取本地XML,然后XML数据转成JSON数据
查看>>
字符串循环右移-c语言
查看>>
解决从pl/sql查看oracle的number(19)类型数据为科学计数法的有关问题
查看>>
古训《增广贤文》
查看>>
职场的真相——七句话
查看>>
xcode命令行编译时:codesign命令,抛出“User interaction is not allowed.”异常 的处理...
查看>>
[转载]开机出现A disk read error occurred错误
查看>>
STM32 C++编程 002 GPIO类
查看>>
ELK-Elasticsearch安装
查看>>
day40-socket编程
查看>>
Django后台管理admin笔记
查看>>
JavaScript中的变量
查看>>
iptables基本原理和规则配置
查看>>
ArcGIS JS 学习笔记4 实现地图联动
查看>>
ubuntu 12.04 lts安装golang并设置vim语法高亮
查看>>
使用分层实现业务处理
查看>>
Microsoft Windows平台的NoSQL数据存储引擎
查看>>