基于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请求实战:

# !/usr/bin python3                

# encoding: utf-8 -*-              

# @author: 沙陌 微信:Matongxue_2

# @Time:2021/3/25 9:54 

# @Copyright:北京码同学网络科技有限公司



import  requests 



host='http://10.0.0.18:8080'

def get():

  """

get接口请求

:return:

"""

  url =host+'/pinter/com/getSku' #接口地址

  params={

    'id':1

}

  resp = requests.get(url,params=params)

  status_code=resp.status_code #获取响应状态码

  print('响应状态码:{}'.format(status_code))

  text=resp.text #获取响应内容,结果类型是字符串

  print('响应内容:{}'.format(text))

  json=resp.json() #获取响应内容,结果是字典类型

  print('响应内容:{}'.format(json))

  resp_headers=resp.headers #获取响应headers

  print('响应header:{}'.format(resp_headers))



if__name__=='__main__':

 get()

 结果如下:

D:\Python\Python36\python.exe D:/pycharmprojects/first/requetsstudy/pinter.py

响应状态码:200 

响应内容:{"code":"0","message":"success","data":{"skuId":1,"skuName":"ptest-1","price":"645","stock":709,"brand":"testfan"}}

<class'dict'>

响应内容:{'code':'0','message':'success','data':{'skuId':1,'skuName':'ptest-1','price':'645','stock':709,'brand':'testfan'}}

响应header:{'Content-Type':'application/json;charset=UTF-8','Transfer-Encoding':'chunked','Date':'Fri,12Mar202122:13:49GMT','Keep-Alive':

'timeout=20','Connection':'keep-alive'}



Process finished with exit code 0

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

2、post请求实战

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

第一种:表单形式的参数

import requests 



host = 'http://10.0.0.18:8080'

def post():

"""

post表单

:return:

"""

    url=host+'/pinter/com/login'

    #表单参数

   data={

       'userName':'沙陌',

       'password':'123456'

     }



resp=requests.post(url=url,data=data)

status_code=resp.status_code#获取响应状态码

print('响应状态码:{}'.format(status_code))

text=resp.text#获取响应内容,结果类型是字符串

print('响应内容:{}'.format(text))

json=resp.json()#获取响应内容,结果是字典类型

print('响应内容:{}'.format(json))

resp_headers=resp.headers#获取响应headers 

print('响应header:{}'.format(resp_headers))

 第二种:json格式参数

import requests



host='http://10.0.0.18:8080'

def post_json():

 """

postjson

:return:

"""

  url =host+'/pinter/com/register'

  #header里定义参数类型

  headers={

    'Content-Type':'application/json'

}

  #json参数

  json={

    "userName":"沙陌",

    "password":"1234",

    "gender":1,

    "phoneNum":"110",

    "email":"beihe@163.com",

    "address":"Beijing"

}

  resp=requests.post(url=url,json=json)

  status_code=resp.status_code #获取响应状态码

 print('响应状态码:{}'.format(status_code))

  text=resp.text #获取响应内容,结果类型是字符串

  print('响应内容:{}'.format(text))

  json=resp.json() #获取响应内容,结果是字典类型

 print('响应内容:{}'.format(json))

  resp_headers=resp.headers #获取响应headers

  print('响应header:{}'.format(resp_headers))

3、put接口实战

import requests



host='http://10.0.0.18:8080'

def put():

 """

put 清酒

:return:

"""

  url = host+'/pinter/com/phone' #接口地址

  #参数

  json={

    "brand":"Huawei",


    "memorySize":"64G",

    "cpuCore":"8核",

    "price":"8848",

    "desc":"全新上市"

}

  resp=requests.put(url=url,json=json)

  status_code=resp.status_code #获取响应状态码

  print('响应状态码:{}'.format(status_code))

  text=resp.text #获取响应内容,结果类型是字符串

  print('响应内容:{}'.format(text))

  json=resp.json() #获取响应内容,结果是字典类型

  print('响应内容:{}'.format(json))

  resp_headers=resp.headers #获取响应headers

  print('响应header:{}'.format(resp_headers))

4、delete请求
在这里插入图片描述

5、request.session.request用法

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

结果如下:

D: \Python\Python36 \py thon. exeD: /pycharmprojects/first/requetsstudy/pinter.py
响应状态码: 200
响应内容:{"code":"0", "message": "success", "data": "$22; 378,198"}
Process finished with exitcode 0

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

结果如下:

D:\Python\Python36\python.exeD:/pycharmprojects/first/requetsstudy/pinter1.py

响应状态码:200 

响应内容:{"code":"0","message":"success","data":"$74,780,457"}

Process finished with exit code 0

总结一下:

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

查询参数就用 params=params

表单参数就用 data=data

json参数就用 json=json

请求头信息header就用 headers=headers

最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:【文末领取】


              【下面是我整理的2023年最全的软件测试工程师学习知识架构体系图】


一、Python编程入门到精通

二、接口自动化项目实战 

三、Web自动化项目实战


四、App自动化项目实战 

五、一线大厂简历


六、测试开发DevOps体系 

七、常用自动化测试工具

八、JMeter性能测试 

九、总结(文末尾部小惊喜)

生命不息,奋斗不止。每一份努力都不会被辜负,只要坚持不懈,终究会有回报。珍惜时间,追求梦想。不忘初心,砥砺前行。你的未来,由你掌握!

生命短暂,时间宝贵,我们无法预知未来会发生什么,但我们可以掌握当下。珍惜每一天,努力奋斗,让自己变得更加强大和优秀。坚定信念,执着追求,成功终将属于你!

只有不断地挑战自己,才能不断地超越自己。坚持追求梦想,勇敢前行,你就会发现奋斗的过程是如此美好而值得。相信自己,你一定可以做到!   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值