pytest + yaml 框架 -15.fixtures 功能使用
前言
前面讲到全局只登录一次,全部的yaml 用例都会公用一个请求会话。
那有些接口不需要登录怎么办呢?比如登录和注册的接口,是不需要带上登录 token 的。
我除了默认用到一个 requests_session 全局的内置 fixture,还预留了2个
- requests_module: 每个yaml文件中用一次
- requests_function: 每个用例中用一次
pip 安装插件
pip install pytest-yaml-yoyo
requests_module 和 requests_function 内置 fixture 功能在 v1.1.1 版本实现
全局设置请求头部
公共请求头部带上token参考这篇https://www.cnblogs.com/yoyoketang/p/16924506.html
我在pytest + yaml 框架框架中封装了一个内置 fixture 叫 requests_session, 它的作用范围是 scope="session" ,也就是全部 session 用例会话中仅实例化一次。
现在我只需在 conftest 中写一个登录的 fixture 功能,获取 token 后添加到 requests_session 头部
import pytest
import uuid
"""
全局仅登录一次,获取token,
在请求头部添加Authentication Bearer 认证
内置fixture requests_session
"""
def login():
"""登录方法"""
# 调用登录方法,返回token
return str(uuid.uuid4()) # noqa
@pytest.fixture(scope="session", autouse=True)
def login_first(requests_session):
"""全局仅一次登录, 更新session请求头部"""
# 调用登录方法,获得token
token = login()
headers = {
"Authentication": f"Bearer {token}"
}
requests_session.headers.update(headers)
接着我写2个yaml文件(注意,yaml文件中也不需要重复去添加请求头部了)
test_get_demo.yml
config:
name: get
teststeps:
-
name: get
request:
method: GET
url: http://httpbin.org/get
validate:
- eq: [status_code, 200]
test_post_demo.yml
config:
name: post示例
variables:
username: test
password: "123456"
teststeps:
-
name: post
request:
method: POST
url: http://httpbin.org/post
json:
username: ${username}
password: ${password}
validate:
- eq: [status_code, 200]
在命令行中输入pytest
运行, 于是可以看到,在2个用例中都自动带上了请求头部参数。
有小伙伴问到:请求头部需加其他固定的参数怎么办?比如versionId 和configId, 方法都是一样
headers = {
"Authentication": f"Bearer {token}",
"versionId": "v1.0",
"configId": "10086"
}
requests_session.headers.update(headers)
当我们设置了全局 requests_session 请求会话后, 默认所有的请求都会带上添加的头部参数
headers = {
"Authentication": f"Bearer {token}",
"versionId": "v1.0",
"configId": "10086"
}
requests_module 和 requests_function
那有些接口不需要登录怎么办呢?比如登录和注册的接口,是不需要带上登录token的。
除了默认用到一个requests_session 全局的内置fixture,还预留了2个
- requests_module: 每个yaml文件中用一个请求会话(会保持cookies)
- requests_function: 每个用例中用一次,每个用例独立运行,不保持cookies
接下来看下如何在用例中使用test_register.yml
config:
name: post示例
fixtures: requests_module
注册1:
request:
method: POST
url: http://httpbin.org/post
json:
username: test123
password: "123456"
validate:
- eq: [status_code, 200]
注册2:
request:
method: POST
url: http://httpbin.org/post
json:
username: test444
password: "123456"
validate:
- eq: [status_code, 200]
在config 中传入 fixtures参数,requests_module 是每个yaml文件中用一个请求会话(会保持cookies)
requests_function 作用是每个用例中用一次,每个用例独立运行,不保持cookies。
自定义 fixtures
pytest 的核心功能是学会灵活使用fixtures, 那么我们的这个插件也是可以支持在用例中调用fixtures功能的。
在conftest.py 文件中写你需要实现的fixture 功能, 设置使用范围为scope="function"
函数级别
import pytest
@pytest.fixture(scope="function")
def demo_fixture():
print("用例前置操作->do something .....")
yield
print("用例后置操作,do something .....")
在 yaml 文件中引用 fixture
config:
name: post示例
fixtures: demo_fixture
注册1:
request:
method: POST
url: http://httpbin.org/post
json:
username: test123
password: "123456"
validate:
- eq: [status_code, 200]
注册2:
request:
method: POST
url: http://httpbin.org/post
json:
username: test444
password: "123456"
validate:
- eq: [status_code, 200]
于是运行结果可以看到,每个用例前后都会执行
collected 2 items
test_f2.yml 用例前置操作->do something .....
.用例后置操作,do something .....
用例前置操作->do something .....
用例后置操作,do something .....
如果想整个yaml 文件中仅运行一次,那么conftest.py 文件中写你需要实现的 fixture 功能, 设置使用范围为scope="module"
模块级别
import pytest
@pytest.fixture(scope="module")
def demo_fixture():
print("用例前置操作->do something .....")
yield
print("用例后置操作,do something .....")
于是看到运行的时候,仅在yaml 文件的全部用例中只执行一次
collected 2 items
test_f2.yml 用例前置操作->do something .....
..用例后置操作,do something .....
多个fixtures的使用
当 yaml 中的用例需要用到多个fixtures时, 支持2种格式
格式一: 逗号隔开
config:
fixtures: fixture_name1, fixture_name2
格式二: 用 list
config:
fixtures: [fixture_name1, fixture_name2]
requests_module 和 requests_function 内置 fixture 功能在 v1.1.1 版本实现, 版本太低的请及时更新版本。
网易云完整视频课程https://study.163.com/course/courseMain.htm?courseId=1213419817&share=2&shareId=480000002230338
报名咨询wx:283340479 (已报名的同学学习过程中有问题,都可以协助解决)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2018-12-14 关于面试总结6-SQL经典面试题
2016-12-14 Selenium2+python自动化25-js处理日历控件(修改readonly属性)