通过之前的学习,了解到了如何利用excel进行读取数据,如何采用DDT数据驱动,如何使用unittest。下面是将之前所学进行结合,并发送邮件-->leader,废话不多说,上代码:
email_report.py
1 # -*- coding: utf-8 -*- 2 # @Time : 2019/3/29/029 20:55 3 # @Author : bing 4 # @File : email_report.py 5 # @Software: PyCharm 6 7 import smtplib 8 import time 9 from email.mime.text import MIMEText 10 from email.mime.multipart import MIMEMultipart 11 from email.mime.application import MIMEApplication 12 from email.header import Header 13 class email_Smtp: 14 '''smtp-email sendemail''' 15 def __init__(self,email_User,email_Pwd,to_Email_user): 16 self.email_User = email_User 17 self.to_Email_user = to_Email_user 18 self.server = smtplib.SMTP_SSL("smtp.qq.com", 465) 19 self.server.login(email_User, email_Pwd) 20 21 def send_email_msg(self,file_path): 22 test_Time = str(time.strftime("%Y_%m_%d", time.localtime())) 23 test_file_name = test_Time + "_testCase_request_Report.html" 24 msg_total = MIMEMultipart() # multipart类型主要有三种子类型:mixed、alternative、related 默认mixed 25 msg_total['From'] = Header("Test-Mine", 'utf-8') 26 msg_total['To'] = Header("leader", 'utf-8') 27 msg_total['Subject'] = '测试报告' 28 # 正文模块 29 msg_raw = open(file_path,"r",encoding='utf-8').read() 30 msg = MIMEText(msg_raw, 'html') 31 msg_total.attach(msg) 32 # 附件模块 33 mfile = MIMEApplication(open(file_path,"rb").read()) 34 # 添加附件的头信息 35 mfile.add_header('Content-Disposition', 'attachment', filename=test_file_name) 36 # 附件摸快添加到总的里面 37 msg_total.attach(mfile) 38 self.server.sendmail(self.email_User , self.to_Email_user , msg_total.as_string()) 39 print("邮件发送成功!请查收")
excel_file.py
1 from openpyxl import load_workbook 2 import json 3 class RwExcelFile: 4 5 def read_Excel_Case(self,file_path): 6 """获取excel的登录信息,保存到列表里面""" 7 excel_File = load_workbook(file_path) 8 sheet = excel_File.worksheets[0] 9 10 login_datas = [] 11 for row in range(2, sheet.max_row + 1): 12 user = { 13 "method": sheet.cell(row, 1).value, 14 "data": json.loads(sheet.cell(row, 2).value), 15 "exp_data": sheet.cell(row, 3).value, 16 "case_id": sheet.cell(row, 6).value, 17 } 18 login_datas.append(user) 19 excel_File.close() 20 return login_datas 21 22 def read_Excel(self,file_path,sheet_name,start_row,start_cloumn): 23 ''' 24 读取excel中所有数据并以列表形式返回 25 :param file_path: 26 :return: 27 ''' 28 excel_File = load_workbook(file_path) 29 sheet = excel_File[sheet_name] 30 list_column = [] 31 list_row = [] 32 for i in range(start_row,sheet.max_row+1): 33 for j in range(start_cloumn,sheet.max_column+1): 34 list_column.append(sheet.cell(i,j).value) 35 list_row.append(list_column) 36 list_column = [] 37 excel_File.close() 38 return list_row 39 40 41 def write_Excel(self,file_path,sheetname,row,cloumn,T_value): 42 ''' 43 向excel指定位置写入值 44 :param file_path: 文件地址 45 :param sheetname: sheet名 46 :param row: 行 47 :param cloumn:列 48 :param T_value: 值 49 :return: 无返回 50 ''' 51 excel_File = load_workbook(file_path) 52 excel_File[sheetname].cell(row,cloumn,T_value) 53 excel_File.save(file_path) 54 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
1 import unittest 2 import HTMLTestRunnerNew 3 import time 4 from email_report import email_Smtp 5 from Test import testCase_request 6 7 loader=unittest.TestLoader()#用例的加载器 8 suite=unittest.TestSuite()#创建了一个对象 9 suite.addTest(loader.loadTestsFromModule(testCase_request))#注意引入的是模块名不是类名,这里犯过错 10 11 #执行并生成html测试报告--HTMLTestRunnerNew 12 test_Time = str(time.strftime("%Y_%m_%d", time.localtime())) 13 test_file_name = test_Time+"_testCase_request_Report.html" 14 with open(test_file_name,'wb+') as file: 15 runner=HTMLTestRunnerNew.HTMLTestRunner( 16 stream=file, 17 verbosity=2, 18 title='测试报告', 19 description='request测试报告', 20 tester='Test_Mine' 21 )#创建一个对象来执行用例 22 runner.run(suite)#这一行没有任何改变 23 24 email_Smtp("****@qq.com", "密钥" , "*****@qq.com").send_email_msg(test_file_name)
邮件截图: