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

歡迎訪問 生活随笔!

生活随笔

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

python

Python接口自动化实战 ( 第一阶段) - 封装接口请求类和异常处理

發(fā)布時間:2025/7/25 python 61 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python接口自动化实战 ( 第一阶段) - 封装接口请求类和异常处理 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

1.封裝http接口請求

已經(jīng)實(shí)現(xiàn)了一個簡單的接口請求,接下來就要考慮封裝這個請求,在后面的用例中,只需要傳遞參數(shù)(URL ,Params,cookie,heade,method 等)每次去調(diào)用這個請求類,

然后根據(jù)接口的請求類型來調(diào)用相應(yīng)的處理,如果是get方式就調(diào)用get方法,如果是post方式就調(diào)用post方法,經(jīng)過這樣的設(shè)計后,測試方法中的代碼結(jié)構(gòu)會更加清晰有層次,也更容易維護(hù)。

代碼實(shí)現(xiàn):

# 導(dǎo)入requests包 import requestsclass HttpRequest:def http_request(self, url, params, http_method):res = ''if http_method.upper()=='POST':try:res=requests.post(url,params)print("正在進(jìn)行post請求")except Exception as e:print("post請求出現(xiàn)了異常:{0}".format(e))elif http_method.upper()=='GET':try:res=requests.post(url,params)print("正在進(jìn)行g(shù)et請求")except Exception as e:print("get請求出現(xiàn)了異常:{0}".format(e))return resif __name__ == '__main__':url = 'http://27.154.55.14:8180/api/fcb2bcrm/webRegister'datas = {'LoginAccount': 'testapi01@emai.com', 'Password': '123456', 'Type': 'Pro'}res=HttpRequest().http_request(url, datas, 'post')print(res.json())

執(zhí)行結(jié)果:

4.TestCase調(diào)用封裝好的http請求,并添加異常處理。

在運(yùn)行用例的時候,發(fā)現(xiàn)無論斷言成功與否,測試報告都是全部通過,后面發(fā)現(xiàn)是在處理異常的時候,沒有將異常拋出。

以下是test_register.py 修改后的代碼:

# 導(dǎo)入 import unittest import requests from Common.http_request import HttpRequestclass TestRegister (unittest.TestCase): # 類必須以Test開頭,繼承TestCaseurl = 'http://27.154.55.14:8180/api/fcb2bcrm/webRegister'def setUp(self):print("======開始執(zhí)行測試用例======")def tearDown(self):print("======測試用例執(zhí)行完畢======")# 測試用例 - 正常注冊def test_register_normal(self): # 每一條測試用例以test_開頭# 發(fā)送請求params = {'LoginAccount': 'apitest09@emai.com', 'Password': '123456', 'Type': 'Pro'}# res = requests.post(self.url,params)res = HttpRequest().http_request(self.url,params,'post')# 斷言:根據(jù)實(shí)際測試場景,可以查詢數(shù)據(jù)庫是否有新注冊的用戶、對比接口的返回信息、對比狀態(tài)碼等等try:self.assertEqual(200, res.status_code)print('成功測試用戶:{}'.format(params['LoginAccount']))except AssertionError as e:print('Failed')raise e # 注意一定要拋出異常# 測試用例 - 重復(fù)注冊def test_register_existing(self):# 發(fā)送請求params = {'LoginAccount': 'apitest05@emai.com', 'Password': '123456', 'Type': 'Pro'}# res = requests.post(self.url,params)res = HttpRequest().http_request(self.url, params, 'post')# 斷言try:self.assertIn("The email has been registered", res.json()['Message'])print("執(zhí)行結(jié)果:pass:", res.json()['Message'])except AssertionError as e:print('執(zhí)行結(jié)果:Failed')raise e# 測試用例 - 無效的郵箱格式去注冊def test_register_invalid_email(self):# 發(fā)送請求params = {'LoginAccount': 'testapi@emai', 'Password': '123456', 'Type': 'Pro'}# res = requests.post(self.url,params)res = HttpRequest().http_request(self.url, params, 'post')# 斷言try:self.assertIn("valid email", res.json()['Message'])print("執(zhí)行結(jié)果:pass:", res.json()['Message'])except AssertionError as e:print('執(zhí)行結(jié)果:Failed')raise e

?

轉(zhuǎn)載于:https://www.cnblogs.com/testjiali/p/10415990.html

總結(jié)

以上是生活随笔為你收集整理的Python接口自动化实战 ( 第一阶段) - 封装接口请求类和异常处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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