Python self-study interface test

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.