《一》接口关联讲解
(1)补充流程:商品添加到购物车和提交订单
test_py.py
'''
1.补充流程:商品添加到购物车和提交订单
'''
'''
(1)get请求传参是以params的形式进行传参
(2)post请求传参是以 data 或 json 的形式进行传参
1.传的是json数据,则用json数据
2.传的是表单数据,则用data数据
3.传的是json数据,非要用data数据,则
3.1 字典改成字符串: json.dumps 即:data1 = json.dumps(data)
3.2 加个请求头: header = {"content-type":"application/json"}
3.3 data=data1
'''
import jsonpath
import pytest
import requests
class TestCase:
# 定义类的变量
token=None
openid=None
productid=None
userid=None
cartid=None
# 响应 token号会变
# 1.响应回来的token号提取出来 保存在变量。
# 2.获得token的三种方式:
# 2-1:放在一个类变量中,都可以拿到token
# 2-2:其他方式(session)
# 2-3:返回值
# 登录
'''接口地址-接口参数-发送请求-获得结果'''
'''
接口关联的应用:
1.要用的数据,提取出来,保存在变量中
1-1:postman保存在环境变量中,下个接口要用的时候用{{}}引用
1-2:Pycharm代码保存在类变量(公共变量)中,下个接口要用的时候直接在类变量中去取
'''
def test_login(self):
url='http://39.98.138.157:5000/api/login'
data={"password": "123456","username": "admin"}
res=requests.post(url,json=data)
print(res.json())
# 提取token 保存在变量中了
TestCase.token=jsonpath.jsonpath(res.json(), '$..token')[0]
sjmsg=jsonpath.jsonpath(res.json(),'$..msg')[0]
assert 'success'==sjmsg
# 注意 添加购物车 下单的时候需要用到这里面的userid和openid 这个怎么弄
# 获取个人信息
def test_getUserinfo(self):
url = 'http://39.98.138.157:5000/api/getuserinfo'
# 这个token不能写死 获得登录时的token
header={"token":TestCase.token}
res=requests.get(url,headers=header)
# 获取所有的结果
print(res.json())
'''
1.取openid 和 userid 的值(用jsonpath 或者 字典去取值)
2.jsonpath的语法结构: jsonpath(数据,表达式)
'''
TestCase.openid = jsonpath.jsonpath(res.json(), '$..openid')[0]
TestCase.userid = jsonpath.jsonpath(res.json(), '$..userid')[0]
print('openid:',TestCase.openid)
print('userid:',TestCase.userid)
sjname=jsonpath.jsonpath(res.json(),'$..nikename')[0]
assert '风清扬'==sjname
# 选择商品
def test_shopping(self):
url ='http://39.98.138.157:5000/api/getproductinfo?productid=8888'
res=requests.get(url)
print(res.json())
# 取 productid 的值
TestCase.productid = jsonpath.jsonpath(res.json(), '$..productid')[0]
sjproductid=jsonpath.jsonpath(res.json(),'$..productid')[0]
assert 8888==sjproductid
# 商品添加到购物车
def test_cart(self):
url = 'http://39.98.138.157:5000/api/addcart'
header = {'token':TestCase.token}
'''
1.参数不能写死,不同的用户有不同的userid openid
2.灵活的去取 userid 和 openid 的值
3.怎么去取?答:哪里响应的到哪个地方去取?
'''
data = {"openid": TestCase.openid,"productid": TestCase.productid,"userid": TestCase.userid}
res = requests.post(url,json=data,headers=header)
print(res.json())
# 取 cartid 的值
TestCase.cartid = jsonpath.jsonpath(res.json(), '$..cartid')[0]
# 断言
exmsg = 45233
sjmsg = jsonpath.jsonpath(res.json(),'$..cartid')[0]
assert exmsg == sjmsg
# 提交订单
def test_order(self):
url = 'http://39.98.138.157:5000/api/createorder'
data = {"cartid": TestCase.cartid, "openid": TestCase.openid, "productid": TestCase.productid, "userid": TestCase.userid}
header = {"token": TestCase.token}
res = requests.post(url, json=data, headers=header)
print(res.json())
# 断言
usermsg = 17890
sjmsg = jsonpath.jsonpath(res.json(), '$..userid')[0]
assert usermsg == sjmsg
if __name__ == '__main__':
pytest.main(['-sv','test_py.py'])
(2)返回值返回token
test_py2.py
import jsonpath
import pytest
import requests
'''
2.返回值返回token
'''
'''
1.上个接口的返回值,下个接口想要用 返回值
2.test_login测试方法中
2-1:新增return token
2-2:去掉TestCase.token=jsonpath.jsonpath(res.json(), '$..token')[0]中的TestCase.
3.test_getUserinfo测试方法中
3-1:将header={"token":TestCase.token} 修改为以下二种都可以:
3-2:第一种方法 header={'token':TestCase().test_login()}
3-3:第二种方法 header = {'token': self.test_login()}
'''
class TestCase:
def test_login(self):
url='http://39.98.138.157:5000/api/login'
data={"password": "123456","username": "admin"}
res=requests.post(url,json=data)
print(res.json())
# 提取token 保存在变量中了
token=jsonpath.jsonpath(res.json(), '$..token')[0]
sjmsg=jsonpath.jsonpath(res.json(),'$..msg')[0]
assert 'success'==sjmsg
return token
# 注意 添加购物车 下单的时候需要用到这里面的userid和openid 这个怎么弄
# 获取个人信息,需要用token,方法调用
def test_getUserinfo(self):
url = 'http://39.98.138.157:5000/api/getuserinfo'
# 这个token不能写死 获得登录时的token
# header={"token":TestCase.token}
# 第一种写法
# header={'token':TestCase().test_login()}
# 第二种写法
header = {'token': self.test_login()}
res=requests.get(url,headers=header)
# 获取所有的结果
# print(res.json())
'''
1.取openid 和 userid 的值(用jsonpath 或者 字典去取值)
2.jsonpath的语法结构: jsonpath(数据,表达式)
'''
TestCase.openid = jsonpath.jsonpath(res.json(), '$..openid')[0]
TestCase.userid = jsonpath.jsonpath(res.json(), '$..userid')[0]
print('openid:',TestCase.openid)
print('userid:',TestCase.userid)
sjname=jsonpath.jsonpath(res.json(),'$..nikename')[0]
assert '风清扬'==sjname
if __name__ == '__main__':
pytest.main(['-sv','test_py2.py'])
(3)把token放在seesion中
test_py3.py
'''
3.把token放在seesion中
'''
'''
把token放在seesion中
a.创建session对象
b.设置session的headers属性 token属性
1.test_login测试方法中:
注释: res=requests.post(url,json=data)
加此行: res = TestCase.session.post(url,json=data)
2.TestCase类中:
注释: token=None
加此行: session = requests.session() # 创建session对象
3.test_login测试方法中:
注释: TestCase.token=jsonpath.jsonpath(res.json(), '$..token')[0]
加此行: token=jsonpath.jsonpath(res.json(), '$..token')[0]
4.test_login测试方法中:
把这个token放在session中的headers里面,给头部里面设置属性,用update
加此行: TestCase.session.headers.update({'token':token})
5.test_getUserinfo测试方法中:
注释: header={"token":TestCase.token}
注释: res=requests.get(url,headers=header)
6.test_cart测试方法中:
注释: header={'token':TestCase.token}
注释: res=requests.post(url,json=data,headers=header)
加此行: res=TestCase.session.post(url,json=data)
7.test_order测试方法中:
注释: header={"token": TestCase.token}
注释: res=requests.post(url,json=data,headers=header)
加此行: res=TestCase.session.post(url,json=data)
'''
import jsonpath
import pytest
import requests
class TestCase:
# 定义类的变量
'''
创建session对象,利用requests的session库发送post请求,进入session会话
'''
# token=None
session = requests.session()
openid=None
productid=None
userid=None
cartid=None
# 响应 token号会变
# 1.响应回来的token号提取出来 保存在变量。
# 2.获得token的三种方式:
# 2-1:放在一个类变量中,都可以拿到token
# 2-2:其他方式(session)
# 2-3:返回值
# 登录
'''接口地址-接口参数-发送请求-获得结果'''
'''
接口关联的应用:
1.要用的数据,提取出来,保存在变量中
1-1:postman保存在环境变量中,下个接口要用的时候用{{}}引用
1-2:Pychrome代码保存在类变量(公共变量)中,下个接口要用的时候直接在类变量中去取
'''
def test_login(self):
url='http://39.98.138.157:5000/api/login'
data={"password": "123456","username": "admin"}
# res=requests.post(url,json=data)
res=TestCase.session.post(url,json=data)
print(res.json())
# 提取token 保存在变量中了
# TestCase.token = jsonpath.jsonpath(res.json(), '$..token')[0]
token=jsonpath.jsonpath(res.json(), '$..token')[0]
# 把这个token放在session中的headers里面,给头部里面设置属性,用update
TestCase.session.headers.update({'token':token})
sjmsg=jsonpath.jsonpath(res.json(),'$..msg')[0]
assert 'success'==sjmsg
# 获取个人信息,获得谁的个人信息 拿到token
# 注意 添加购物车 下单的时候需要用到这里面的userid和openid 这个怎么弄
# 获取个人信息
def test_getUserinfo(self):
url = 'http://39.98.138.157:5000/api/getuserinfo'
# 这个token不能写死 获得登录时的token
# header={"token":TestCase.token}
# res=requests.get(url,headers=header)
res = TestCase.session.get(url)
# 获取所有的结果
print(res.json())
'''
1.取openid 和 userid 的值(用jsonpath 或者 字典去取值)
2.jsonpath的语法结构: jsonpath(数据,表达式)
'''
TestCase.openid = jsonpath.jsonpath(res.json(), '$..openid')[0]
TestCase.userid = jsonpath.jsonpath(res.json(), '$..userid')[0]
print('openid:',TestCase.openid)
print('userid:',TestCase.userid)
sjname=jsonpath.jsonpath(res.json(),'$..nikename')[0]
assert '风清扬'==sjname
# 选择商品
def test_shopping(self):
url ='http://39.98.138.157:5000/api/getproductinfo?productid=8888'
res=requests.get(url)
print(res.json())
# 取 productid 的值
TestCase.productid = jsonpath.jsonpath(res.json(), '$..productid')[0]
sjproductid=jsonpath.jsonpath(res.json(),'$..productid')[0]
assert 8888==sjproductid
# 商品添加到购物车
def test_cart(self):
url = 'http://39.98.138.157:5000/api/addcart'
# header={'token':TestCase.token}
'''
1.参数不能写死,不同的用户有不同的userid openid
2.灵活的去取 userid 和 openid 的值
3.怎么去取?答:哪里响应的到哪个地方去取?
'''
data = {"openid": TestCase.openid,"productid": TestCase.productid,"userid": TestCase.userid}
# res=requests.post(url,json=data,headers=header)
res=TestCase.session.post(url,json=data)
print(res.json())
# 取 cartid 的值
TestCase.cartid = jsonpath.jsonpath(res.json(), '$..cartid')[0]
# 断言
exmsg = 45233
sjmsg = jsonpath.jsonpath(res.json(),'$..cartid')[0]
assert exmsg == sjmsg
# 提交订单
def test_order(self):
url = 'http://39.98.138.157:5000/api/createorder'
data = {"cartid": TestCase.cartid, "openid": TestCase.openid, "productid": TestCase.productid, "userid": TestCase.userid}
# header={"token": TestCase.token}
# res=requests.post(url,json=data,headers=header)
res=TestCase.session.post(url,json=data)
print(res.json())
# 断言
usermsg = 17890
sjmsg = jsonpath.jsonpath(res.json(), '$..userid')[0]
assert usermsg == sjmsg
if __name__ == '__main__':
pytest.main(['-v','test_py3.py'])
'''
1.流程优化的地方:get post:有没有一种方式,即能接受get又能接受post请求?(有!针对请求方式进行封装)
'''
《二》接口二次封装设计
(1)针对请求方式进行封装
httpclient.py
'''
1.流程优化的地方:get post:有没有一种方式,即能接受get又能接受post请求?(有!针对请求方式进行封装)
'''
import requests
'''
专门封装geupost请求方式
'''
class HttpClient:
def __init__(self):
# 只要用post,get请求方式,就创建会话
self.session=requests.Session()
'''
Session发送请求:
发送请求的方式(method),
接口地址(url), 接口参数类型(json?from?),
接口数据(data), 头部信息(headers), 其他信息(**kwargs)...
'''
def send_request(self,method,url,param_type=None,data=None,headers=None,**kwargs):
# 请求方式转成大写
method=method.upper()
# 参数类型转成大写
param_type=param_type.upper()
# 判断: 发送的请求方式(get、post、delete、put)
if method == 'GET':
response = self.session.request(method=method, url=url, params=data, **kwargs)
# post请求参数类型 json data
elif method == 'POST':
if param_type=='FROM':
response = self.session.request(method=method, url=url, data=data, **kwargs)
else:
response = self.session.request(method=method, url=url, json=data, **kwargs)
elif method == 'DELETE':
if param_type == 'FROM':
response = self.session.request(method=method, url=url, data=data, **kwargs)
else:
response = self.session.request(method=method, url=url, json=data, **kwargs)
elif method == 'PUT':
if param_type == 'FROM':
response = self.session.request(method=method, url=url, data=data, **kwargs)
else:
response = self.session.request(method=method, url=url, json=data, **kwargs)
else:
raise ValueError
return response
'''
# 1.实例化类:
HttpClient().send_request(): 先实例化类,然后实例化类.方法
# 2.魔法方法(使用__call__方法):
a=HttpClient() 等同于 HttpClient().send_request()
'''
def __call__(self,method,url,param_type,data=None,headers=None,**kwargs):
return self.send_request(method,url,param_type,data,headers=None,**kwargs)
def close_session(self):
self.session.close()
《三》接口代码优化
(1)结合httpclient.py的应用
'''
结合httpclient.py的应用:
1.TestCase类:
注释: session = requests.session()
加此行: httpclient = HttpClient()
2.test_login测试方法中:
注释: res=TestCase.session.post(url,json=data)
加此行: res=TestCase.httpclient(method='post',url=url,param_type='json',data=data)
注释: TestCase.session.headers.update({'token':token})
加此行: TestCase.httpclient.session.headers.update({'token':token})
3.test_getUserinfo测试方法中:
注释: res = TestCase.session.get(url)
加此行: res = TestCase.httpclient(method='get', url=url, param_type='json')
4.test_shopping测试方法中:
注释: res=requests.get(url)
加此行: res=TestCase.httpclient(method='get', url=url, param_type='json')
5.test_cart测试方法中:
注释: url = 'http://39.98.138.157:5000/api/addcart'
加此行: data = {"openid": TestCase.openid,"productid": TestCase.productid,"userid": TestCase.userid}
6.test_order测试方法中:
注释: res=TestCase.session.post(url,json=data)
加此行: res = TestCase.httpclient(method='post', url=url, param_type='json', data=data)
'''
import allure
import jsonpath
import pytest
import requests
from test013.httpclient import HttpClient
@allure.feature('接口自动化测试')
class TestCase:
# 定义类的变量
# session = requests.session()
httpclient = HttpClient()
openid=None
productid=None
userid=None
cartid=None
@allure.story('登录流程')
@allure.title('登录')
def test_login(self):
url='http://39.98.138.157:5000/api/login'
data={"password": "123456","username": "admin"}
# res=requests.post(url,json=data)
# res=TestCase.session.post(url,json=data)
res=TestCase.httpclient(method='post',url=url,param_type='json',data=data)
print(res.json())
# 提取token 保存在变量中了
token=jsonpath.jsonpath(res.json(), '$..token')[0]
# 把这个token放在session中的headers里面,给头部里面设置属性,用update
# TestCase.session.headers.update({'token':token})
TestCase.httpclient.session.headers.update({'token':token})
sjmsg=jsonpath.jsonpath(res.json(),'$..msg')[0]
assert 'success'==sjmsg
# 获取个人信息
@allure.story('获得个人信息流程')
@allure.title('个人信息')
def test_getUserinfo(self):
url = 'http://39.98.138.157:5000/api/getuserinfo'
# 这个token不能写死 获得登录时的token
# header={"token":TestCase.token}
# res=requests.get(url,headers=header)
# res = TestCase.session.get(url)
res = TestCase.httpclient(method='get', url=url, param_type='json')
# 获取所有的结果
print(res.json())
TestCase.openid = jsonpath.jsonpath(res.json(), '$..openid')[0]
TestCase.userid = jsonpath.jsonpath(res.json(), '$..userid')[0]
print('openid:',TestCase.openid)
print('userid:',TestCase.userid)
sjname=jsonpath.jsonpath(res.json(),'$..nikename')[0]
assert '风清扬'==sjname
# 选择商品
@allure.story('选择商品流程')
@allure.title('商品')
def test_shopping(self):
url ='http://39.98.138.157:5000/api/getproductinfo?productid=8888'
# res=requests.get(url)
res=TestCase.httpclient(method='get', url=url, param_type='json')
print(res.json())
# 取 productid 的值
TestCase.productid = jsonpath.jsonpath(res.json(), '$..productid')[0]
sjproductid=jsonpath.jsonpath(res.json(),'$..productid')[0]
assert 8888==sjproductid
# 商品添加到购物车
@allure.story('添加购物车流程')
@allure.title('购物车')
def test_cart(self):
url = 'http://39.98.138.157:5000/api/addcart'
data = {"openid": TestCase.openid,"productid": TestCase.productid,"userid": TestCase.userid}
# res=TestCase.session.post(url,json=data)
res=TestCase.httpclient(method='post', url=url, param_type='json', data=data)
print(res.json())
# 取 cartid 的值
TestCase.cartid = jsonpath.jsonpath(res.json(), '$..cartid')[0]
# 断言
exmsg = 45233
sjmsg = jsonpath.jsonpath(res.json(),'$..cartid')[0]
assert exmsg == sjmsg
# 提交订单
@allure.story('下单流程')
@allure.title('下单')
def test_order(self):
url = 'http://39.98.138.157:5000/api/createorder'
data = {"cartid": TestCase.cartid, "openid": TestCase.openid, "productid": TestCase.productid, "userid": TestCase.userid}
# res=TestCase.session.post(url,json=data)
res = TestCase.httpclient(method='post', url=url, param_type='json', data=data)
print(res.json())
# 断言
usermsg = 17890
sjmsg = jsonpath.jsonpath(res.json(), '$..userid')[0]
assert usermsg == sjmsg
if __name__ == '__main__':
pytest.main(['-vs','test_py4.py'])
pytest.main(['-v','test_py4.py'])
人生苦短,及时行乐
分类:
测码教育 / 课后作业
, 测码教育 / 课堂代码
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现