Python接口自动化实战 ( 第一阶段) - 封装接口请求类和异常处理
生活随笔
收集整理的這篇文章主要介紹了
Python接口自动化实战 ( 第一阶段) - 封装接口请求类和异常处理
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1.封裝http接口請求
已經(jīng)實(shí)現(xiàn)了一個(gè)簡單的接口請求,接下來就要考慮封裝這個(gè)請求,在后面的用例中,只需要傳遞參數(shù)(URL ,Params,cookie,heade,method 等)每次去調(diào)用這個(gè)請求類,
然后根據(jù)接口的請求類型來調(diào)用相應(yīng)的處理,如果是get方式就調(diào)用get方法,如果是post方式就調(diào)用post方法,經(jīng)過這樣的設(shè)計(jì)后,測試方法中的代碼結(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)行用例的時(shí)候,發(fā)現(xiàn)無論斷言成功與否,測試報(bào)告都是全部通過,后面發(fā)現(xiàn)是在處理異常的時(shí)候,沒有將異常拋出。
以下是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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 字符串反序,逆序输出字符串
- 下一篇: Java基础相关