基于requests框架实现接口自动化测试项目实战

逃离我推掉我的手 2023-09-24 15:34 134阅读 0赞

requests库是一个常用的用于http请求的模块,它使用python语言编写,在当下python系列的接口自动化中应用广泛,本文将带领大家深入学习这个库,Python环境的安装就不在这里赘述了,我们直接开干。

01 requests的安装

windows下执行如下命令:

  1. pip install requests -i http://pypi.douban.com/simple/--trust-host pypi.douban.com

mac终端下执行如下命令:

  1. python3 -m pip install requests -i http://pypi.douban.com/simple/--trust-host pypi.douban.com

02 常用方法

74e5de84c0aad6924a3dd359e36fe84b.png

1、get请求实战:

  1. # !/usr/bin python3
  2. # encoding: utf-8 -*-
  3. # @author: 沙陌 微信:Matongxue_2
  4. # @Time:2021/3/25 9:54
  5. # @Copyright:北京码同学网络科技有限公司
  6. import requests
  7. host='http://10.0.0.18:8080'
  8. def get():
  9. """
  10. get接口请求
  11. :return:
  12. """
  13. url =host+'/pinter/com/getSku' #接口地址
  14. params={
  15. 'id':1
  16. }
  17. resp = requests.get(url,params=params)
  18. status_code=resp.status_code #获取响应状态码
  19. print('响应状态码:{}'.format(status_code))
  20. text=resp.text #获取响应内容,结果类型是字符串
  21. print('响应内容:{}'.format(text))
  22. json=resp.json() #获取响应内容,结果是字典类型
  23. print('响应内容:{}'.format(json))
  24. resp_headers=resp.headers #获取响应headers
  25. print('响应header:{}'.format(resp_headers))
  26. if__name__=='__main__':
  27. get()

结果如下:

  1. D:\Python\Python36\python.exe D:/pycharmprojects/first/requetsstudy/pinter.py
  2. 响应状态码:200
  3. 响应内容:{"code":"0","message":"success","data":{"skuId":1,"skuName":"ptest-1","price":"645","stock":709,"brand":"testfan"}}
  4. <class'dict'>
  5. 响应内容:{'code':'0','message':'success','data':{'skuId':1,'skuName':'ptest-1','price':'645','stock':709,'brand':'testfan'}}
  6. 响应header:{'Content-Type':'application/json;charset=UTF-8','Transfer-Encoding':'chunked','Date':'Fri,12Mar202122:13:49GMT','Keep-Alive':
  7. 'timeout=20','Connection':'keep-alive'}
  8. Process finished with exit code 0

上述代码中请求发起后得到一个响应对象变量resp,那么resp对象的常用方法如下:

11cb4ac856ce2ddf8975b1fd68add0d2.png

2、post请求实战

post请求的参数格式通常有多种,我们依次学习

第一种:表单形式的参数

  1. import requests
  2. host = 'http://10.0.0.18:8080'
  3. def post():
  4. """
  5. post表单
  6. :return:
  7. """
  8. url=host+'/pinter/com/login'
  9. #表单参数
  10. data={
  11. 'userName':'沙陌',
  12. 'password':'123456'
  13. }
  14. resp=requests.post(url=url,data=data)
  15. status_code=resp.status_code#获取响应状态码
  16. print('响应状态码:{}'.format(status_code))
  17. text=resp.text#获取响应内容,结果类型是字符串
  18. print('响应内容:{}'.format(text))
  19. json=resp.json()#获取响应内容,结果是字典类型
  20. print('响应内容:{}'.format(json))
  21. resp_headers=resp.headers#获取响应headers
  22. print('响应header:{}'.format(resp_headers))

第二种:json格式参数

  1. import requests
  2. host='http://10.0.0.18:8080'
  3. def post_json():
  4. """
  5. postjson
  6. :return:
  7. """
  8. url =host+'/pinter/com/register'
  9. #header里定义参数类型
  10. headers={
  11. 'Content-Type':'application/json'
  12. }
  13. #json参数
  14. json={
  15. "userName":"沙陌",
  16. "password":"1234",
  17. "gender":1,
  18. "phoneNum":"110",
  19. "email":"beihe@163.com",
  20. "address":"Beijing"
  21. }
  22. resp=requests.post(url=url,json=json)
  23. status_code=resp.status_code #获取响应状态码
  24. print('响应状态码:{}'.format(status_code))
  25. text=resp.text #获取响应内容,结果类型是字符串
  26. print('响应内容:{}'.format(text))
  27. json=resp.json() #获取响应内容,结果是字典类型
  28. print('响应内容:{}'.format(json))
  29. resp_headers=resp.headers #获取响应headers
  30. print('响应header:{}'.format(resp_headers))

3、put接口实战

  1. import requests
  2. host='http://10.0.0.18:8080'
  3. def put():
  4. """
  5. put 清酒
  6. :return:
  7. """
  8. url = host+'/pinter/com/phone' #接口地址
  9. #参数
  10. json={
  11. "brand":"Huawei",
  12. "memorySize":"64G",
  13. "cpuCore":"8核",
  14. "price":"8848",
  15. "desc":"全新上市"
  16. }
  17. resp=requests.put(url=url,json=json)
  18. status_code=resp.status_code #获取响应状态码
  19. print('响应状态码:{}'.format(status_code))
  20. text=resp.text #获取响应内容,结果类型是字符串
  21. print('响应内容:{}'.format(text))
  22. json=resp.json() #获取响应内容,结果是字典类型
  23. print('响应内容:{}'.format(json))
  24. resp_headers=resp.headers #获取响应headers
  25. print('响应header:{}'.format(resp_headers))

4、delete请求

9b1643f6c172ca0c1339a05e8664a094.png

5、request.session.request用法

可以自动管理cookie,比如如下需要采用cookie认证的接口

9f7a1b41f95dbe9e5ce9f7b67892189e.png

342f04f417143fa1e216b09fdf6dd652.png

结果如下:

  1. D:\Python\Python36\python.exeD:/pycharmprojects/first/requetsstudy/pinter.py
  2. 响应状态码:200
  3. Process finished with exitcode 0

6、token关联的接口如何做呢?

6ac92e2e63e97a15de1a3ebd4a15d9d4.png

对于需要token关联的接口来说,需要从登录接口的返回值中提取token信息,并传递给需要token的接口

8453cd3955d068f8d0840977621815f5.png

结果如下:

  1. D:\Python\Python36\python.exeD:/pycharmprojects/first/requetsstudy/pinter1.py
  2. 响应状态码:200
  3. 响应内容:{"code":"0","message":"success","data":"$74,780,457"}
  4. Process finished with exit code 0

总结一下:

requests库的请求方法里参数众多,所以简单划分一下:

  • 查询参数就用 params=params
  • 表单参数就用 data=data
  • json参数就用 json=json
  • 请求头信息header就用 headers=headers

资源分享【这份资料必须领取~】

下方这份完整的软件测试视频学习教程已经上传CSDN官方认证的二维码,朋友们如果需要可以自行免费领取 【保证100%免费】

在这里插入图片描述

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,134人围观)

还没有评论,来说两句吧...

相关阅读