专题定制网站建设工信部网站原来是
news/
2025/9/25 19:27:52/
文章来源:
专题定制网站建设,工信部网站原来是,搭建网站的架构,宁夏网站seo创建测试用例和测试结果集文件夹#xff1a; excel编写的接口测试用例如下#xff1a; 1 encoding 响应的编码格式。所测项目大部分是utf-8#xff0c;有一个特殊项目是utf-8-sig
2 params 对应requests的params 3 data#xff0c;对应requests的data 有些参数是动态的 excel编写的接口测试用例如下 1 encoding 响应的编码格式。所测项目大部分是utf-8有一个特殊项目是utf-8-sig
2 params 对应requests的params 3 data对应requests的data 有些参数是动态的写参数名程序中用eval()函数转化 baseapi.py #-*-coding:utf-8-*-接口基类
dinghanhua
baseapi.py
import requests
from requests.cookies import RequestsCookieJarclass apitest:def __init__(self):self.cookies RequestsCookieJar()def response_text(self,method,url,encodingutf-8,**kwargs):请求函数response requests.request(method,url,cookiesself.cookies,**kwargs)self.cookies.update(response.cookies) #更新cookiesif encoding: # 如果有响应编码则配置编码response.encoding encodingresult response.textreturn result runtest.py #-*-coding:utf-8-*-读取excel用例并执行
dinghanhua
import xlrd
from xlutils.copy import copy
from baseapi import apitest
import datetime
import os
import re
import logging;logging.basicConfig(levellogging.INFO)def run_testcase(excelpath,sheetindex0):# region 读取excel和sheetbook xlrd.open_workbook(excelpath) # 打开exceltry:sheet book.sheet_by_index(sheetindex) # 读取sheetwb copy(book) # 转变成xlwt book对象wsheet wb.get_sheet(sheetindex)except IndexError:logging.info(读取的sheet不存在)raise IndexError(读取的sheet不存在)#endregion# region 读取和运行用例testapi apitest()fail 0 # 用例执行失败个数success 0 # 用例执行成功个数for row in range(1, sheet.nrows):isrun str(int(sheet.cell_value(row,8))).strip() # 是否运行if isrun 1: # 判断用例是否运行运行则读取其他字段label str(sheet.cell_value(row, 1)).strip()method str(sheet.cell_value(row,2)).strip()url str(sheet.cell_value(row, 3)).strip()encoding str(sheet.cell_value(row, 4)).strip()headers str(sheet.cell_value(row, 5)).strip()params str(sheet.cell_value(row, 6)).strip()data str(sheet.cell_value(row, 7)).strip()checkpoint str(sheet.cell_value(row, 9)).strip()try:params eval(params) # 参数转变 从字符串转变成字典或带入参数转变不了则不处理except:passtry:data eval(data)except:passtry:headers eval(headers)except:passactual_result testapi.response_text(methodmethod,urlurl,paramsparams,datadata,encodingencoding,headersheaders) # 获取响应if re.search(checkpoint,actual_result): #测试通过写excel 测试通过计数加1wsheet.write(row,10,pass)success 1logging.info(labeltest pass)else: #测试不通过写excel 测试不通过计数加1wsheet.write(row,10,fail)wsheet.write(row,11, actual_result) # 写入响应fail 1logging.info(%s test fail.检查点%s,响应结果%s%(label,checkpoint,actual_result))# endregion# region 保存测试结果filepath os.path.join(os.path.dirname(__file__),testresult) # 测试结果目录if not os.path.exists(filepath): # 目录不存在则创建os.mkdir(filepath)filename os.path.join(filepath,datetime.datetime.now().strftime(%Y%m%d%H%M%S%f).xls) # 测试结果文件wb.save(filename) # 保存文件logging.info(测试结果测试通过%s测试不通过%s。测试结果文件路径%s%(success,fail,filename))# endregion#执行测试用例# 测试要用的动态参数
nowtime datetime.time().strftime(%Y%m%d%H%M%S%f) # 时间戳
today str(datetime.date.today()) # 当前日期
newdate (datetime.datetime.now()-datetime.timedelta(minutes30)).strftime(%Y-%m-%d %H:%M:%S) # 当前时间点前半小时
sso test # sso串
id 1 # id
token token # token通过抓包获取excelpath rC:\Users\dinghanhua\PycharmProjects\ApiTest\testcase\testcase1.xlsx # excel用例文件
run_testcase(excelpathexcelpath,sheetindex1) # 逐条读取并运行用例 运行之后testresult文件夹下查看已生成的测试结果文件文件名测试运行时间戳
该项目用jmeter、postman也可以做接口测试。用python脚本可以用excel写好用例直接执行。脚本中的检查点等可根据实际项目再调整。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/917420.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!