码迷,mamicode.com
首页 > 编程语言 > 详细

python+request+Excel做接口自动化测试(二)

时间:2018-07-13 01:14:17      阅读:677      评论:0      收藏:0      [点我收藏+]

标签:还需   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做接口自动化测试(二)

标签:还需   csv   cep   put   读取   python   encoding   book   alt   

原文地址:https://www.cnblogs.com/LinxiHuang/p/9302495.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!