Try to write a simple interface test, based on Python3.4, the main use of Python read Excel and requests library knowledge, but also for this period of time Python basic knowledge of learning a consolidation bar.
Because you have not learned the knowledge of classes, objects, and so on in Python, the code seems to be scattered, not encapsulated, or optimized. If there is a problem, I hope you can help point out.
Import xlrd
Import requests
#下文中将用到的全局变量
nrows = 0
Rdict = {}
Relist=[]
#从excel中导入数据
Def imptestcase ():
fname = ' testcase.xlsx '
BK = Xlrd.open_workbook (fname)
Try
Sheet = bk.sheet_by_name (' case1 ')
Except
Print ("No sheet in%s named Sheet1"%fname)
Global nrows
nrows = Sheet.nrows
Ncols = Sheet.ncols
For I in Range (0,ncols):
data = Sheet.col_values (i)
Global Rdict
Rdict[data[0]]=data[1:]
#将excel读取的数据封装成请求, and send
Def sendpost ():
#response = requests.post (Url,json = data,headers = Headers,verify=false)
For I in Range (0,nrows-1):
url = rdict[' url '][i]
If rdict[' headers '][i]!= ':
headers = eval (rdict[' headers '][i]) #str to Dict
Else
headers = {}
If rdict[' json '][i]!= ':
JSON = eval (rdict[' json '][i]) #str to Dict
Else
JSON = {}
If rdict[' params '][i]!= ':
params = eval (rdict[' params '][i]) #str to Dict
Else
params = {}
Try
R = requests.get (Url,json = json,headers = Headers,params = Params,verify=false)
R.raise_for_status ()
R.encoding=r.apparent_encoding
Global relist
Relist.append (R.status_code)
Except Exception as E:
Print (e)
Print (' request failed ')
#把返回的结果输出到html中, form HTML reports (simple display of knowledge, no style)
Def output_html ():
Fout=open (' output.html ', ' W ', encoding= ' UTF-8 ', newline= ')
Fout.write (' Fout.write (' Fout.write (' <meta charset = ' UTF-8 '/> ')
Fout.write (' Fout.write (' <body> ')
Fout.write (' Fout.write (' <table> ')
Fout.write (' <tr> ')
Fout.write (' <td>%s</td> '% relist)
Fout.write (' </tr> ')
Fout.write (' </table> ')
Fout.write (' </body> ')
Fout.write (' Fout.close ()
if __name__ = = ' __main__ ':
Imptestcase ()
Sendpost ()
Output_html ()
"Excel Content"
Python self-study interface test