当前位置: 首页 > article >正文

Pytest测试用例生命周期管理-Fixture

1、Fixture 用法

Fixture 特点及优势

  • 1、命令灵活:对于 setup,teardown,可以不起这两个名字
  • 2、数据共享:在 conftest.py 配置⾥写⽅法可以实现数据共享,不需要 import 导⼊。可以跨⽂件共享
  • 3、scope 的层次及神奇的 yield 组合相当于各种 setup 和 teardown
  • 4、实现参数化

Fixture 在自动化中的应用- 基本用法

  • 场景:

测试⽤例执⾏时,有的⽤例需要登陆才能执⾏,有些⽤例不需要登陆。

通常用上面的方法,但是pytest有更简单的实现方式

setup 和 teardown ⽆法满⾜。fixture 可以。默认 scope(范围)function

  • 步骤:
    • 1.导⼊ pytest
    • 2.在登陆的函数上⾯加@pytest.fixture()
    • 3.在要使⽤的测试⽅法中传⼊(登陆函数名称),就先登陆
    • 4.不传⼊的就不登陆直接执⾏测试⽅法。
 
  1. import pytest

  2. # 定义了登录的fixture

  3. @pytest.fixture()

  4. def login():

  5. print("完成登录操作")

  6. def test_search():

  7. print("搜索")

  8. # 传入登陆函数名称,执行用例的时候会先完成登录操作,在执行用例

  9. def test_cart(login):

  10. print("购物车")

  11. def test_order(login):

  12. print("下单功能")

Fixture 在自动化中的应用 - 作用域

取值范围说明
function函数级每一个函数或方法都会调用(默认函数级别)
class类级别每个测试类只运行一次
module模块级每一个.py 文件调用一次
package包级每一个 python 包只调用一次(暂不支持)
session会话级每次会话只需要运行一次,会话内所有方法及类,模块都共享这个方法

一个会话指pytest运行起来后,就是一个会话,运行的整个项目就是一个会话

 
  1. import pytest

  2. # 定义了登录的fixture尽量避免以test_开头

  3. # 默认函数级别

  4. @pytest.fixture()

  5. def login():

  6. print("完成登录操作")

  7. def test_search(login):

  8. print("搜索")

  9. def test_cart(login):

  10. print("购物车")

  11. def test_order(login):

  12. print("下单功能")

'

运行

运行

默认函数级别-每个函数执行前都会执行

 
  1. import pytest

  2. # 定义了登录的fixture,尽量避免以test_开头

  3. # 默认函数级别,改为模块级别

  4. @pytest.fixture(scppe="module")

  5. def login():

  6. print("完成登录操作")

  7. def test_search(login):

  8. print("搜索")

  9. def test_cart(login):

  10. print("购物车")

  11. def test_order(login):

  12. print("下单功能")

整个模块.py文件,所有用例执行之前执行。

Fixture 在自动化中的应用 - yield 关键字

  • 场景:

你已经可以将测试⽅法【前要执⾏的或依赖的】解决了, 测试⽅法后销毁清除数据的要如何进⾏呢?

  • 解决:

通过在 fixture 函数中加⼊ yield 关键字,yield 是调⽤第⼀次返回结果, 第⼆次执⾏它下⾯的语句返回。

  • 步骤:

在@pytest.fixture(scope=module)。 在登陆的⽅法中加 yield,之后加销毁清除的步骤

 
  1. # fixture 的作用域

  2. import pytest

  3. # 定义了登录的fixture,尽量避免以test_开头

  4. """

  5. @pytest.fixture

  6. def fixture_name():

  7. setup 操作

  8. yield 返回值

  9. teardown 操作

  10. """

  11. @pytest.fixture(scope="class")

  12. def login():

  13. # setup 操作

  14. print("完成登录操作")

  15. token = "abcdafafadfafda"

  16. username = 'hogwarts'

  17. yield token,username # 相当于return

  18. # teardown 操作

  19. print("完成登出操作")

  20. def test_search(login):

  21. token,username = login

  22. print(f"token:{token} , name : {username}")

  23. # login 返回 None,方法不加return,默认返回None

  24. print("搜索")

  25. def test_cart(login):

  26. print("购物车")

  27. def test_order(login):

  28. print("下单功能")

  29. class TestDemo:

  30. def test_case1(self,login):

  31. print("case1")

  32. def test_case2(self,login):

  33. print("case2")

'

运行

运行

Fixture 在自动化中的应用 - 数据共享

  • 场景:

你与其他测试⼯程师合作⼀起开发时,公共的模块要在不同⽂件中,要在⼤家都访问到的地⽅。

  • 解决:

使⽤ conftest.py 这个⽂件进⾏数据共享,并且他可以放在不同位置起着不同的范围共享作⽤。

  • 前提:
    • conftest ⽂件名是不能换的
    • 放在项⽬下是全局的数据共享的地⽅
  • 执⾏:
    • 系统执⾏到参数 login 时先从本模块中查找是否有这个名字的变量什么的,
    • 之后在 conftest.py 中找是否有。
  • 步骤:

将登陆模块带@pytest.fixture 写在 conftest.py

 
  1. # conftest.py 名字是固定的,不能改变

  2. import pytest

  3. @pytest.fixture(scope="function")

  4. def login():

  5. # setup 操作

  6. print("完成登录操作")

  7. token = "abcdafafadfafda"

  8. username = 'hogwarts'

  9. yield token,username # 相当于return

  10. # teardown 操作

  11. print("完成登出操作")

'

运行

运行

如果设置session会话级别,整个运行期间只调用一次 所有用例都添加login后,运行所有用例,只运行了一次

Fixture 在自动化中的应用 - 自动应用

场景:

不想原测试⽅法有任何改动,或全部都⾃动实现⾃动应⽤,

没特例,也都不需要返回值时可以选择⾃动应⽤

解决:

使⽤ fixture 中参数 autouse=True 实现

步骤:

在⽅法上⾯加 @pytest.fixture(autouse=True)

 
  1. # conftest.py 名字是固定的,不能改变

  2. import pytest

  3. @pytest.fixture(scope="function",autouse=True)

  4. def login():

  5. # setup 操作

  6. print("完成登录操作")

  7. token = "abcdafafadfafda"

  8. username = 'hogwarts'

  9. yield token,username # 相当于return

  10. # teardown 操作

  11. print("完成登出操作")

'

运行

运行

Fixture 在自动化中的应用 -参数化

场景:

测试离不开数据,为了数据灵活,⼀般数据都是通过参数传的

解决:

fixture 通过固定参数 request 传递

步骤:

在 fixture 中增加@pytest.fixture(params=[1, 2, 3, ‘linda’])

在⽅法参数写 request,方法体里面使用 request.param 接收参数

 
  1. import pytest

  2. @pytest.fixture(params=[["selenium",123],["appium",123456]])

  3. def login(request):

  4. print(f"用户名:{request.param}")

  5. return request.param

  6. def test_demo1(login):

  7. print(f"demo1 case: 数据为: {login}")

'

运行

运行

Fixture 的用法总结

  • 模拟 setup,teardown(一个用例可以引用多个 fixture)
  • yield 的用法
  • 作用域( session,module, 类级别,方法级别 )
  • 自动执行 (autouse 参数)
  • conftest.py 用法,一般会把 fixture 写在 conftest.py 文件中(这个文件名字是固定的,不能改)
  • 实现参数化

感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!有需要的小伙伴可以点击下方小卡片领取   


http://www.kler.cn/news/341839.html

相关文章:

  • VBA即用型代码手册:将工作表复制到已关闭的工作簿
  • YOLO11改进|SPPF篇|引入YOLOv9提出的SPPELAN模块
  • uni-app之旅-day04-商品列表
  • 旅游管理智能化转型:SpringBoot系统设计与实现
  • 基于证书的身份验证方式及示例
  • Linux-控制脚本
  • RabbitMQ 交换机的类型
  • Vue入门-Vue中实例和java中类的相同和不同
  • MySQL 中的 GROUP BY 使用
  • ppt压缩文件怎么压缩?压缩PPT文件的多种压缩方法
  • 影刀RPA实战:Excel排序、替换与格式
  • 用source Map还原被打包编译的源代码
  • 33-Golang开发入门精讲
  • 周易解读开篇语
  • DC-1靶机搭建与通关详解
  • 基于java的零食销售系统(源码+定制+开发)
  • vSAN06:ESA与OSA对比、ESA安装、新架构、工作方式、自动策略管理、原生快照、数据压缩、故障处理
  • 【PostgreSQL 】实战篇——如何使用 EXPLAIN 和 ANALYZE 工具分析查询计划和性能,优化查询
  • 大数据新视界 --大数据大厂之大数据于基因测序分析的核心应用 - 洞悉生命信息的密钥
  • QT day05