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

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

01、requests的安装

windows下执行如下命令:

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

mac终端下执行如下命令:

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

02、常用方法

在这里插入图片描述
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对象的常用方法如下:
在这里插入图片描述

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请求
在这里插入图片描述

5、request.session.request用法

可以自动管理cookie,比如如下需要采用cookie认证的接口
在这里插入图片描述
在这里插入图片描述

结果如下:

 
  1. D: \Python\Python36 \py thon. exeD: /pycharmprojects/first/requetsstudy/pinter.py

  2. 响应状态码: 200

  3. 响应内容:{"code":"0", "message": "success", "data": "$22; 378,198"}

  4. Process finished with exitcode 0

 6、token关联的接口如何做呢?
在这里插入图片描述
对于需要token关联的接口来说,需要从登录接口的返回值中提取token信息,并传递给需要token的接口
在这里插入图片描述

结果如下:

 
  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

 

感谢每一个认真阅读我文章的人!!!

作为一位过来人也是希望大家少走一些弯路,如果你不想再体验一次学习时找不到资料,没人解答问题,坚持几天便放弃的感受的话,在这里我给大家分享一些自动化测试的学习资源,希望能给你前进的路上带来帮助。

软件测试面试文档

我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。

 

          视频文档获取方式:
这份文档和视频资料,对于想从事【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!以上均可以分享,点下方小卡片即可自行领取。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值