python接口測試(三)——Excell文件讀取進行參數化

python進行http請求時,須要對參數進行參數化,此時就能夠運用Excel進行,具體以下:python

1.梳理出請求中那些參數須要參數化,而後新建一個Excel,如圖:json

  

2.讀取Excel中的內容,在讀取前須要導入python中的xlrd,Python讀取Excel文件須要使用第三方的庫文件xlrd,方可對文件進行操做,經過文件的位置,sheet名稱,讀取每行的內容app

#coding=utf-8
import xlrd
def xlrd_excel():
    data = xlrd.open_workbook('params.xls')  # 打開xls文件
    table = data.sheet_by_name(u'alisaName') #經過名稱獲取
    nrows=table.nrows  # 獲取表的行數
    li=[]
    for i in range(nrows):     # 循環逐行打印
        if i!=0:        #跳過第一行
            li.append(str(table.row_values(i)))
    return li        #將取回的值放入列表中
print xlrd_excel()

獲取sheet也能夠經過data.sheet_by_index();url

3.請求中調用,讀取時已經將其放入list中,因此直接就好spa

#coding:utf-8
import requests, login, ast,json
from testcase import readExcel
def login(li):
    url= "http://xxx.xxx.com/login"
    headers={'Content-Type':'application/json;charset=UTF-8' }
    li=ast.literal_eval(li)     #運算響應的內容是否爲合法的python類型
    request_type={
    'name':li[0],
    'password':li[1]
    'phone':li[2]
    }
    response=requests.get(url,params=request_type,headers=headers)
    return response.text

for i in readExcel.xlrd_excel():
    if i[0]:
        print login(i)
相關文章
相關標籤/搜索