python+request+Excel做接口自動化測試(二)
今天整了下python用request寫接口測試用例,做了個粗糙的大概的執行,具體還需找時間優化。這個采用對象化,配置以及常用函數為一個類,執行測試用例為另外的一個類。
測試用例的撰寫大概如下(還需美化):
1、寫測試用例
這裏使用pyunit,讀取excel中的測試用例並執行
# coding=utf-8
from openpyxl import load_workbook
import sys
import json
reload(sys)
sys.setdefaultencoding(‘utf8‘)
from requestConfig import testClass
import unittest
class testApi(unittest.TestCase):
def setUp(self):
self.test=testClass()
print ("開始")
def tearDown(self):
print ("結束")
def test0000101(self):
test=self.test
host=test.readSheetdata(‘D2‘)
path=test.readSheetdata(‘E2‘)
data=test.readSheetdata(‘F2‘)
method=test.readSheetdata(‘G2‘)
url=host+path
print url
print data
print method
res=test.httpGetOrPost(method,url,data)
print res
testCode=res[‘code‘]
def test0000201(self):
test=self.test
host=test.readSheetdata(‘D12‘)
path=test.readSheetdata(‘E12‘)
data=test.readSheetdata(‘F12‘)
method=test.readSheetdata(‘G12‘)
url=host+path+test.getAfterUrl()
res = test.httpGetOrPost(method, url, data)
print res
test.assertResponseCode(res[‘code‘],‘H12‘)
if __name__ == "__main__":
unittest.main()
1、配置文件
主要是封裝用到的函數。包括http請求,讀excel,url配置
import requests
import unittest
import json
from openpyxl import load_workbook
import sys
reload(sys)
sys.setdefaultencoding(‘utf8‘)
import csv
reload(sys)
sys.setdefaultencoding(‘utf8‘)
class testClass(object):
#封裝http POST 函數,返回請求response
def httpPost(self,keyword,url):
# data=json.dumps(keyword)
headers={"Content-Type":"application/json"}
res=requests.post(url,data=keyword,headers=headers)
responseJson=res.json()
return responseJson
#封裝http Get函數,返回response
def httpGet(self,keyword,url,data):
headers={"Content-Type":"application/json"}
res=requests.get(url,data=data,headers=headers)
responseJson=res.json()
return responseJson
#封裝請求函數
def httpGetOrPost(self,method,url,data):
# global mres
headers = {"Content-Type": "application/json"}
if method in "get":
mres=requests.get(url,data=data,headers=headers)
elif method == "post":
# postdata = json.dumps(data)
mres=requests.post(url,data=data,headers=headers)
elif method in"put":
mres=requests.put(url,data=data,headers=headers)
elif method in "delete":
mres=requests.delete(url,data=data,headers=headers)
else:
mres = requests.post(url, data=data, headers=headers)
print("錯誤")
# responseJson=mres.json()
return mres.json()
#讀excel
def readSheetdata(self,cell):
wb=load_workbook(r‘d:\apitestcase.xlsx‘)
sheet=wb.active
value=sheet[cell]
# print(value.value)
return value.value
#封裝斷言函數
def assertResponseCode(self,code,cell):
excelcode=self.excelCode=self.readSheetdata(cell)
try:
assert excelcode==code
except AssertionError as msg:
print "斷言失敗"
return False
else:
return True
def getLoginUserMess(self):
method="post"
url="http://apptest.buddyniu.com/api/apps/BUDDY_API_TEST/accounts/login"
data={"password":"e10adc3949ba59abbe56e057f20f883e","clientSecret":"a123af4e331cf61c0324cd43cbc2135d","accountId":"13590404631"}
data = json.dumps(data)
res=self.httpPost(data,url)
return res
def getAccesssToken(self):
res=self.getLoginUserMess()
return res[‘data‘][‘accessToken‘]
def getUserId(self):
res=self.getLoginUserMess()
return res[‘data‘][‘userId‘]
def getRefreshToken(self):
res=self.getLoginUserMess()
return res[‘data‘][‘refreshToken‘]
def getAfterUrl(self):
userMess=self.getLoginUserMess()
afterUrl="?access_token ="+userMess[‘data‘][‘accessToken‘]+"&userId ="+userMess[‘data‘][‘userId‘]
return afterUrl
python+request+Excel做接口自動化測試(二)