python接口测试框架哪个好_python接口测试框架(1)

本文介绍了作者使用Python进行接口测试的过程,通过Weather API为例,展示了如何创建Excel测试用例、配置文件管理及HTTP请求操作。展示了从Python编程、文件操作到实际测试执行的完整流程。
摘要由CSDN通过智能技术生成

最近在做接口测试时,选用了多种方案,由于本人喜欢在测试时随性发挥,jmeter这种方案直接被忽略了,那么就考虑用java或python,发现python代码及其简略,便选用了python进行接口测试,如下展示我的框架简图

968d6b5d68dd

image.png

下面就开始我们的工作吧

本次测试直接选用天气预报的接口试试手,参考连接https://www.sojson.com/open/api/weather/json.shtml?city=北京

https是请求的方式,www.sojson.com是后台服务器域名,/open/api/weather/json.shtml是路径名,city=北京是请求参数

首先创建好一个excel准备测试用例,excel如下

968d6b5d68dd

image.png

下面开始我们的python编程,本人选用python2.7

安装好xlrd,configparser,urllib2模块,可通过pip insgtall 的方式安装

我的python目录结构如下

968d6b5d68dd

image.png

下面附上代码

1.config.ini文件内容如下

[Excel]

filepath = D:\\TestWether.xls

2.FileUtils.py代码如下

#!/usr/bin/env python

# -*- coding:utf-8 -*-

import time

import xlrd

from xlutils.copy import copy

import configparser

class FileUtil:

cfg = configparser.ConfigParser() # 创建对象

print cfg.read('config.ini') # 读取ini配置文件

filepath = cfg.get("Excel", "filepath")

#获取当前时间

def getTime(self):

return time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

#读取指定单元格的内容

def read_excel(self,x, y):

# 打开文件

workbook = xlrd.open_workbook(self.filepath)

# 获取所有sheet

# print workbook.sheet_names() # [u'sheet1', u'sheet2']

# 获取sheet2

sheet_name = workbook.sheet_names()[1]

# 根据sheet索引或者名称获取sheet内容

sheet1 = workbook.sheet_by_name('Sheet1')

aa=sheet1.cell(x, y).value

if type(aa)==float:

return str(aa).encode('utf-8')

return aa

# return str(sheet1.cell(x, y).value).encode('utf-8')

def read_excel_h_w(self):

# 打开文件

workbook = xlrd.open_workbook(self.filepath)

# 获取所有sheet

# print workbook.sheet_names() # [u'sheet1', u'sheet2']

# 获取sheet2

sheet_name = workbook.sheet_names()[1]

# 根据sheet索引或者名称获取sheet内容

sheet1 = workbook.sheet_by_name('Sheet1')

# sheet的名称,行数,列数

return [sheet1.nrows, sheet1.ncols]

#写入excel

def write_excel(self,x,y,str):

workbook = xlrd.open_workbook(self.filepath)

workbooknew = copy(workbook)

ws = workbooknew.get_sheet(0)

ws.write(x, y, str)

workbooknew.save(self.filepath)

3.HttpTestUtils.py代码如下

#!/usr/bin/env python

# -*- coding:utf-8 -*-

from __future__ import division

import json,urllib2

class HttpRequest():

def sendGetRequest(self,url,args1):

#请求网址和请求参数

url1 =url+'?'+args1

# textmod = urllib2.urlencode(textmod)

# print(textmod)

print url

#发送请求

req = urllib2.Request(url1)

#请求响应

res = urllib2.urlopen(req)

#读取响应结果

res = res.read()

#响应结果转化为json格式

s = json.loads(res);

# print json.dumps(s, ensure_ascii=False).decode('utf8').encode('gb2312')

data = json.dumps(s, ensure_ascii=False)

return data

def sendPostRequest(self, url, args,header):

# textmod = args

#请求的参数json转化为str

text = json.dumps(args)

# text= json.dumps(args, ensure_ascii=False).decode('utf8').encode('gb2312')

print(text)

#发送请求

req = urllib2.Request(url=url, data=text, headers=header)

#请求响应

res = urllib2.urlopen(req)

#读取请求响应结果

res = res.read()

return res

# print(res)

4.RunTestCae.py代码如下

#!/usr/bin/env python

# -*- coding:utf-8 -*-

import sys

reload(sys)

sys.setdefaultencoding( "utf-8" )

from HttpTestUtils import HttpRequest

from FileUtils import FileUtil

p=HttpRequest()

f=FileUtil()

h=f.read_excel_h_w()[0]

w=f.read_excel_h_w()[1]

#for循环遍历excel

for a in range(1,h):

testName=f.read_excel(a,0)

host=f.read_excel(a,1)

path=f.read_excel(a,2)

httpMethod=f.read_excel(a,3)

header=f.read_excel(a,4)

args=f.read_excel(a,5)

print args

url=host+path

#执行get请求

if(httpMethod=='get'):

str1=p.sendGetRequest(url, args)

print testName+"========="+str1

starttime=f.getTime()

#写入执行时间到excel

f.write_excel(a, 6, starttime)

#写入响应结果到excel

f.write_excel(a, 7, str1)

if(httpMethod=='post'):

headers = header

str1=p.sendPostRequest(url, args, headers)

print testName+"========="+str1

starttime = f.getTime()

f.write_excel(a, 6, starttime)

f.write_excel(a, 7, str1)

执行RunTestCae.py类后控制台执行结果如下

968d6b5d68dd

image.png

然后再打开exacel里面会显示用例的执行时间和响应参数

968d6b5d68dd

image.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值