python+request+Excel做介面自動化測試(二)

來源:互聯網
上載者:User

標籤:還需   csv   cep   put   讀取   python   encoding   book   alt   

今天整了下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做介面自動化測試(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.