• Pytest学习之 autouse=True,自动调用fixture功能


    平常写自动化用例会写一些前置的 fixture 操作,用例需要用到就直接传该函数的参数名称就行了。当用例很多的时候,每次都传返个参数,会比较麻烦。
    fixture 里面有个参数 autouse,默讣是 Fasle 没开启的,可以设置为True 开启自动使用 fixture 功能,返样用例就丌用每次都去传参了
    调用 fixture 三种方法
      1.函数或类里面方法直接传 fixture 的函数参数名称
      2.使用装饰器@pytest.mark.usefixtures()修饰
      3.autouse=True 自动使用

    方法一:autouse 设置为 True,自动调用 fixture 功能
    1.start 设置 scope 为 module 级别,在当前.py 用例模块只执行一次,autouse=True 自动使用
    2.open_home 设置 scope 为 function 级别,每个用例前都调用一次,自动使用

    import pytest
    
    @pytest.fixture(scope="module",autouse=True)
    def start(request):
        print("
    ----开始执行module------")
        print('module : %s'% request.module.__name__)
        print('------启动浏览器-------')
        yield
        print("------结束测试 end!----------")
    
    @pytest.fixture(scope="function",autouse=True)
    def open_home(request):
        print("function:%s 
    --回到首页--"% request.function.__name__)
    
    def test_01():
        print('----用例01-----')
    
    def test_02():
        print('----用例02-----')
    
    if __name__ == '__main__':
        pytest.main(["-s","test_hest.py"])
    

    运行结果:

    ============================= test session starts =============================
    platform win32 -- Python 3.6.5, pytest-4.5.0, py-1.8.0, pluggy-0.12.0
    rootdir: C:UsershghPycharmProjectsuntitled1	estcase
    plugins: allure-pytest-2.8.2, html-1.22.0, metadata-1.8.0, rerunfailures-7.0, shutil-1.7.0
    collected 2 items
    
    test_hest.py 
    ----开始执行module------
    module : testcase.test_hest
    ------启动浏览器-------
    function:test_01 
    --回到首页--
    ----用例01-----
    .function:test_02 
    --回到首页--
    ----用例02-----
    .------结束测试 end!----------
    
    
    ========================== 2 passed in 0.10 seconds ===========================
    

    方法二:使用装饰器@pytest.mark.usefixtures()修饰需要运行的用例

    import pytest
    
    @pytest.fixture(scope='function')
    def start(request):
        print('==========开始执行function===========')
    
    @pytest.mark.usefixtures('start')
    def test_a():
        print('==============执行用例a===============')
    
    @pytest.mark.usefixtures('start')
    class TestCase():
    
        def test_01(self):
            print('============用例01============')
    
        def test_02(self):
            print('============用例02============')
    
    if __name__=='__main__':
        pytest.main(["-s", "test_hest.py"])
    

    运行结果:

    ============================= test session starts =============================
    platform win32 -- Python 3.6.5, pytest-4.5.0, py-1.8.0, pluggy-0.12.0
    rootdir: C:UsershghPycharmProjectsuntitled1	estcase
    plugins: allure-pytest-2.8.2, html-1.22.0, metadata-1.8.0, rerunfailures-7.0, shutil-1.7.0
    collected 3 items
    
    test_hest.py ==========开始执行function===========
    ==============执行用例a===============
    .==========开始执行function===========
    ============用例01============
    .==========开始执行function===========
    ============用例02============
    .
    
    ========================== 3 passed in 0.11 seconds ===========================
    

    方法三:先定义 start 功能,用例全部传 start 参数,调用该功能

    import pytest
    
    
    @pytest.fixture(scope='function')
    def start(request):
        print('
    =========开始执行function============')
    
    def test_a(start):
        print('=======用例a==========')
    
    class TestCase():
    
        def test_01(self, start):
            print('=======用例01=========')
    
        def test_02(self, start):
            print('=======用例02=========')
            
    if __name__=='__main__':
        pytest.main(["-s", "test_hest.py"])
    

    运行结果:

    ============================= test session starts =============================
    platform win32 -- Python 3.6.5, pytest-4.5.0, py-1.8.0, pluggy-0.12.0
    rootdir: C:UsershghPycharmProjectsuntitled1	estcase
    plugins: allure-pytest-2.8.2, html-1.22.0, metadata-1.8.0, rerunfailures-7.0, shutil-1.7.0
    collected 3 items
    
    test_hest.py 
    =========开始执行function============
    =======用例a==========
    .
    =========开始执行function============
    =======用例01=========
    .
    =========开始执行function============
    =======用例02=========
    .
    
    ========================== 3 passed in 0.11 seconds ===========================
    
    

    详见此链接:https://www.icode9.com/content-4-631455.html

  • 相关阅读:
    *** 82 删除排序链表中的重复元素II
    83 删除排序链表中的重复元素
    61 旋转链表
    ASP.NET MVC3 ModelState.IsValid为false的问题
    ServletContext
    ServletConfig
    Servlet线程安全
    Servlet的一些细节(2)
    Servlet的一些细节(1)
    Servlet的接口实现类
  • 原文地址:https://www.cnblogs.com/hghua/p/13264467.html
Copyright © 2020-2023  润新知