设计平台网站,从化建网站,wordpress扁平化风格主题,深圳网站优化培训1、Fixture 用法
Fixture 特点及优势
1#xff64;命令灵活#xff1a;对于 setup,teardown,可以不起这两个名字2#xff64;数据共享#xff1a;在 conftest.py 配置⾥写⽅法可以实现数据共享#xff0c;不需要 import 导⼊。可以跨⽂件共享3#xff64;scope 的层次及…1、Fixture 用法
Fixture 特点及优势
1命令灵活对于 setup,teardown,可以不起这两个名字2数据共享在 conftest.py 配置⾥写⽅法可以实现数据共享不需要 import 导⼊。可以跨⽂件共享3scope 的层次及神奇的 yield 组合相当于各种 setup 和 teardown4、实现参数化
Fixture 在自动化中的应用- 基本用法
场景
测试⽤例执⾏时有的⽤例需要登陆才能执⾏有些⽤例不需要登陆。 通常用上面的方法但是pytest有更简单的实现方式
setup 和 teardown ⽆法满⾜。fixture 可以。默认 scope范围function
步骤 1.导⼊ pytest2.在登陆的函数上⾯加pytest.fixture()3.在要使⽤的测试⽅法中传⼊登陆函数名称就先登陆4.不传⼊的就不登陆直接执⾏测试⽅法。
import pytest
# 定义了登录的fixture
pytest.fixture()def login():print(完成登录操作)def test_search():print(搜索)# 传入登陆函数名称执行用例的时候会先完成登录操作在执行用例
def test_cart(login):print(购物车)def test_order(login):print(下单功能)
Fixture 在自动化中的应用 - 作用域
取值范围说明function函数级每一个函数或方法都会调用(默认函数级别)class类级别每个测试类只运行一次module模块级每一个.py 文件调用一次package包级每一个 python 包只调用一次(暂不支持)session会话级每次会话只需要运行一次会话内所有方法及类模块都共享这个方法
一个会话指pytest运行起来后就是一个会话运行的整个项目就是一个会话
import pytest
# 定义了登录的fixture尽量避免以test_开头
# 默认函数级别
pytest.fixture()
def login():print(完成登录操作)def test_search(login):print(搜索)def test_cart(login):print(购物车)def test_order(login):print(下单功能) 默认函数级别-每个函数执行前都会执行
import pytest
# 定义了登录的fixture,尽量避免以test_开头
# 默认函数级别改为模块级别
pytest.fixture(scppemodule)
def login():print(完成登录操作)def test_search(login):print(搜索)def test_cart(login):print(购物车)def test_order(login):print(下单功能) 整个模块.py文件所有用例执行之前执行。
Fixture 在自动化中的应用 - yield 关键字
场景
你已经可以将测试⽅法【前要执⾏的或依赖的】解决了 测试⽅法后销毁清除数据的要如何进⾏呢
解决
通过在 fixture 函数中加⼊ yield 关键字yield 是调⽤第⼀次返回结果 第⼆次执⾏它下⾯的语句返回。
步骤
在pytest.fixture(scopemodule)。 在登陆的⽅法中加 yield之后加销毁清除的步骤
# fixture 的作用域
import pytest
# 定义了登录的fixture尽量避免以test_开头pytest.fixture
def fixture_name():setup 操作yield 返回值teardown 操作pytest.fixture(scopeclass)
def login():# setup 操作print(完成登录操作)token abcdafafadfafdausername hogwartsyield token,username # 相当于return# teardown 操作print(完成登出操作)def test_search(login):token,username loginprint(ftoken{token} , name : {username})# login 返回 None方法不加return默认返回Noneprint(搜索)def test_cart(login):print(购物车)def test_order(login):print(下单功能)class TestDemo:def test_case1(self,login):print(case1)def test_case2(self,login):print(case2)
Fixture 在自动化中的应用 - 数据共享
场景
你与其他测试⼯程师合作⼀起开发时公共的模块要在不同⽂件中要在⼤家都访问到的地⽅。
解决
使⽤ conftest.py 这个⽂件进⾏数据共享并且他可以放在不同位置起着不同的范围共享作⽤。
前提 conftest ⽂件名是不能换的放在项⽬下是全局的数据共享的地⽅执⾏ 系统执⾏到参数 login 时先从本模块中查找是否有这个名字的变量什么的之后在 conftest.py 中找是否有。步骤
将登陆模块带pytest.fixture 写在 conftest.py
# conftest.py 名字是固定的不能改变
import pytestpytest.fixture(scopefunction)
def login():# setup 操作print(完成登录操作)token abcdafafadfafdausername hogwartsyield token,username # 相当于return# teardown 操作print(完成登出操作)如果设置session会话级别整个运行期间只调用一次 所有用例都添加login后运行所有用例只运行了一次
Fixture 在自动化中的应用 - 自动应用
场景
不想原测试⽅法有任何改动或全部都⾃动实现⾃动应⽤
没特例也都不需要返回值时可以选择⾃动应⽤
解决
使⽤ fixture 中参数 autouseTrue 实现
步骤
在⽅法上⾯加 pytest.fixture(autouseTrue)
# conftest.py 名字是固定的不能改变
import pytestpytest.fixture(scopefunction,autouseTrue)
def login():# setup 操作print(完成登录操作)token abcdafafadfafdausername hogwartsyield token,username # 相当于return# teardown 操作print(完成登出操作)Fixture 在自动化中的应用 -参数化
场景
测试离不开数据为了数据灵活⼀般数据都是通过参数传的
解决
fixture 通过固定参数 request 传递
步骤
在 fixture 中增加pytest.fixture(params[1, 2, 3, ‘linda’])
在⽅法参数写 request方法体里面使用 request.param 接收参数
import pytestpytest.fixture(params[[selenium,123],[appium,123456]])
def login(request):print(f用户名{request.param})return request.paramdef test_demo1(login):print(fdemo1 case: 数据为 {login})Fixture 的用法总结
模拟 setupteardown一个用例可以引用多个 fixtureyield 的用法作用域 sessionmodule, 类级别方法级别 自动执行 autouse 参数conftest.py 用法一般会把 fixture 写在 conftest.py 文件中这个文件名字是固定的不能改实现参数化
最后感谢每一个认真阅读我文章的人礼尚往来总是要有的虽然不是什么很值钱的东西如果你用得到的话可以直接拿走 这些资料对于【软件测试】的朋友来说应该是最全面最完整的备战仓库这个仓库也陪伴上万个测试工程师们走过最艰难的路程希望也能帮助到你