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

相关推荐
尘世中一位迷途小书童2 分钟前
无人机航线里的云台角度怎么调?Cesium 做了个视锥 + 鹰眼联动
前端·javascript·数据可视化
思码梁田5 分钟前
CSS display 属性:从元素类型转换到隐藏元素的实用指南
前端·css·display·inline·block·none·inline-block
Csvn6 分钟前
⚡ Vite 依赖预构建(optimizeDeps)的 3 个经典坑:改了源码不生效、新增依赖 404、缓存怎么清都不行
前端
很楠爱上14 分钟前
AI项目------赛博负熵:拆解一个 Codex 生成的英语背单词全栈工程(附源码文件免费)
人工智能·python·codex
A242073493017 分钟前
Vue3 + TypeScript:后端数据在表格内渲染后进行增删改的完整实现步骤
前端·javascript·typescript
小挪号底迪滴17 分钟前
Docker 多阶段构建实战:让 Python 镜像更小、更快、更安全
python·安全·docker
他们叫我 悦儿遥遥雨20 分钟前
.NET 8 Web开发入门(六):Blazor 全栈开发——告别 JavaScript 焦虑
前端·javascript·.net
WangChenGe26 分钟前
Windows 10/11 上 Pyenv-win 完整安装教程
python
想会飞的蒲公英26 分钟前
PyTorch中SGD 与 Momentum 从零理解:给最朴素的优化器加上“惯性“
人工智能·pytorch·python·深度学习·机器学习
用户21816970493027 分钟前
Flutter(九)StatelessWidget StatefullWidget
前端