当前位置:   article > 正文

接口自动化测试之request模块讲解,以及初步接口自动化测试框架封装(统一请求)_自动化测试用例统一请求封装

自动化测试用例统一请求封装

一、有接口测试工具的情况下,为什么要做接口自动化?

1.敏捷开发,接口一般数量很大,团队实现接测试,版本控制。
2.功能太死板,有些接口完全无法实现(复杂的加密接口,签名接口等)
3.接口项目当中有多种不同协议的接口。
4.排错,定位接口问题不方便,结合抓包实现。
5.没有办法生成美观的报告。
6.多接口串联,数据库验证,日志监控。
7.有些公司做web自动化+接口自动化。

二、python+requests模块

requests第三方库,主要用于发送http请求,做接口自动化。

pip install requests

三、requests全局观请求

1.requests.get()       发送get请求
2.requests.post()     发送post请求
3.requests.delete()  发送delete请求
4.requests.put()       发送put请求
5.requests.request() 最核心的方法

响应:response对象

  1. rep=requests.request()
  2. #返回字符串的数据
  3. print(rep.text)
  4. # 返回字节格式的数据
  5. print(rep.content)
  6. # 返回字典格式的数据
  7. print(rep.json())
  8. # 返回状态信息
  9. print(rep.reason)
  10. #返回cookie信息
  11. print((rep.cookies))

四、接口实战

pytest默认规则

1.py文件必须以test_开头或者_test结尾。
2.类名必须以Test开头
3.测试用例必须以test_开头

注意︰

1、get请求通过params传递参数。

2、post请求通过json或者data传参。

json或者data传参,他们的区别是什么?

data
①数据报文:dict字典类型,那么默认情况下,请求头: applilcation/x-www-form-urlencoded,表示以form表单的方式传参,格式: a=1&b=2&c=3
②数据报文∶str类型,那么默认情况下,请求头:text/plain(如果是字典格式需要转换成dict格式传参)
json
①数据报文∶不管是dict还是str类型,默认都是application/json,格式:{"a":1,"b":2}

json.dumps(data)      序列化把字典格式的数据转换成str格式。
json.loads(data)        反序列化把str格式转换成字典格式

总结:data只能传简单的只有键值对的dict或者是str格式,json一般只能传dict格式

cookie鉴权:网页的接口基本上都要做cookie鉴权。这种不常用。
通过session实现cookie鉴权,这种常用。

  1. import json
  2. import re
  3. import time
  4. import pytest
  5. import requests
  6. class TestsendRequest:
  7. # 类变量:通过类名访问
  8. access_token = " "
  9. csrf_token = ""
  10. def test_get_token(self):
  11. # 发送get请求ctrl+左键
  12. url = "https://api.weixin.gq.com/cgi-bin/token"
  13. data = {
  14. "grant_type": "client_credential",
  15. "appid": "wx6b11b3efd1cdc290",
  16. "secret": "106a9c6157c4db5f6029918738f9529d"
  17. }
  18. rep = requests.get(url=url, params=data)
  19. print(rep.json())
  20. TestsendRequest.access_token = rep.json()['access_token']
  21. def test_edit_flag(self):
  22. # 发送post请求(data和json只需要传一个,下面举例两种)
  23. url = "https://api.weixin.qq.com/cgi-bin/tags/update?acess_token=" + TestsendRequest.access_token + ""
  24. data = {"tag": {"id": 134, "name": "广东人"}}
  25. #因为上面是字典的形式,所以使用json的方式进行传参
  26. rep = requests.post( url, json=data)
  27. #如果是使用data的形式,就得将字典转换成str格式进行传参
  28. # rep=TestsendRequest.session.request(url,data=json.dump(data))
  29. #如果是简单的字典(如只有一层的简单的键值对),也是可以使用data的形式.如:
  30. # data={"tag":""}
  31. # rep=TestsendRequest.session.request((url,data=data))
  32. print(rep.json)
  33. def test_file_upload(self):
  34. url = "https://api.weixin.qq.com/cgi-bin/media/uploading?acess_token=" + TestsendRequest.access_token + ""
  35. data = {
  36. "media": open(r"E:\shu.png", "rb")
  37. }
  38. rep = requests.post(url=url, files=data)
  39. #也可以这样
  40. # rep = requests.request("post", url=url, files=data)
  41. print(rep.json())
  42. # 需要带请求头的接口以及需要cookie关联的接口如何测试?
  43. def test_start(self):
  44. # 发送get请求ctrl+左键
  45. url = "http://47.107.116.139/phpwind"
  46. rep = requests.get(url=url)
  47. # print(rep.text)
  48. # 通过正则表达式获取鉴权码
  49. TestsendRequest.csrf_token = re.search('name="csrf_token"value="(.*?)"', rep.text)[1]
  50. print(TestsendRequest.csrf_token)
  51. TestsendRequest.cks = rep.cookies
  52. # 请求需要带请求头的接口
  53. def test_login(self):
  54. url = "http://47.107.116.139/phpwind/index.php?m=u&c=login&a=dorun"
  55. data = {
  56. "username": "msxy",
  57. "password": "msxy",
  58. "csrf_token": TestsendRequest.csrf_token,
  59. "backurl": "http://47.107.116.139/phpwind/",
  60. "invite": ""
  61. }
  62. headers = {
  63. "Accept": "application/json,text/javascript,/;q=0.01",
  64. "X-Requested-With": "XMLHttpRequest"
  65. }
  66. time.sleep(4)
  67. rep = requests.post( url, data=data, headers=headers, cookies=TestsendRequest.cks)
  68. print(rep.json)
  69. if __name__ == '__main__':
  70. pytest.main(['-vs'])

五、接口自动化测试框架封装

接口自动化框架封装的第一步:统一请求方式。

将上述的代码进行框架封装,如下所示:

  1. import json
  2. import re
  3. import time
  4. import pytest
  5. import requests
  6. class TestsendRequest:
  7. # 类变量:通过类名访问
  8. access_token = " "
  9. csrf_token = ""
  10. session = requests.session()
  11. def test_get_token(self):
  12. # 发送get请求ctrl+左键
  13. url = "https://api.weixin.gq.com/cgi-bin/token"
  14. data = {
  15. "grant_type": "client_credential",
  16. "appid": "wx6b11b3efd1cdc290",
  17. "secret": "106a9c6157c4db5f6029918738f9529d"
  18. }
  19. rep = TestsendRequest.session.request("get", url=url, params=data)
  20. print(rep.json())
  21. TestsendRequest.access_token = rep.json()['access_token']
  22. def test_edit_flag(self):
  23. # 发送post请求(data和json只需要传一个,下面举例两种)
  24. url = "https://api.weixin.qq.com/cgi-bin/tags/update?acess_token=" + TestsendRequest.access_token + ""
  25. data = {"tag": {"id": 134, "name": "广东人"}}
  26. #因为上面是字典的形式,所以使用json的方式进行传参
  27. rep = TestsendRequest.session.request("post", url, json=data)
  28. #如果是使用data的形式,就得将字典转换成str格式进行传参
  29. # rep=TestsendRequest.session.request(url,data=json.dump(data))
  30. #如果是简单的字典(如只有一层的简单的键值对),也是可以使用data的形式.如:
  31. # data={"tag":""}
  32. # rep=TestsendRequest.session.request((url,data=data))
  33. print(rep.json)
  34. def test_file_upload(self):
  35. url = "https://api.weixin.qq.com/cgi-bin/media/uploading?acess_token=" + TestsendRequest.access_token + ""
  36. data = {
  37. "media": open(r"E:\shu.png", "rb")
  38. }
  39. rep = TestsendRequest.session.request("post", url=url, files=data)
  40. #也可以这样
  41. # rep = requests.request("post", url=url, files=data)
  42. print(rep.json())
  43. # 需要带请求头的接口以及需要cookie关联的接口如何测试?
  44. def test_start(self):
  45. # 发送get请求ctrl+左键
  46. url = "http://47.107.116.139/phpwind"
  47. rep = TestsendRequest.session.request("get", url=url)
  48. # print(rep.text)
  49. # 通过正则表达式获取鉴权码
  50. TestsendRequest.csrf_token = re.search('name="csrf_token"value="(.*?)"', rep.text)[1]
  51. print(TestsendRequest.csrf_token)
  52. # TestsendRequest.cks = rep.cookies
  53. # 请求需要带请求头的接口
  54. def test_login(self):
  55. url = "http://47.107.116.139/phpwind/index.php?m=u&c=login&a=dorun"
  56. data = {
  57. "username": "msxy",
  58. "password": "msxy",
  59. "csrf_token": TestsendRequest.csrf_token,
  60. "backurl": "http://47.107.116.139/phpwind/",
  61. "invite": ""
  62. }
  63. headers = {
  64. "Accept": "application/json,text/javascript,/;q=0.01",
  65. "X-Requested-With": "XMLHttpRequest"
  66. }
  67. time.sleep(4)
  68. rep = TestsendRequest.session.request("post", url, data=data, headers=headers, cookies=TestsendRequest.cks)
  69. print(rep.json)
  70. if __name__ == '__main__':
  71. pytest.main(['-vs'])


 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/735406
推荐阅读
相关标签
  

闽ICP备14008679号