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

相关推荐
AI视觉网奇3 小时前
rknn yolo11 推理
前端·人工智能·python
gplitems1233 小时前
Gunslinger – Gun Store & Hunting WordPress Theme: A Responsible
开发语言·前端·javascript
AI数据皮皮侠4 小时前
中国各省森林覆盖率等数据(2000-2023年)
大数据·人工智能·python·深度学习·机器学习
西柚小萌新5 小时前
【深入浅出PyTorch】--3.1.PyTorch组成模块1
人工智能·pytorch·python
Bella_chene6 小时前
Excel转PDF不分页
pdf·excel
wyzqhhhh6 小时前
less和sass
前端·less·sass
2401_841495647 小时前
【数据结构】红黑树的基本操作
java·数据结构·c++·python·算法·红黑树·二叉搜索树
西猫雷婶7 小时前
random.shuffle()函数随机打乱数据
开发语言·pytorch·python·学习·算法·线性回归·numpy
♛小小小让让7 小时前
RabbitMQ (二)参数
笔记·python·rabbitmq
fly-phantomWing7 小时前
在命令提示符页面中用pip命令行安装Python第三方库的详细步骤
开发语言·python·pip