pytest学习和使用7-fixture参数scope作用域(范围)执行顺序
2023-03-07 09:16:13 时间
之前学习了fixture的基本使用,其中参数scope类似作用域,就是fixture的使用范围,那么针对scope的这几个值,他的执行顺序是怎样的?
1 scope的五个范围
值 | 作用范围 |
---|---|
| 整个测试会话,跨文件调用 |
| 跨文件调用,可以跨 .py 文件 |
| 一个.py 执行一次,一个.py 文件可能包含多个类和方法 |
| 每个类都会执行一次。类中有多个方法调用,只在第一个方法调用时执行 |
| 每个方法(函数)都会执行一次 。.如果 |
2 执行顺序
- 较高 scope 范围的fixture(session)在较低 scope 范围的fixture( function 、 class )之前执行:
【session > package > module > class > function】
- 具有相同作用域的fixture遵循测试函数中声明的顺序,并遵循fixture之间的依赖关系;
【在test_one里面依赖的fixture_A优先执行,然后到test_one本身】
- 使用(autouse=True)的fixture在使用传参或装饰器的fixture之前执行。
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/11/17
# 文件名称:test_fixture_scope.py
# 作用:scope的范围
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
import pytest
@pytest.fixture(scope="function")
def case2():
print("session执行后,再执行module,最后执行function")
@pytest.fixture(scope="session")
def case():
print("session先执行")
@pytest.fixture(scope="module")
def case1():
print("session执行后,再执行module")
def test_one(case2, case1, case):
print("111111111111")
if __name__ == "__main__":
pytest.main(["-s", "test_fixture_scpoe.py"])
test_fixture_scope.py::test_one session先执行
session执行后,再执行module
session执行后,再执行module,最后执行function
PASSED [100%]111111111111
============================== 1 passed in 0.02s ==============================
3 多个fixture依赖
- 添加了
@pytest.fixture
,如果fixture还想依赖其他fixture,需要用函数传参的方式; - 不能用
@pytest.mark.usefixtures()
的方式,否则会不生效 - 那么我们来试试,看下效果如何?
3.1 正常调用
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/11/17
# 文件名称:test_fixtures.py
# 作用:多个fixture依赖
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
import pytest
@pytest.fixture()
def one():
print("第一个fixture")
@pytest.fixture()
def two(one):
print("第二个fixture依赖第一个fixture")
def test_case(two):
print("测试用例依赖第一个fixture和第二个fixture")
if __name__ == '__main__':
pytest.main(["-s", "test_fixtures.py"])
test_fixtures.py::test_case 第一个fixture
第二个fixture依赖第一个fixture
PASSED [100%]测试用例依赖第一个fixture和第二个fixture
============================== 1 passed in 0.02s ==============================
3.2 使用mark.usefixtures
# -*- coding:utf-8 -*-
# 作者:NoamaNelson
# 日期:2022/11/17
# 文件名称:test_fixtures.py
# 作用:多个fixture依赖
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson
import pytest
@pytest.fixture()
def one():
print("第一个fixture")
@pytest.fixture()
def two(one):
print("第二个fixture依赖第一个fixture")
@pytest.mark.usefixtures("one") # 没有生效
def three():
print("多个依赖不能用mark.usefixtures===这句话之前应该不会打印‘第一个fixture’")
def test_case(two):
print("测试用例依赖第一个fixture和第二个fixture")
three()
if __name__ == '__main__':
pytest.main(["-s", "test_fixtures.py"])
test_fixtures.py::test_case 第一个fixture
第二个fixture依赖第一个fixture
PASSED [100%]测试用例依赖第一个fixture和第二个fixture
多个依赖不能用mark.usefixtures===这句话之前应该不会打印‘第一个fixture’
============================== 1 passed in 0.03s ==============================
相关文章
- 发现 Linux SpaceFM 文件管理器的威力
- 夯实数据智能底座 共筑数字经济发展“新基石”
- 五分钟教你使用console.log发布公司的招聘信息
- 微软发布 Windows 11 Beta 预览版 Build 22621.1250 和 22623.1250 (KB5023008)
- Windows 11 学院:在 Windows 11 Build 25290 上如何为文件管理器启用标签页拖拽支持
- 微软Windows 11 Dev 预览版 Build 25290 发布:带来新的开始菜单提醒角标
- 微软正用全屏通知提醒 Windows 10 用户免费升级 Windows 11,要点击 5 次才能退出
- Linux Mint 21.2 将于 6 月发布:改进登录屏幕,优化 Pix 图像管理程序等等
- 字节面试也会问SPI机制?
- 微软正式停售 Windows 10 产品密钥 / 许可证:继续提供 ISO 镜像下载,推荐用户升级 Windows 11
- 用复古电脑程序 Toy CPU 学习低级编程
- Windows 11 新功能泄露:现代音量合成器、实验工具和新的文件管理器
- 微软 Windows 11 文件资源管理器全新设计曝光:UI 迎来大修,整合 Microsoft 365
- 对比Pandas,学习PySpark大数据处理
- 微软宣布为Windows 11 21H2设备开启自动更新到22H2版本
- 互联网都在说降本增效,小红书技术团队是怎么做的?
- 微软将继续提供 Windows 10 ISO 镜像下载,即将停售产品密钥 / 许可证
- 微软宣布为Windows 11 21H2 设备开启自动更新到 22H2 版本
- 面试突击:Properties和Yml有什么区别?
- 微软 Windows 11 Beta 预览版 22621.1245 和 22623.1245 (KB5022358) 发布