python自动化接口测试excel脚本_python+requests+excel 实现接口自动化测试

前言

自己摸索写了一套接口自动化的脚本,目前可以完成本人测试的需求。在这里做下分享。(也许会有各种局限性,欢迎大家给我留言~~)

方案

目前我们常用的接口协议是http协议,其测试的基本原理是模拟前端(客户端)向服务器发送数据,得到相应的响应数据,从而判断接口是否可以正常的进行数据交换。

本人的想法是在excel中维护请求数据也就是用例,通过脚本读取excel数据,构造请求;判断响应结果与excel表中的校验字段是否一致,同时将执行结果更新到excel表中。

excel用例格式如下:

0c2d66c4a892

用例表.png

说明:

关键字:接口请求的方式。目前支持的有两种:post,get

参数:post方式参数格式需为json串;get方式参数格式为:parameter1=value1&parameter2=value2

headers:请求头,需为json格式;若接口无需请求头,放空即可

校验字段:响应结果中的某个字段,与校验值一起判断是否执行成功

执行结果如下:

0c2d66c4a892

执行结果.png

附上代码:

interfacetest.py

import requests

import xlrd

import json

import xlwt

from xlrd import open_workbook

def xlsx_open(filepath):

book = xlrd.open_workbook(filepath)

return book

def xlsx_getRow(sheet, row):

object = {}

object["method"] = sheet.cell_value(row, 2)

object["host"] = sheet.cell_value(row, 3)

object["path"] = sheet.cell_value(row, 4)

object["url"] = object["host"] + object["path"]

object["params"] = sheet.cell_value(row, 5)

object["header"] = sheet.cell_value(row, 6)

object["code"] = sheet.cell_value(row, 7)

object["expected_code"] = sheet.cell_value(row, 8)

if object["header"] == "":

object["header"] = None

return object

def xlsx_request(object):

try:

if object["method"] == "post":

response = requests.post(object["url"], data=json.loads(object["params"]), headers=object["header"], cookies=object["cookies"])

result = response.json()

elif object["method"] == "get":

response = requests.get(object["url"], object["params"], headers=object["header"], cookies=object["cookies"])

result = response.json()

else:

print("Unknown method " + object["method"])

except requests.exceptions.ConnectTimeout as e:

result = {object["code"]: "timeout"}

print(result)

return result

def xlsx_set(sheet, row, col, value, red=False):

style = "font:colour_index red;"

if red == False:

sheet.write(row, col, value)

else:

sheet.write(row, col, value, xlwt.easyxf(style))

def xlsx_save(book, filepath):

book.save(filepath)

def dosheet(brd, bwt, sheetIndex, cookies=None):

brd_sheet = brd.sheets()[sheetIndex]

bwt_sheet = bwt.get_sheet(sheetIndex)

count = brd_sheet.nrows

for i in range(1, count):

object = xlsx_getRow(brd_sheet, i)

object["cookies"] = cookies

result = xlsx_request(object)

if result.get(object["code"]) == object["expected_code"]:

xlsx_set(bwt_sheet, i, 9, "pass", False)

xlsx_set(bwt_sheet, i, 10, result[object["code"]], False)

else:

xlsx_set(bwt_sheet, i, 9, "fail", True)

xlsx_set(bwt_sheet, i, 10, result[object["code"]], False)

runtest.py

from interfacetest import xlsx_open

from interfacetest import xlsx_save

from interfacetest import dosheet

import requests

from xlutils.copy import copy

# 打开文件

brd = xlsx_open("接口用例.xlsx")

# copy文件用于写入结果

bwt = copy(brd)

# 获取cookie

# url = "http://xxx"

# data = {

# "name":"xxx",

# "password":"xxx"

# }

# response = requests.post(url,data)

# c = response.cookies

# cookies = dict(c)

# 执行文件

dosheet(brd, bwt, 0)

# dosheet(brd, bwt, 1, cookies)

# 保存

xlsx_save(bwt, "用例result.xlsx")

说明:

部分接口需要传入token的话,自行加入获取代码,传参中加入cookie字段

0c2d66c4a892

获取cookie.png

需要导入requests库,一个很实用的python HTTP客户端库

python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库。但xlwt只能在原文件中修改,我希望能够保留原用例表,所以引入了中间库xlutils(依赖于xlrd和xlwt)提供复制excel文件内容和修改文件的功能

以上内容对大家有用的话,麻烦点个赞。也欢迎大家给我反馈问题,一起进步~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值