接口測試python腳本
寫了很久了,一直沒發博客,今兒多發幾篇,先上代碼。
#__author__ = 'chubby_superman'
#_*_coding=utf-8 _*
import requests
from datetime import datetime
class Httpreq():
def __init__(self,url,data,heards):
self.url=url
self.data=data
self.heards=heards
def req_1(self):
requests_statuscode=requests.post(url=self.url,json=self.data).status_code
try:
if requests_statuscode == 200:
print(1)
#return "請求成功,開始判斷響應狀態"
else:
#return requests_statuscode
print("請求失敗if內")
except Exception as e:
print("請求失敗try內")
x = requests.post(url=self.url,json=self.data)
try :
a = x.text
with open(datetime.now().strftime('%Y%m%d%H%M%S') + '.txt','w',encoding="utf-8") as f:
f.write(a)
except Exception as e:
print("寫入文件有問題")
a = x.json()["code"]
if a == 1000:
print("正常返回")
elif a == -1:
print("系統繁忙")
elif a == 1001:
print("partnerId無效")
elif a == 1002:
print("IP白名單錯誤,禁止服務")
elif a == 1003 :
print("簽名錯誤")
elif a == 1004 :
print("缺少必要的參數")
else:
print("返回未知的錯誤")
if __name__ == '__main__':
the_url='http://test.pipifit.com/analysisShopCartOverCate'
#接口url
the_data= {'day': 1, 'period': 1, 'goodsInfo': [{"goodsId": 1213,"goodsNumber":1}], 'sex': 0, 'symptoms': 1001}
#請求body
Httpreq(the_url,the_data,'').req_1()
後期如果需要進行接口併發測試,可以將接口的請求抽離出來,將功能驗證去掉,使用協程來做接口併發。