码迷,mamicode.com
首页 > 编程语言 > 详细

Python加requests框架实现接口自动化测试

时间:2021-05-23 23:39:42      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:info   框架   sage   art   alt   poi   log   res   customer   

找了一个不需要验证码登录的网站,康爱多网上药店,第一次研究Python+requests框架实现接口自动化,选择登录-加购-下单-取消订单-删除订单一条业务链五个接口测试,代码如下:

import requests
import time

# 登录
session = requests.session()
body = {
‘userName‘: ‘xxxxxxxxxxx‘,
‘pass‘: ‘xxxxxxxxx‘
}
login = session.post(‘https://user.360kad.com/Login/AjaxLoginV2‘, data=body)
print(login.text)
# 加入购物车
cartparam = {
‘id‘: 1406,
‘quantity‘: 1,
‘buyType‘: 0,
‘sellerCode‘: ‘‘,
‘rd‘: 0.7634724674063651
}
cart = session.get(‘https://www.360kad.com/cart/AddCart‘, params=cartparam)
print(cart.text)
# 提交订单
orderdata = {
‘AddressId‘: 11710216491,
‘PayType‘: 1,
‘InvoiceType‘: 0,
‘InvoiceTitle‘: ‘‘,
‘InvoiceCustomerType‘: 1,
‘InvoiceDutyParagraph‘: ‘‘,
‘IsInvoice‘: 0,
‘CartType‘: 0,
‘SendTimeType‘: 0,
‘Remark‘: ‘‘,
‘CouponCode‘: ‘‘,
‘PointsAmt‘: 0,
‘SendType‘: 1,
‘IsFreightInsurance‘: ‘false‘,
‘IsExchangeInsurance‘: ‘false‘
}
order = session.post(‘https://cart.360kad.com/Order/CreateOrder‘, data=orderdata)
print(order.text)
time.sleep(3)
# 取消订单
orderid = order.json()[‘Message‘] # 调用上一接口订单编号,变量名为orderid
ordercancel = {
‘orderCode‘: orderid,
‘cancelTypeCode‘: 200005,
‘cancelReason‘: ‘%E4%B8%8D%E6%83%B3%E4%B9%B0%E4%BA%86‘,
‘_‘: 1620053063259
}
cancelorder = session.get(‘https://user.360kad.com/Order/CancelOrder‘, params=ordercancel)
print(cancelorder.text)
time.sleep(2)
# 删除订单
orderdel1 = {‘orderCode‘: orderid}
orderdel = session.get(‘https://user.360kad.com/Order/Del‘, params=orderdel1)
print(orderdel.text)

最终测试结果:

技术图片

 

Python加requests框架实现接口自动化测试

标签:info   框架   sage   art   alt   poi   log   res   customer   

原文地址:https://www.cnblogs.com/zengyu0714/p/14728540.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!