接口自动化测试2---线性脚本

接口自动化测试2---线性脚本,第1张

根据接口文档,目前使用python 的编写线性脚本来验证下那几个接口

第一:创建token

# -*- coding: utf-8 -*-
import requests
# post和get的参数设置如下
# get(url, params=None, **kwargs):
# post(url, data=None, json=None, **kwargs):

# 第一:创建token,请求方式是post
url1="https://restful-booker.herokuapp.com/auth"
data1={"username":"admin",
       "password":"password123"}
#返回数据
rep1=requests.post(url1,data1)
print(rep1.json())

执行打印返回数据:与接口文档反馈参数一致

第二:获取预定的ID

# -*- coding: utf-8 -*-
import requests
# post和get的参数设置如下
# get(url, params=None, **kwargs):
# post(url, data=None, json=None, **kwargs):

# 获取ID,请求方式get
url2="https://restful-booker.herokuapp.com/booking"
# 请求参数都是可选的,可以就可以不用写值,不输入参数就行
data2={"firstname":"",
       "lastname":"",
       "checkin":"",
       "checkout":""}
# 返回数据
rep2=requests.get(url2)
print(rep2.json())

 第三:获取特定ID的预定信息

# -*- coding: utf-8 -*-
import requests
# post和get的参数设置如下
# get(url, params=None, **kwargs):
# post(url, data=None, json=None, **kwargs):

# 获取预定ID,请求方式get
data3 ={"id":"1"}
url3="https://restful-booker.herokuapp.com/booking/"+data3["id"]+""
# 返回数据
rep3 =requests.get(url3)
print(rep3.json())

 第四:创建预定信息

# -*- coding: utf-8 -*-
import requests
# post和get的参数设置如下
# get(url, params=None, **kwargs):
# post(url, data=None, json=None, **kwargs):

# 创建预定信息,请求方式post
url4="https://restful-booker.herokuapp.com/booking"
data4={
    "firstname" : "ceshi",
    "lastname" : "jiekou",
    "totalprice" : 222,
    "depositpaid" : "true",
    "bookingdates" : {
        "checkin" : "2019-01-01",
        "checkout" : "2022-01-01"
    },
    "additionalneeds" : "Breakfast"}
# 返回数据,
rep4 =requests.post(url4,json=data4)
print(rep4.json())

第五点:更新

# -*- coding: utf-8 -*-
import requests
# post和get的参数设置如下
# get(url, params=None, **kwargs):
# post(url, data=None, json=None, **kwargs):
# put(url, data=None, **kwargs):
# 更新预定消息,请求方式put
# 第一:创建token,请求方式是post
url1="https://restful-booker.herokuapp.com/auth"
data1={"username":"admin",
       "password":"password123"}
#返回数据
rep1=requests.post(url1,data1)
url5="https://restful-booker.herokuapp.com/booking/1"
data6={
    "firstname" : "xiaoxi",
    "lastname" : "genxing",
    "totalprice" : 224,
    "depositpaid" : "true",
    "bookingdates" : {
        "checkin" : "2022-01-01",
        "checkout" : "2022-04-21"
    },
    "additionalneeds" :"dinner"}
# 返回数据,
headers={"Cookie": "token=" + rep1.json()["token"]}
rep5 = requests.put(url5,json=data6,headers=headers)
print(rep5.status_code)
print(rep5.json())

 第六点,局部更新:

# put(url, data=None, **kwargs):
# 更新预定消息,请求方式put
# 第一:创建token,请求方式是post
url1="https://restful-booker.herokuapp.com/auth"
data1={"username":"admin",
       "password":"password123"}
#返回数据
rep1=requests.post(url1,data1)
url5="https://restful-booker.herokuapp.com/booking/3"
data6={
    "firstname" : "xiaoxi33",
    "lastname" : "genxing33"}
# 返回数据,
headers={"Cookie": "token=" + rep1.json()["token"]}
rep5 = requests.patch(url5,json=data6,headers=headers)
print(rep5.status_code)
print(rep5.json())

 

 

第7:删除预定信息,这里注意下要cookie信息,才能访问

# -*- coding: utf-8 -*-
import requests

# 第一:创建token,请求方式是post
url1="https://restful-booker.herokuapp.com/auth"
data1={"username":"admin",
       "password":"password123"}
#返回数据token
rep1=requests.post(url1,data1)
#删除的请求方式和url
url8="https://restful-booker.herokuapp.com/booking/1"
headers = {"Cookie":"token="+rep1.json()["token"]}
rep6 =requests.delete(url8,headers=headers)
print(rep6.status_code)
print(rep6.text)

 

 

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/725950.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-26
下一篇2022-04-26

发表评论

登录后才能评论

评论列表(0条)

    保存