zoukankan      html  css  js  c++  java
  • 【pytest学习6】fixture的作用范围

    fixture可以代替setup和teardown,怎么在不同的场景下进行使用运行呢?比如我只想要启动浏览器一次呢?如果每个用例按照前面的都加入fixture那么每条用例都会运行,其实fixture中有参数可以进行配置,配置后可以在不同的场景下进行使用,这里就要引入新的知识fixture的作用范围。

    fixture作用范围

    fixture中的scope参数可以进行配置,我们可以从源码中看到scope的默认参数为function。下面也介绍了scope中的其他参数:“class”,“module”,“session”

    复制代码
    def fixture(  # noqa: F811
        fixture_function: Optional[_FixtureFunction] = None,
        *,
        scope: "Union[_Scope, Callable[[str, Config], _Scope]]" = "function",
        params: Optional[Iterable[object]] = None,
        autouse: bool = False,
        ids: Optional[
            Union[
                Iterable[Union[None, str, float, int, bool]],
                Callable[[Any], Optional[object]],
            ]
        ] = None,
        name: Optional[str] = None
    ) -> Union[FixtureFunctionMarker, _FixtureFunction]:
        """用于标识夹具工厂功能的装饰器。
        该装饰器可以使用,也可以不使用参数,来定义
        夹具的功能。
        fixture函数的名称稍后可以被引用以导致its
        在运行测试之前调用:测试模块或类可以使用
        ``pytest.mark.usefixtures(fixturename)`` marker.
    
       测试函数可以直接使用fixture名称作为其中的输入参数
        从fixture函数返回的fixture实例的情况
        注入。
        :param scope:
            The scope for which this fixture is shared; one of ``"function"``
            (default), ``"class"``, ``"module"``, ``"package"`` or ``"session"``.
    复制代码

     

    function

    参数scope默认才是就是function,主要应用单个测试函数中,yield前面的代码在测试用例运行前执行,后面的代码在测试用例运行之后运行。未添加到的测试函数,不进行执行

    复制代码
    # test_01.py
    import pytest
    
    
    @pytest.fixture(scope='function')
    def login():
        print('输入用户名,密码,完成登录!')
        yield
        print('关闭浏览器!')
    
    
    class Test_01:
    
        def test_01(self, login):
            print('需要用到登录功能')
            print('----用例01---')
    
        def test_02(self):
            print('不需要登录功能')
            print('----用例02---')
    
        def test_03(self, login):
            print('需要用到登录功能')
            print('----用例03---')
    
    
    if __name__ == '__main__':
        pytest.main(['-s', 'test__01.py'])
    复制代码

     

    module

    scope值为“module”表示为模块级别的fixture每个模块之运行1次,无论模块中多少个测试用例,都只运行一次。

    复制代码
    # test_01.py
    import pytest
    
    
    @pytest.fixture(scope='module')
    def login():
        print('输入用户名,密码,完成登录!')
        yield
        print('关闭浏览器!')
    
    
    class Test_01:
    
        def test_01(self, login):
            print('需要用到登录功能')
            print('----用例01---')
    
        def test_02(self):
            print('不需要登录功能')
            print('----用例02---')
    
        def test_03(self, login):
            print('需要用到登录功能')
            print('----用例03---')
    
    
    if __name__ == '__main__':
        pytest.main(['-s', 'test__01.py'])
    复制代码

     

    class

    scope的值为“class”表示类机会的fixture中每个测试函数都只执行一次。无论模块中多少个用例,都只执行一次。

    复制代码
    # test_01.py
    import pytest
    
    
    @pytest.fixture(scope='class')
    def login():
        print('输入用户名,密码,完成登录!')
        yield
        print('关闭浏览器!')
    
    
    class Test_01:
    
        def test_01(self, login):
            print('需要用到登录功能')
            print('----用例01---')
    
        def test_02(self):
            print('不需要登录功能')
            print('----用例02---')
    
        def test_03(self, login):
            print('需要用到登录功能')
            print('----用例03---')
    
    
    if __name__ == '__main__':
        pytest.main(['-s', 'test__01.py'])
    复制代码

    session

    scope的值为“session”表示会话级别的fixture。每次会话都会只执行一次。每个添加的用例只会执行1次fixture

    复制代码
    # test__01.py
    
    import pytest
    
    
    @pytest.fixture(scope='session')
    def login():
        print('输入用户名,密码,完成登录!')
        yield
        print('关闭浏览器!')
    
    
    class Test_01:
    
        def test_01(self, login):
            print('需要用到登录功能')
            print('----用例01---')
    
        def test_02(self):
            print('不需要登录功能')
            print('----用例02---')
    
        def test_03(self, login):
            print('需要用到登录功能')
            print('----用例03---')
    
    
    if __name__ == '__main__':
        pytest.main(['-s', 'test__01.py'])
    复制代码

    执行顺序

    了解了fixture中scope参数的各个使用范围,那么如果同时使用,这些执行顺序是什么样子的呢?实践证明下

    复制代码
    # test__01.py
    
    import pytest
    
    
    @pytest.fixture(scope='session')
    def fix_session():
        print('这是属于fixture中的会话级别')
        yield
        print('session')
    
    
    @pytest.fixture(scope='class')
    def fix_class():
        print('这是属于fixture中的类级别')
        yield
        print('session')
    
    
    @pytest.fixture(scope='module')
    def fix_module():
        print('这是属于fixture中的模块级别')
        yield
        print('module')
    
    
    @pytest.fixture()
    def fix_function():
        print('这是属于fixture中的函数级别')
        yield
        print('function')
    
    
    class Test_01:
    
        def test_01(self, fix_function, fix_class,fix_module,fix_session):
            print('----用例01---')
    
    
    if __name__ == '__main__':
        pytest.main(['-s', 'test__01.py'])
    复制代码

    通过上面实践发现执行顺序:session>>module>>class>>function

    声明 欢迎转载,但请保留文章原始出处:) 博客园:https://www.cnblogs.com/chenxiaomeng/ 如出现转载未声明 将追究法律责任~谢谢合作
  • 相关阅读:
    数值项目的格式化
    ORACLE ERROR CODE代表的意思
    深入了解 Microsoft AJAX Library
    调用MSScriptContro来运算字符串表达式
    C# 调用带参数EXE文件及带启动参数EXE制作
    将DataTable或Ilist<>转换成JSON格式
    客户端控件调用服务器的参数
    调用ICodeCompiler来计算字符串表达式
    录像工具
    今天是开博客园的第一天
  • 原文地址:https://www.cnblogs.com/chenxiaomeng/p/14817872.html
Copyright © 2011-2022 走看看