QA不加班 QAbujiaban | QA不加班 [微信群:QAbujiaban] | 博客园 | 管理 | 写随笔

Python unittest+ddt+openpyxl

1.技术介绍

框架:unittest

请求处理:requests

excel数据处理:openpyxl

参数化:ddt

报告模板:HTMLTestRunnerNew.py(下载地址:https://pan.baidu.com/s/1w9AZU9AkIpxCYuzTto0EQA?pwd=1234)

testdemo.xlsx:测试数据,注意:implement=y为执行数据,否则不执行

2.代码正题结构

----->DoCase.py  测试用例类
----->DoExcel.py  Excel文件处理类,筛选有效数据返回
----->DoHttp.py    http请求处理类
----->HttpRunner  unittest加载用例执行生成报告
----->report.html   执行后生成的报告
----->HTMLTestRunnerNew.py  测试报告模板

 

3.Docase.py

#测试用例类
import unittest
from test02.DoHttp import Request_Http#引入请求类
from ddt import ddt,data,unpack#引入ddt做参数化
from test02.DoExcel import GetExcelData
data_list = GetExcelData("C:\\Users\\Administrator\\Desktop\\testdemo.xlsx","s1").get_data()
#用例类
@ddt#装饰器装饰类
class HttpCase(unittest.TestCase):#继承unittest测试用例类
    @data(*data_list)#脱外套
    def test_login_yes(self,item):
        res = Request_Http(item["method"], item["url"], item["data"]).request_http()
        try:
            self.assertEqual(item["expect"],res.json()["code"])#断言:预期与实际是否相等
        except Exception as e:
            print("断言异常:{0}".format(e))
            raise e#抛出异常

4.DoExcel.py

#Excel文件处理类
from openpyxl import load_workbook
class GetExcelData():
    def __init__(self,file,sheet):
        self.file=file
        self.sheet=sheet
    def get_data(self):
        wb = load_workbook(self.file)#打开excel
        sheet = wb[self.sheet]#定位工作簿
        data_list = []
        for n in range(2,sheet.max_row+1):
            data_dic = {}
            for m in range(2,sheet.max_column+1):
                data_dic["case_id"] = sheet.cell(n, 1).value
                data_dic["module"] = sheet.cell(n, 2).value
                data_dic["title"] = sheet.cell(n, 3).value
                data_dic["method"]=sheet.cell(n,4).value
                data_dic["url"]=sheet.cell(n,5).value
                data_dic["data"]=eval(sheet.cell(n, 6).value)#还原数据类型
                data_dic["expect"]=sheet.cell(n, 7).value
                data_dic["implement"]=sheet.cell(n, 8).value#y执行
            if data_dic["implement"]=="y":#如果implement为y添加到列表
                data_list.append(data_dic)
        return data_list

5.DoHttp.py

#http请求处理类
import requests
class Request_Http():
    def __init__(self,method,url,data,expected=None,headers=None,cookie=None):
        self.method=method.lower()#请求方式转小写
        self.url=url
        self.data=data
        self.excepted=expected
        self.headers=headers
        self.cookie=cookie

    def request_http(self):
        if self.method=="get":
            try:
                return requests.get(self.url, params=self.data, headers=self.headers, cookies=self.cookie)
            except Exception as e:
                print("异常请求:{0}".format(e))
                raise e  # 抛出异常
        else:
            try:
                return requests.post(self.url,params=self.data,headers=self.headers,cookies=self.cookie)
            except Exception as e:
                print("异常请求:{0}".format(e))
                raise e#抛出异常

6.HttpRunner.py

#unittest加载用例执行生成报告
import unittest
from test02 import DoCase
import HTMLTestRunnerNew
suite = unittest.TestSuite()#存放测试用例

loader = unittest.TestLoader()#加载器
suite.addTest(loader.loadTestsFromModule(DoCase))#加载测试模块

#上下文管理器
with open("report.html","wb") as file:
    runner = HTMLTestRunnerNew.HTMLTestRunner(stream=file, verbosity=2,title="标题",description="备注",tester="姓名")
    runner.run(suite)

7.report.html

8.报告模板下载

HTMLTestRunnerNew.py(下载地址:https://pan.baidu.com/s/1w9AZU9AkIpxCYuzTto0EQA?pwd=1234)

 

posted @ 2022-12-20 22:52  味卜鲜码(QA不加班)  阅读(90)  评论(0编辑  收藏  举报
QA不加班 QAbujiaban | QA不加班 [微信群:QAbujiaban]