zoukankan      html  css  js  c++  java
  • python3接口自动化-run_all_case

    第一步:用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"] = sender
    11     msg["to"] = psw
    12     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.sender
    10     psw = readConfig.psw
    11     smtp_server = readConfig.smtp_server
    12     port = readConfig.port
    13     receiver = readConfig.receiver
    14     send_mail(sender,psw,receiver,smtp_server,report_file,port) # 4 最后一步发送报告
  • 相关阅读:
    友盟统计 新的集成方法
    Error:Unable to tunnel through proxy. Proxy returns "HTTP/1.1 400 Bad Request"解决方法
    关于如何获取Google 官方 NavgationView中的控件的方法
    使用线程池管理线程!
    文件上传MultipartBody使用方法
    Ddos 分布式拒绝服务 (报告)
    linux+
    模型事件
    广东惠州游
    PhoneGap & Cordova 安装白皮书
  • 原文地址:https://www.cnblogs.com/jayson-0425/p/9809666.html
Copyright © 2011-2022 走看看