The class libraries involved in this article, configparser, xlrd, xlutils, requests
Objective: to achieve a simple http interface test, put the data into the Excel table, test by reading the data in the Excel table, and write the test results back to the Excel table
1. Data preparation
2. Interface, port, path
baseurl = 127.0.0.1 port = 8000 /api/add_event/
3. Read the config file and Excel file and write the code( one.py)
import configparser import xlrd from xlutils.copy import copy cf = configparser.ConfigParser() cf.read('config.ini') # The reason why the file name can be transferred directly is that the py file and the config.ini file are in the same level directory # fp = open(r'config.ini','w') # You must read it before open ing it, otherwise the content cannot be written into the config file 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')
4. Test examples( two.py)
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);
5. Operation results
Note: the congfig.ini file should be in the same level directory as the one.py file, otherwise reading will cause problems; it can also not be in the same level directory, but the absolute path of the file should be passed in when reading.