模块1 :getdata.py 读取测试用例test_case.xlsx,返回测试用例结果
import json from openpyxl import load_workbook class DoExcel: """ :get_data 读取execl中的测试数据 :write_data 将测试结果写入excel中 """ def get_data(self): """读取excel中的登录信息存储到列表中""" wb = load_workbook("test_case.xlsx") sheet = wb["test_case"] login_data=[] print(sheet.max_row) for row in range(2,sheet.max_row+1): user={"url":sheet.cell(row,5).value, "method":sheet.cell(row,4).value, # "data": sheet.cell(row, 6).value, "data": json.loads(sheet.cell(row, 6).value), "exp_result":sheet.cell(row,7).value, "case_id": sheet.cell(row, 1).value } login_data.append(user) wb.close() return login_data def write_result(self,row,result): """将测试结果返回到excel中""" wb = load_workbook("test_case.xlsx") sheet = wb["test_case"] sheet.cell(row,8,result) wb.save("test_case.xlsx") wb.close() if __name__ == '__main__': res=DoExcel().get_data() print(res)
模块2 :httprq.py 获取login的请求信息
import requests class HttpRequest: """完成http的get请求或post请求 :method 请求方法 可以是get or post :url 请求地址 :data 请求参数,字典的格式 """ def http_request(self,url,method,data): if method.lower()=="get": return requests.get(url,params=data) elif method.lower()=="post": return requests.post(url,data=data) if __name__ == '__main__': t=HttpRequest().http_request("http://47.107.168.87:8080/futureloan/mvc/api/member/login", "get", {"mobilephone":"1868877346", "pwd":123456}) print(t.json()) print(t.text)
模块3 :logintest.py 编写测试用例
import unittest from ddt import ddt, data, unpack from httprq import HttpRequest from homework.ddt_20190323.unittest_ddt_exl_http_request.getdata import * @ddt class Testlogin(unittest.TestCase): """通过ddt将excel中获取的数据循环赋值到data""" @data(*DoExcel().get_data()) @unpack def test_httprq(self,method,data,url,exp_result,case_id):#方法名必须以test开头 res=HttpRequest().http_request(url,method,data).json()#http请求的实际值 try: self.assertEqual(exp_result,res["msg"])#断言,实际值与预期值进行对比 row=case_id+1 DoExcel().write_result(row,"pass") #通过,excel中返回pass except Exception as e:#捕获异常 row=case_id+1 DoExcel().write_result(row,"fail")#不通过,excel中返回fail raise Exception #抛出异常
模块4 :runtest.py 执行测试用例,生成测试报告
import unittest from homework.ddt_20190323.unittest_ddt_exl_http_request import testlogin import HTMLTestRunnerNew class RunCase: """ :test_suite_html 执行测试用例后生成html的测试报告 :test_suite_module 按test_module来执行测试用例 :test_suite_testcase 按照测试用例类来执行测试用例 """ def test_suite_html(self): """ 测试步骤:新建一个用例收集器 新建一个用例加载器 suit.addTest()通过加载测试模块来收集用例 新建一个html文档来存放测试报告 :return: """ suite=unittest.TestSuite() loader=unittest.TestLoader() suite.addTest(loader.loadTestsFromModule(testlogin)) with open ("login_http.html","wb") as file: runner=HTMLTestRunnerNew.HTMLTestRunner(stream=file, verbosity=2,#生成报告级别 title="login测试报告", description="验证登录是否成功", tester="七月")#创建一个对象来执行测试用例 runner.run(suite)#执行收集器中的所有测试用例 def test_suite_module(self): suite=unittest.TestSuite()#用例收集器 loader=unittest.TestLoader()#用例加载器 runner=unittest.TextTestRunner(verbosity=0)#用例执行器 suite.addTest(loader.loadTestsFromModule(testlogin))#收集测试用例 runner.run(suite)#执行测试用例 if __name__ == '__main__': RunCase().test_suite_html() # RunCase().test_suite_module()
5、sendemail.py发送测试报告邮件
import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.header import Header from configparser import ConfigParser from email.mime.image import MIMEImage class SendMail: def sent_mail(self): # 第三方smtp服务 mail_host = "smtp.qq.com" # 设置服务器 # 获取发件箱的登录名,登录密码 cp=ConfigParser() cp.read("user_pwd.config",encoding="utf-8") mail_user = cp.get("userinfo","emailname") mail_pass = cp.get("userinfo","emailpwd") # 配置文件中获取发件地址,收件地址 sender=cp.get("userinfo","emailname") receivers=[cp.get("userinfo","emailname")] # 创建一个带附件,带图片的邮件实例,添加邮件主题,发件人,收件人 message=MIMEMultipart("mixed") subject = "Python SMTP 邮件测试" message["Subject"] = Header(subject,"utf-8") message["From"] = Header("七月","utf-8") message["To"] = Header("七月","utf-8") # 添加附件1、test_case.xlsx with open("test_case.xlsx","rb") as attach_msg1: attach1 = MIMEText(attach_msg1.read(),"plain","utf-8") attach1["Content-Type"] = "application/octet-stream" attach1["Content-Disposition"] = "attachment;filename='test_case.xlsx'" message.attach(attach1) # 添加附件2、login_http.html with open("login_http.html", "rb") as attach_msg2: attach2 = MIMEText(attach_msg2.read(), "html", "utf-8") attach2["Content-Type"] = "application/octet-stream" attach2["Content-Disposition"] = "attachment;filename='login_http.html'" message.attach(attach2) # 添加邮件正文 with open("login_http.html", "rb") as attach_msg2: mail_msg =attach_msg2.read() message_con = MIMEText(mail_msg, "html", "utf-8") message.attach(message_con) try: server = smtplib.SMTP(mail_host,25) # 创建邮件服务商 server.login(mail_user,mail_pass) # 登录邮箱 server.sendmail(sender,receivers,message.as_string()) # 发送邮件 print("邮件发送成功") except smtplib.SMTPException : print("Error:邮件发送失败") if __name__ == '__main__': SendMail().sent_mail()