从接口自动化测试框架设计到开发(三)主流程封装、返回数据写入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接口自动化测试框架落地全流程|零基础到实战通关

相关推荐
llz_1127 分钟前
web-第二次课后作业
前端·后端·web
大数据魔法师4 小时前
Streamlit(二十三)- 教程(二)- 动态导航
python·web
vipbic5 小时前
别再把“做个H5”挂嘴边了:这个词,官方压根就没有定义过
前端
心中有国也有家6 小时前
GE图引擎深度解析——CANN的计算图优化与执行引擎
人工智能·pytorch·python·学习·numpy
ZC跨境爬虫7 小时前
跟着 MDN 学CSS day_39:(Flexbox 弹性盒子核心机制)
前端·css·ui·html·tensorflow
小陈同学呦7 小时前
前端如何处理订单状态导航的数据竞态问题
前端·javascript
卷毛的技术笔记7 小时前
告别硬编码!Spring AI Alibaba 实现 AI Agent 智能工具调用(Tool Calling)
java·人工智能·后端·python·spring·ai编程
编程大师哥7 小时前
匿名函数 lambda + 高阶函数
java·python·算法
喵个咪7 小时前
GoWind Toolkit 前端代码生成|Vue3(ElementPlus/Vben)、React(AntDesign)全自动一键生成教程
前端·vue.js·react.js
vb2008118 小时前
FastAPI APIRouter
开发语言·python