从接口自动化测试框架设计到开发(三)主流程封装、返回数据写入excel

主流程封装

复制代码
#Run_Test.py
# -*- coding: utf-8 -*-
# @Author: jiujiu
# @Date:   2020-03-04 16:30:31
# @Last Modified time: 2020-03-05 15:00:46
import sys
sys.path.append("G:/uni_test")
#添加当前过程的目录
import json
from base.run_method import RunMethod
from data.get_data import GetData
class RunTest:
    def __init__(self):
        self.runmethod = RunMethod()
        self.data = GetData()
    #程序执行
    def go_on_run(self):
        res = None
        #如果有10行,循环遍历每一行,从0行开始
        rows_count = self.data.get_case_lines()
        #排除0行,从第1行开始
        for i in range(1,rows_count):
            is_run = self.data.get_is_run(i)
            if is_run:
                url = self.data.get_request_url(i)
                method = self.data.get_request_method(i)
                data = self.data.get_data_for_json(i)#传入行数
                # request_data = self.data.get_data(i)
                header = self.data.is_header(i)
                # print(i)
                res = self.runmethod.run_main(method,url,data,header)
                # return res
                print(i)
                print(res)
            else:
                print('失败')
            # if is_run :
            #     res = self.runmethod.run_main(method,url,data,header)
            #     return res
            # else:
            #     return None
if __name__ == '__main__':
    run = RunTest()
    print(run.go_on_run())

返回结果:

如果想确定接口有没有挂掉,运行是否正常可以加上

复制代码
return res.status_code

如果返回200,说明运行正常,可以和预期结果在一起比对,就能实现上线以后查看接口运行情况的测试。

如果要处理返回数据,使用

复制代码
json.dumps(res,ensure_ascii=False,sort_keys=True,indent=2)

将返回的数据写入excel中

首先安装包

复制代码
pip install xlutils
复制代码
#operation_excel.py        
#写入数据
    def write_value(self,row,col,value):
        read_data = xlrd.open_workbook(self.file_name)#打开文件
        write_data = copy(read_data)#复制文件
        sheet_data = write_data.get_sheet(0)#获取第一个表
        sheet_data.write(row,col,value)#写入数据
        write_data.save(self.file_name)
复制代码
#get.data.py
#把返回结果写入excel
    def write_result(self,row,value):
        col = int(self.dataconfig.get_result())
        result = self.opera_excel.write_value(row,col,value)
复制代码
#Run.Test.py    
#程序执行
    def go_on_run(self):
        res = None
        #如果有10行,循环遍历每一行,从0行开始
        rows_count = self.data.get_case_lines()
        #排除0行,从第1行开始
        for i in range(1,rows_count):
            is_run = self.data.get_is_run(i)
            if is_run:
                url = self.data.get_request_url(i)
                method = self.data.get_request_method(i)
                data = self.data.get_data_for_json(i)#传入行数
                # request_data = self.data.get_data_for_json(i)
                header = self.data.is_header(i)
                # print(i)
                res = self.runmethod.run_main(method,url,data,header)
                # return res
                self.data.write_result(i,res)#写入数据
                print(i)
                print(res)
            else:
                print('失败')

我把post和get方法里面返回的res改成了res.url,即返回请求的url

运行结果:

excel结果:

预期结果这一列已写入了返回的数据

高薪必备!18K接口自动化测试框架落地全流程|零基础到实战通关

相关推荐
王中阳Go几秒前
两个入口答案不一致先别调模型:6 行入口口径对账表(附脚本)
人工智能·后端·python·大语言模型·1024程序员节
小poop7 分钟前
CSS 基础与实战:从入门到精通网页美化
前端
BK贝壳12 分钟前
bool-hybrid-array 落盘我只认 .bha:用 Create_BHA / Ask_BHA 做布尔数组序列化
数据结构·python·numpy·1024程序员节
Sakura_j_h_38 分钟前
Python 在定时任务里输出 emoji,UnicodeEncodeError 从哪来
开发语言·字符编码·windows·python·定时任务
Csvn2 小时前
运行时性能
前端
supabc1232 小时前
欧盟电子发票(XRechnung/Peppol/ZUGFeRD)踩坑记:用 Python 不装 Java 跑官方校验规则
xml·python·开源·电子发票·1024程序员节
Never_every992 小时前
位图转矢量工具哪个好?实测抠抠图AI位图转矢量工具
大数据·前端
ZhengEnCi2 小时前
AI Agent工具调用防护层怎么写-一份能直接跑的Python四层拦截实现
python·llm·agent
paopaokaka_luck3 小时前
宠物门店经营管理系统(销售与库存联动、进货自动记账、 退换与退款关联、员工个人业绩、ECharts 经营分析、滞销识别与客户分层、宠物档案年龄计算)
前端·javascript·spring boot·mysql·echarts·mybatis·1024程序员节
ZhengEnCi3 小时前
AI Agent工具误调用怎么解决-从参数校验到高危二次确认的四层工程防线
python·llm·agent