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 2022-04-18 17:37  大话人生  阅读(41)  评论(0编辑  收藏  举报