本文涉及到的类库,configparser,xlrd,xlutils,requests
目标:实现简单的http接口测试,把数据放到Excel表格中,通过读取Excel表格中的数据进行测试,测试结果回写到Excel表格中
baseurl = 127.0.0.1
port = 8000
/api/add_event/
import configparser
import xlrd
from xlutils.copy import copy
cf = configparser.ConfigParser()
cf.read('config.ini') # 之所以能够直接传文件名,是因为执行py文件和config.ini文件在同级目录下
# fp = open(r'config.ini','w') # 必须先读取,再open否则内容无法写进config文件
def getbaseurl():
return cf.get('HTTP','baseurl')+":"+cf.get('HTTP','port')
def gettestdatakey():
table = xlrd.open_workbook('11.xls')
data = table.sheet_by_name('Sheet1')
return data.row_values(0)
def gettestdatavalue():
array = []
table = xlrd.open_workbook('11.xls')
data = table.sheet_by_name('Sheet1')
for i in range(1,data.nrows):
array.append(data.row_values(i))
return array
def gettestdatadic():
tmpvalue = gettestdatavalue()
tmpkey = gettestdatakey()
dic = []
for i in tmpvalue:
dic.append(dict(zip(tmpkey,i)))
return dic
def gettestresult(resultdata):
rb = xlrd.open_workbook('11.xls','w+b')
rs = rb.sheet_by_name('Sheet1')
cols = rs.ncols
wb = copy(rb)
data = wb.get_sheet(0)
for i in range(rs.nrows):
if i == 0:data.write(0, cols, 'Execute Result')
else:data.write(i,cols,str(resultdata[i-1]))
wb.save('11.xls')
import requests
from one import getbaseurl,gettestdatadic,gettestresult
url = 'http://'+getbaseurl()+'/api/add_event/'
data = gettestdatadic()
resultdata = []
for i in data:
tt = requests.post(url=url,data=i)
resultdata.append(tt.json())
gettestresult(resultdata=resultdata);
注:congfig.ini 文件要和one.py文件在同一级目录下,否则读取会出现问题;也可以不放在同一级目录下,但是read的时候要传文件的绝对路径进去。