通过之前的学习,了解到了如何利用excel进行读取数据,如何采用DDT数据驱动,如何使用unittest。下面是将之前所学进行结合,并发送邮件-->leader,废话不多说,上代码:

email_report.py

 # -*- coding: utf-8 -*-
# @Time : 2019/3/29/029 20:55
# @Author : bing
# @File : email_report.py
# @Software: PyCharm import smtplib
import time
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
from email.header import Header
class email_Smtp:
'''smtp-email sendemail'''
def __init__(self,email_User,email_Pwd,to_Email_user):
self.email_User = email_User
self.to_Email_user = to_Email_user
self.server = smtplib.SMTP_SSL("smtp.qq.com", 465)
self.server.login(email_User, email_Pwd) def send_email_msg(self,file_path):
test_Time = str(time.strftime("%Y_%m_%d", time.localtime()))
test_file_name = test_Time + "_testCase_request_Report.html"
msg_total = MIMEMultipart() # multipart类型主要有三种子类型:mixed、alternative、related 默认mixed
msg_total['From'] = Header("Test-Mine", 'utf-8')
msg_total['To'] = Header("leader", 'utf-8')
msg_total['Subject'] = '测试报告'
# 正文模块
msg_raw = open(file_path,"r",encoding='utf-8').read()
msg = MIMEText(msg_raw, 'html')
msg_total.attach(msg)
# 附件模块
mfile = MIMEApplication(open(file_path,"rb").read())
# 添加附件的头信息
mfile.add_header('Content-Disposition', 'attachment', filename=test_file_name)
# 附件摸快添加到总的里面
msg_total.attach(mfile)
self.server.sendmail(self.email_User , self.to_Email_user , msg_total.as_string())
print("邮件发送成功!请查收")

excel_file.py

 from openpyxl import load_workbook
import json
class RwExcelFile: def read_Excel_Case(self,file_path):
"""获取excel的登录信息,保存到列表里面"""
excel_File = load_workbook(file_path)
sheet = excel_File.worksheets[0] login_datas = []
for row in range(2, sheet.max_row + 1):
user = {
"method": sheet.cell(row, 1).value,
"data": json.loads(sheet.cell(row, 2).value),
"exp_data": sheet.cell(row, 3).value,
"case_id": sheet.cell(row, 6).value,
}
login_datas.append(user)
excel_File.close()
return login_datas def read_Excel(self,file_path,sheet_name,start_row,start_cloumn):
'''
读取excel中所有数据并以列表形式返回
:param file_path:
:return:
'''
excel_File = load_workbook(file_path)
sheet = excel_File[sheet_name]
list_column = []
list_row = []
for i in range(start_row,sheet.max_row+1):
for j in range(start_cloumn,sheet.max_column+1):
list_column.append(sheet.cell(i,j).value)
list_row.append(list_column)
list_column = []
excel_File.close()
return list_row def write_Excel(self,file_path,sheetname,row,cloumn,T_value):
'''
向excel指定位置写入值
:param file_path: 文件地址
:param sheetname: sheet名
:param row: 行
:param cloumn:列
:param T_value: 值
:return: 无返回
'''
excel_File = load_workbook(file_path)
excel_File[sheetname].cell(row,cloumn,T_value)
excel_File.save(file_path)
excel_File.close()

request_login.py

import requests
class Interface_Request: def __init__(self,mobilephone,pwd):
'''login参数初始化'''
self.url = '****'
self.dict = {'mobilephone':mobilephone,'pwd':pwd} def http_request(self,way):
'''login接口请求'''
if way=='get':
return requests.get(self.url,self.dict).text
elif way=='post':
return requests.post(self.url,self.dict).text

testCase_request.py

import unittest
from excel_file import *
from request_login import Interface_Request
from ddt import ddt,unpack,data @ddt#@ddt装饰测试类 unittest.TestCase的子类
class TestRequest(unittest.TestCase,RwExcelFile):
@data(*RwExcelFile().read_Excel_Case('request测试数据.xlsx'))
@unpack
def test_001(self,method,data,exp_data,case_id):
print('Test_login,验证:',exp_data)
expected=exp_data#期望值
res=Interface_Request(data["mobilephone"],data["pwd"]).http_request(method) #实际值
#断言
try:
self.assertEqual(expected,json.loads(res)['msg'])
row = int(case_id) + 1
RwExcelFile().write_Excel('request测试数据.xlsx', "Sheet1", row, 5, 'pass')
except AssertionError as e:
# logging
# 测试不通过的结果: “failed” 写到 Excel
row = int(case_id) + 1
RwExcelFile().write_Excel('request测试数据.xlsx',"Sheet1",row,5,'failed')
raise

testSuite_Run.py

 import unittest
import HTMLTestRunnerNew
import time
from email_report import email_Smtp
from Test import testCase_request loader=unittest.TestLoader()#用例的加载器
suite=unittest.TestSuite()#创建了一个对象
suite.addTest(loader.loadTestsFromModule(testCase_request))#注意引入的是模块名不是类名,这里犯过错 #执行并生成html测试报告--HTMLTestRunnerNew
test_Time = str(time.strftime("%Y_%m_%d", time.localtime()))
test_file_name = test_Time+"_testCase_request_Report.html"
with open(test_file_name,'wb+') as file:
runner=HTMLTestRunnerNew.HTMLTestRunner(
stream=file,
verbosity=2,
title='测试报告',
description='request测试报告',
tester='Test_Mine'
)#创建一个对象来执行用例
runner.run(suite)#这一行没有任何改变 email_Smtp("****@qq.com", "密钥" , "*****@qq.com").send_email_msg(test_file_name)

邮件截图:

python3 通过邮件发送测试报告的更多相关文章

  1. Java+Selenium3框架设计篇5-如何实现邮件发送测试报告

    https://blog.csdn.net/u011541946/article/details/77278837 本篇继续回答网友的问题,这个主题是如何通过邮件发送测试报告.通过邮件发送测试报告,这 ...

  2. Jenkins配置邮件发送测试报告

    前言 在之前的文章(Jenkins自动执行python脚本输出测试报告)中,我们已成功实现利用Jenkins自动执行python脚本,输出并可直接在界面上查看测试报告,这里我们还差最后一步,我们需要将 ...

  3. Python添加邮件附件并通过邮件发送测试报告

    import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText c ...

  4. python3实现邮件发送程序

    刚开始的想法很简单,由于有上千封的邮件需要发出去,并且需要一条一条发送,不能转发或群发,每条邮件要署对方的姓名,并加上几个相同的符件,考虑到手工操作繁琐无趣,所以想到用程序实现,python好像非常胜 ...

  5. Jenkins发送测试报告

    邮件全局配置 邮件插件:Email Extension Plugin 功能:发送邮件 邮件全局配置:jenkins--系统管理--系统配置:截图: 配置说明: 系统管理员邮件地址:必须配置,配置后邮件 ...

  6. python3 邮件方式发送测试报告

    以邮件方式发送测试报告 import smtplib from email.mime.text import MIMEText class SendEmail: """邮 ...

  7. 【Python + Selenium3】自动化测试之DDT数据驱动并生成测试报告以及用yagmail邮件发送文件

    我的文件路径 一.DDT代码: import unittest from time import sleep from selenium import webdriver from ddt impor ...

  8. python3 邮件发送

    这是搜罗网络上的文章总结的一份文档, 参考: https://www.jb51.net/article/140604.htm https://www.jb51.net/article/140604.h ...

  9. Python Selenium unittest+HTMLTestRunner实现 自动化测试及发送测试报告邮件

    1.UI测试框架搭建-目录结构 2. 文件介绍 2.1.baseinfo->__init__.py 配置文件定义基础参数 #-*-coding:utf-8-*- #测试用例配置参数 base_u ...

随机推荐

  1. D. Vanya and Treasure Codeforces Round #355 (Div. 2)

    http://codeforces.com/contest/677/problem/D 建颗新树,节点元素包含r.c.dis,第i层包含拥有编号为i的钥匙的所有节点.用i-1层更新i层,逐层更新到底层 ...

  2. Groovy闭包详解

    Groovy闭包是一种可执行代码块的方法,闭包也是对象,可以向方法一样传递参数,因为闭包也是对象,因此可以在需要的时候执行,像方法一样闭包可以传递一个或多个参数.闭包最常见的用途就是处理集合,可以遍历 ...

  3. swift 4 生成随机数的内置方法汇总

    第一种是drand48(),不接收参数, 返回的类型是Double. 就返回 0到1之间的Double类型的随机数.举个例子: //每次点击button,button 的颜色会随机变换. class ...

  4. ultraEdit软件比较两个文件内容的不同处

    1.软件名称为:UltraEdit ,安装并打开软件; 软件图标: 打开软件如图所示: 2.点击导航图标,蓝色上面有Uc图标,该图标名称为“比较文件” 如图位置: 3.弹出框,根据文件路径选择好比较的 ...

  5. java多线程基础篇第一篇

    1.在开始多线程之前,我们先来聊聊计算机的缓存 计算机处理一个程序需要cpu处理器与存储设备的交互.但是在计算机发展的过程中,cpu处理器的处理速度不断提高,而存储设备的读写速度却没有得到与cpu同样 ...

  6. VS 测试printf 多参数 输出 i++ 和++i 结果

    代码如截图: 总结: printf 多参数中有运算时 是从右到左执行的: i++ 和 ++i 优先级是大于 赋值 =运算的: i++ 和++i 是平级的: i++ 先用在算,++i 先算在用: 从右往 ...

  7. JAVA使用HttpClient时报错:Algorithm constraints check failed on signature algorithm: MD5withRSA

    今天使用httpClient.executeMethod时抛出异常:java.security.cert.CertPathValidatorException: Algorithm constrain ...

  8. JavaScript事件循环机制

    事件循环 事件循环不仅仅包含事件队列,而是具有至少两个队列,除了事件,还要保持浏览器执行的其他操作.这些操作被称为任务,并且分为两类:宏任务(或通常称为任务)和微任务. 单次循环迭代中,最多处理一个宏 ...

  9. C++中public/protect/private三种访问权限控制

    一.成员访问权限控制 1.public (1)public成员变量可以被成员函数访问  [访问性] (2)public成员可以被实体对象访问  [访问性] (3)public成员可以成为子类成员  [ ...

  10. technologies

    RPC Apache Thrift, Thrift is an interface definition language and binary communication protocol, lik ...