留下了感动的泪水*0*~~~终于看到我想要的结果了,附个土先><
接下来咱开始详细的介绍下它是怎么出来的吧
先添加下HTMLTestRunner文件吧 地址:http://tungwaiyip.info/software/HTMLTestRunner.html,下载好后把它放在你安装python的lib目录下,不然可能无效哦
重要的架构
- 先自己创建个package,命名自定义哈,我这里随意叫它demo
- common文件:放一些公共的方法,如:读取excel文件方法,logger日志等。
- cfg.ini文件:这里是配置文件,如邮箱的一些参数:收件人,发件人,密码等
- readConfig文件:用于读取配置文件
- report文件:查看html测试报告
- testcase文件:这个是key,放所有测试用例的地方,也可以封装一些接口,这个我暂时没有用到~
- 需要查看日志的话也可以添加一个log文件
cfg.ini的形成
# -* - coding: UTF-8 -* -
[email]
smtp_server = smtp.qq.com
port = 25
sender = xxx@.com #设置你想要收到文件的邮箱
pwd = xxxx #你的授权码
receiver = xxx@.com
readConfig的形成
# -* - coding: UTF-8 -* -
import os
import configparser
cur_path = os.path.dirname(os.path.realpath(__file__)) #获取当前文件所在路径
configPath = os.path.join(cur_path, "cfg.ini") #拼接出文件路径
conf = configparser.ConfigParser()
conf.read(configPath) #读取配置文件cfg.ini
# 获取指定的section, 指定的option的值
smtp_server = conf.get("email", "smtp_server")
sender = conf.get("email", "sender")
pwd = conf.get("email", "pwd")
receiver = conf.get("email", "receiver")
port = conf.get("email", "port")
testcase,这个我就不多说了哦,毕竟每个项目的接口都不一样,如果对代码格式有疑问怕拿捏不准的童鞋,可以使用postman request,它会贴心的帮你搞定,需要戳
接口脚本模式差不多下图这样子
*注意每个脚本文件命名需要以test开头哦
runtest.py,五颗星级别的文件,请注意看哦
# coding=utf-8
import unittest
import os
import HTMLTestRunner
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from unittest import TestCase
# 父目录:os.path.dirname(所在目录:os.path.dirname(绝对路径:os.path.realpath(__file__)))
cur_path = os.path.dirname(os.path.realpath(__file__)) # 当前文件存在的路径
case_path = os.path.join(os.path.dirname(os.path.abspath(__file__))) # 测试case目录
report_path = os.path.join(cur_path, 'report') # 测试报告存放目录
def all_case():
'''第一步:加载所有的测试用例'''
# 批量加载discover方法里面有三个参数:-case_dir:这个是待执行用例的目录-pattern:这个是匹配脚本名称的规则,test*.py意思是匹配test开头的所有脚本-top_level_dir:这个是顶层目录的名称,一般默认等于None就行了。
# discover加载到的用例是一个list集合,需要重新写入到一个list对象testcase里,这样就可以用unittest里面的TextTestRunner这里类的run方法去执行。
discover = unittest.defaultTestLoader.discover(case_path,
pattern="test*.py",
top_level_dir=None)
# print(discover)
return discover
# def run_case():
def run_case(all_case):
'''第二步:执行所有的用例, 并把结果写入HTML测试报告'''
# 测试报告文件路径
report_abspath = os.path.join(report_path, "result.html")
fp = open(report_abspath, "wb")
# 批量执行测试用例三个参数: --stream:测试报告写入文件的存储区域 --title:测试报告的主题 --description:测试报告的描述
runner = HTMLTestRunner.HTMLTestRunner(stream=fp,
title=u'请查收:',
description=u'用例执行情况:')
# 调用add_case函数返回值
runner.run(all_case)
fp.close()
def send_mail(sender, pwd, receiver, smtpserver, port):
'''第三步:发送最新的测试报告内容'''
file_path = report_path + r"\result.html"
with open(file_path, "rb") as f:
mail_body = f.read()
# 定义邮件内容
msg = MIMEMultipart()
msg['Subject'] = u"请查收" # 主题
msg["from"] = sender # 发件人
msg["to"] = receiver # 收件人
# 正文
body = MIMEText(mail_body, _subtype='html', _charset='utf-8')
msg.attach(body)
# 添加附件
att = MIMEText(mail_body, "base64", "utf-8")
att["Content-Type"] = "application/octet-stream"
att["Content-Disposition"] = 'attachment; filename= "report.html"'
msg.attach(att)
mail = smtplib.SMTP()
mail.connect("smtp.qq.com") # 连接QQ邮箱
mail.login("你的邮箱", "邮箱授权码") # 帐号和授权码
mail.sendmail("发送的邮箱", ["接收的邮箱"], msg.as_string()) # 发送帐号,接受账号和邮件信息
mail.quit() # 关闭
print('test report email has send out !')
if __name__ == "__main__":
all_case = all_case() # 1加载用例
# 生成测试报告的路径
run_case(all_case) # 2执行用例
# 邮箱配置
from config import readConfig
sender = readConfig.sender
pwd = readConfig.pwd
smtp_server = readConfig.smtp_server
port = readConfig.port
receiver = readConfig.receiver
send_mail(sender, pwd, receiver, smtp_server, port) # 发送报告
从加载测试用例到运行到生成报告到发送邮箱都有啦
以上这些都写好了之后,直接run上面这个文件
你会看到report多了份result.html,同时邮箱也会收到测试报告,打开就能看结果啦,赞~
*期间运行的时候也遇到过一些问题,在这里也分享一下 如:ModuleNotFoundError: No module name ‘StringIO’,百度了下,“import StringIO”不兼容python3.8版本,改成“from io import StringIO ”就行了。
总结:去探索,勿放弃,柳暗花明又一村,在学习中获取乐趣,在生活中发现精彩(旁白:这个话着实鸡汤,不要太关注hhh,本人其实也很懒得^^,等着我出炉加上jenkins的完整流程叭)