随笔 - 633,  文章 - 0,  评论 - 13,  阅读 - 48万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
fixture固件装饰器做用例的前后置-4
fixture固件装饰器做用例的前后置 方法 fixture一个结合conftest.py文件一起使用 @pytest.fixture (scope="作用域", params="数据驱动", autouse="自动执行", ids="数据驱动时重命名参数名", name="给fixture作用的函数重命名") scope参数 function 函数 class module 模块 package/session 会话 实例 autouse=True,会在每条用例执行前后置操作 import pytest @pytest.fixture(scope="function",autouse=True) def exe_clean(): print("前置处理") yield print("后置处理") def testsouhulogin(): print("登录搜狐") class TestSouHu(): age=18 @pytest.mark.run(order=2) def testsearch(self): print("搜狐搜索视频") assert False @pytest.mark.run(order=3) @pytest.mark.smoke @pytest.mark.skip(reason="太难了") def testclick(self): print("搜狐点击") assert False @pytest.mark.run(order=1) @pytest.mark.skipif(age>=18,reason='已成年') def testswipe(self): print("搜狐滑动") assert False autouse不设置,单个函数引用,需要引用固件装饰器的函数名 import pytest @pytest.fixture(scope="function") def exe_clean(): print("前置处理") yield print("后置处理") def testsouhulogin(): print("登录搜狐") class TestSouHu(): age=18 @pytest.mark.run(order=2) def testsearch(self,exe_clean): print("搜狐搜索视频") assert False @pytest.mark.run(order=3) @pytest.mark.smoke @pytest.mark.skip(reason="太难了") def testclick(self): print("搜狐点击") assert False @pytest.mark.run(order=1) @pytest.mark.skipif(age>=18,reason='已成年') def testswipe(self): print("搜狐滑动") assert False 实例 autouse=True,会在每个类开始前执行前置,在类结束后执行后置操作 import pytest @pytest.fixture(scope="class",autouse=True) def exe_clean(): print("前置处理") yield print("后置处理") def testsouhulogin(): print("登录搜狐") class TestSouHu(): age=18 @pytest.mark.run(order=2) def testsearch(self): print("搜狐搜索视频") assert False @pytest.mark.run(order=3) @pytest.mark.smoke @pytest.mark.skip(reason="太难了") def testclick(self): print("搜狐点击") assert False @pytest.mark.run(order=1) @pytest.mark.skipif(age>=18,reason='已成年') def testswipe(self): print("搜狐滑动") assert False class TestBaiDuYun(): def testloginBaiDuYun(self): print("登录百度云") autouse不设置,单个类引用,需要用@pytest.mark.usefixtures('exe_clean') import pytest @pytest.fixture(scope="class") def exe_clean(): print("前置处理") yield print("后置处理") def testsouhulogin(): print("登录搜狐") class TestSouHu(): age=18 @pytest.mark.run(order=2) def testsearch(self): print("搜狐搜索视频") assert False @pytest.mark.run(order=3) @pytest.mark.smoke @pytest.mark.skip(reason="太难了") def testclick(self): print("搜狐点击") assert False @pytest.mark.run(order=1) @pytest.mark.skipif(age>=18,reason='已成年') def testswipe(self): print("搜狐滑动") assert False @pytest.mark.usefixtures('exe_clean') class TestBaiDuYun(): def testloginBaiDuYun(self): print("登录百度云") conftest.py与测试用例放在同一级目录下 conftest.py import pytest @pytest.fixture(scope="class") def exe_clean(): print("前置处理") yield print("后置处理") souhu_test.py import pytest def testsouhulogin(): print("登录搜狐") class TestSouHu(): age=18 @pytest.mark.run(order=2) def testsearch(self): print("搜狐搜索视频") assert False @pytest.mark.run(order=3) @pytest.mark.smoke @pytest.mark.skip(reason="太难了") def testclick(self): print("搜狐点击") assert False @pytest.mark.run(order=1) @pytest.mark.skipif(age>=18,reason='已成年') def testswipe(self): print("搜狐滑动") assert False @pytest.mark.usefixtures('exe_clean') class TestBaiDuYun(): def testloginBaiDuYun(self): print("登录百度云") conftest.py单独用于存放fixture固件的配置文件 使用conftest.py中的固件不需要导包,可以执行使用 可以有多个conftest.py autouse=True,会在每个会话开始前执行前置,在会话结束后执行后置操作 import pytest @pytest.fixture(scope="session",autouse=True) def exe_clean(): print("前置处理") yield print("后置处理") autouse=True,会在每个py文件开始前执行前置,在py结束后执行后置操作 conftest.py conftest.py import pytest @pytest.fixture(scope="module",autouse=True) def exe_clean(): print("前置处理") yield print("后置处理")
posted on   大话人生  阅读(42)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2018-04-18 pycharm(pythoon3)_django2.0_xadmin创建测试用例后台管理系统
点击右上角即可分享
微信分享提示