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

Posted 拉努斯石

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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做接口自动化测试的主要内容,如果未能解决你的问题,请参考以下文章

python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告

Python+requests+unittest+excel实现接口自动化测试框架

Python+requests+unittest+excel实现接口自动化测试框架

python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告

Python+requests+unittest+excel实现接口自动化测试框架(摘录)

python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告(优化版)