Python接口自动化实战 ( 第一阶段) - 封装接口请求类和异常处理
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python接口自动化实战 ( 第一阶段) - 封装接口请求类和异常处理,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2819字,纯文字阅读大概需要5分钟。
内容图文
1.封装http接口请求
已经实现了一个简单的接口请求,接下来就要考虑封装这个请求,在后面的用例中,只需要传递参数(URL ,Params,cookie,heade,method 等)每次去调用这个请求类,
然后根据接口的请求类型来调用相应的处理,如果是get方式就调用get方法,如果是post方式就调用post方法,经过这样的设计后,测试方法中的代码结构会更加清晰有层次,也更容易维护。
代码实现:
# 导入requests包 import requests class HttpRequest: def http_request(self, url, params, http_method): res = '' if http_method.upper()=='POST': try: res=requests.post(url,params) print("正在进行post请求") except Exception as e: print("post请求出现了异常:{0}".format(e)) elif http_method.upper()=='GET': try: res=requests.post(url,params) print("正在进行get请求") except Exception as e: print("get请求出现了异常:{0}".format(e)) return res if __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())
执行结果:
4.TestCase调用封装好的http请求,并添加异常处理。
在运行用例的时候,发现无论断言成功与否,测试报告都是全部通过,后面发现是在处理异常的时候,没有将异常抛出。
以下是test_register.py 修改后的代码:
# 导入 import unittest import requests from Common.http_request import HttpRequest class TestRegister (unittest.TestCase): # 类必须以Test开头,继承TestCase url = 'http://27.154.55.14:8180/api/fcb2bcrm/webRegister' def setUp(self): print("======开始执行测试用例======") def tearDown(self): print("======测试用例执行完毕======") # 测试用例 - 正常注册 def test_register_normal(self): # 每一条测试用例以test_开头 # 发送请求 params = {'LoginAccount': 'apitest09@emai.com', 'Password': '123456', 'Type': 'Pro'} # res = requests.post(self.url,params) res = HttpRequest().http_request(self.url,params,'post') # 断言:根据实际测试场景,可以查询数据库是否有新注册的用户、对比接口的返回信息、对比状态码等等 try: self.assertEqual(200, res.status_code) print('成功测试用户:{}'.format(params['LoginAccount'])) except AssertionError as e: print('Failed') raise e # 注意一定要抛出异常 # 测试用例 - 重复注册 def test_register_existing(self): # 发送请求 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("执行结果:pass:", res.json()['Message']) except AssertionError as e: print('执行结果:Failed') raise e # 测试用例 - 无效的邮箱格式去注册 def test_register_invalid_email(self): # 发送请求 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("执行结果:pass:", res.json()['Message']) except AssertionError as e: print('执行结果:Failed') raise e
内容总结
以上是互联网集市为您收集整理的Python接口自动化实战 ( 第一阶段) - 封装接口请求类和异常处理全部内容,希望文章能够帮你解决Python接口自动化实战 ( 第一阶段) - 封装接口请求类和异常处理所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。