日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

Python脚本做接口测试,抛弃接口测试工具是否可行?(二)

發(fā)布時(shí)間:2025/3/15 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python脚本做接口测试,抛弃接口测试工具是否可行?(二) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

學(xué)習(xí)是為了更好的應(yīng)用,之前做接口測(cè)試一直用的postman、fiddler,感覺(jué)用工具還是有些局限性,于是想著把學(xué)到的python靈活運(yùn)用到接口測(cè)試中,于是就有了以下案例,思考著想用unittest做個(gè)簡(jiǎn)單的接口測(cè)試腳本,可以支持單傳參和多傳參,平時(shí)工作中可以隨時(shí)使用,不但方便,還可以鞏固到學(xué)習(xí)的知識(shí),。

思考點(diǎn):

1、腳本支持單傳參和多傳參,利用了for循環(huán)和ddt實(shí)現(xiàn)

2、接口headers里需傳入登錄后的token值,利用了unittest每次運(yùn)行用例時(shí)先執(zhí)行setup初始化好headers

#unittest+ddt實(shí)現(xiàn)的接口測(cè)試腳本import requests,json,unittest,xlrd,os from ddt import ddt,data,unpack import requests@ddt class Test(unittest.TestCase):zp_url = 'http://test.com' # 域名params = [{"self_introduce": "我是參數(shù)1!"}, {"self_introduce": "我是參數(shù)3!"}] # 接口參數(shù)@classmethoddef setUpClass(cls):cls.headers = {"Content-Type": "application/json", "Authorization": "token"} # 請(qǐng)求頭cls.zp_login_url = '/login' # 登錄接口地址cls.zp_login_param = {"captcha": "1234", "client_type": 2, "login_type": 1, "mobile": 18221124103} # 登錄傳參try:res = requests.post(url=cls.zp_url + cls.zp_login_url, headers=cls.headers, json=cls.zp_login_param)cls.headers['Authorization']=res.json()['data']['token']except Exception:print('參數(shù)錯(cuò)誤')cls.url = '/update_user_info' # 接口地址cls.method='POST'@classmethoddef tearDownClass(cls):passdef sendGet(self,api_url,param):try:res=requests.get(url=api_url,headers=self.headers).json()print(res)except Exception:print('參數(shù)錯(cuò)誤')def sendPost(self,api_url,param):try:res = requests.post(url=api_url, headers=self.headers, json=param).json()print(res)except Exception:print('參數(shù)錯(cuò)誤')@data(*params)def test(self,params):if self.method=='GET' or self.method=='get' or self.method=='Get':self.sendGet(self.zp_url+self.url,params)elif self.method== 'POST' or self.method=='post' or self.method=='Post':self.sendPost(self.zp_url+self.url,params)else:print('請(qǐng)傳正確請(qǐng)求方式GET或POST') if __name__== '__main__':unittest.main()"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test.py {'code': 0, 'data': {}, 'msg': '成功'} .. ---------------------------------------------------------------------- {'code': 0, 'data': {}, 'msg': '成功'} Ran 2 tests in 0.462sOKProcess finished with exit code 0 #unittest實(shí)現(xiàn)的接口測(cè)試腳本import requests,json,unittest,xlrd,os from ddt import ddt,data,unpack import requests,json @ddt class Test(unittest.TestCase):zp_url = 'http://test.com' # 域名@classmethoddef setUpClass(cls):cls.headers = {"Content-Type": "application/json", "Authorization": "token"} # 請(qǐng)求頭cls.zp_login_url = '/login' # 登錄接口地址cls.zp_login_param = {"captcha": "1234", "client_type": 2, "login_type": 1, "mobile": 18221124103} # 熟仁直聘登錄傳參try:res = requests.post(url=cls.zp_url + cls.zp_login_url, headers=cls.headers, json=cls.zp_login_param)cls.headers['Authorization']=res.json()['data']['token']except Exception:print('參數(shù)錯(cuò)誤')cls.url = '/update_user_info' # 接口地址cls.params = [{"self_introduce": "我是參數(shù)1!"}, {"self_introduce": "我是參數(shù)3!"}] # 接口參數(shù)cls.method='POST'@classmethoddef tearDownClass(cls):passdef sendGet(self,api_url,param):try:res=requests.get(url=api_url,headers=self.headers).json()print(res)except Exception:print('參數(shù)錯(cuò)誤')def sendPost(self,api_url,param):try:res = requests.post(url=api_url, headers=self.headers, json=param).json()print(res)except Exception:print('參數(shù)錯(cuò)誤')def test(self):for param in self.params:if self.method=='GET' or self.method=='get' or self.method=='Get':self.sendGet(self.zp_url+self.url,param)elif self.method== 'POST' or self.method=='post' or self.method=='Post':self.sendPost(self.zp_url+self.url,param)else:print('請(qǐng)傳正確請(qǐng)求方式GET或POST') if __name__== '__main__':unittest.main()"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test.py {'data': {}, 'code': 0, 'msg': '成功'} . ---------------------------------------------------------------------- Ran 1 test in 0.515sOK {'data': {}, 'code': 0, 'msg': '成功'}Process finished with exit code 0

?

總結(jié)

以上是生活随笔為你收集整理的Python脚本做接口测试,抛弃接口测试工具是否可行?(二)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。