zoukankan      html  css  js  c++  java
  • 接口自动化

                                                                 接口自动化-pytest中的fixture - scope                                                              

      介绍  

    fixture文章中介绍的比较少,同学们可以去搜索下fixture的详解或者去看看源码

    在这之前博主都是用的unittest单元测试框架去写的接口自动化,感觉也挺好用,但是得知pytest的fixture以及allure后,则出现了真香警告!!

    先说fixture源码中包含了几大核心,我摘出了源码中的一部分

    def fixture(
        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,
    )

    1、scope  2、params   3、autouse    4、ids

    本文章对第一条 scope进行详细解释,因过于详细,非精简版内容,适合小白进行观看

    scope:是控制fixture的作用范围

    scope中包含了4个功能

    1、function

    每一个函数都会调用,使用方式:在fixture函数上面增加@pytest.fixture()  括号中不用加function,不传的话默认为function

    2、class

    每一个类调用一次,每个类下的方法是funtion概念  使用方式:在fixture函数当面增加@pytest.fixture(scope='class')

    3、module

    每一个py文件调用一次,里面的方法和类,是class和function概念 使用方式:在fixture函数当面增加@pytest.fixture(scope='module')

    4、session

    多个py文件调用一次,每个py文件都是module的概念 使用方式:在fixture函数当面增加@pytest.fixture(scope='session')

      详解  

    函数调用fixture函数的时候,是前置的

    一、function

    下列代码中,可以看到,我们设置了个fixture-function函数,然后pytest中每个函数都可以调用fixture-function

    import pytest
    @pytest.fixture()
    def getsql_project():  #在我们需要设置的fxture上方增加@pytest.fixture()装饰器
        project_id = 335
        project_id2 = 332
        print('验证是否前置的执行')
        return project_id,project_id2
    
    
    def test_set_project1(getsql_project):    #我们要用fixture函数的时候,直接在括号中调用就好了
        print('第一个id是',getsql_project[0])
    
    def test_set_project2(getsql_project):
        print('第二个id是', getsql_project[1])
    
    if __name__ == '__main__':
        pytest.main(["-s","test_fixture.py"])

    返回结果如下

    ============================= test session starts =============================
    collecting ... collected 2 items
    
    test_fixture_scope.py::test_set_project1 验证是否前置的执行
    PASSED                          [ 50%]第一个id是 335
    
    test_fixture_scope.py::test_set_project2 验证是否前置的执行
    PASSED                          [100%]第二个id是 332
    
    
    ============================== 2 passed in 0.08s ==============================
    
    Process finished with exit code 0

    可以看到,每个方法都调用了一次fixture函数

     二、class

    一个类下只会触发一次

    import pytest
    @pytest.fixture(scope='class')
    def getsql_project():  #在我们需要设置的fxture上方增加@pytest.fixture()装饰器
        project_id = 335
        project_id2 = 332
        print('验证是否前置的执行')
        return project_id,project_id2
    
    class Test_fixture:
    
        def test_set_project1(self,getsql_project):    #我们要用fixture函数的时候,直接在括号中调用就好了
            print('第一个id是',getsql_project[0])
    
        def test_set_project2(self,getsql_project):
            print('第二个id是', getsql_project[1])
    
    if __name__ == '__main__':
        pytest.main(["-s","test_fixture.py"])

    返回结果

    ============================= test session starts =============================
    collecting ... collected 2 items
    
    test_fixture_scope.py::Test_fixture::test_set_project1 
    test_fixture_scope.py::Test_fixture::test_set_project2 
    
    ============================== 2 passed in 0.09s ==============================
    
    Process finished with exit code 0
    验证是否前置的执行
    PASSED            [ 50%]第一个id是 335
    PASSED            [100%]第二个id是 332

    可以看到,验证是否前置的执行,只被执行了一次,意味着我们2个方法都调用了fixture函数,实际只被执行了一次

    三、module

    一个py文件下只会执行一次

    我们首先看下,我们有多个类,每个类都调用下fixture函数

    import pytest
    @pytest.fixture(scope='module')
    def getsql_project():  #在我们需要设置的fxture上方增加@pytest.fixture()装饰器
        project_id = 335
        project_id2 = 332
        print('验证是否前置的执行')
        return project_id,project_id2
    
    class Test_fixture:
        def test_set_project1(self,getsql_project):    #我们要用fixture函数的时候,直接在括号中调用就好了
            print('第一个类id1',getsql_project[0])
        def test_set_project2(self,getsql_project):
            print('第一个类id2', getsql_project[1])
    if __name__ == '__main__':
        pytest.main(["-s","test_fixture.py"])
    
    class Test_fixture2:
        def test_set_project3(self,getsql_project):    #我们要用fixture函数的时候,直接在括号中调用就好了
            print('第二个类id1',getsql_project[0])
        def test_set_project4(self,getsql_project):
            print('第二个类id2', getsql_project[1])
    if __name__ == '__main__':
        pytest.main(["-s","test_fixture.py"])

    返回结果(可以看出,验证是否前置执行  被执行了2次,因为我们定义的class,每个类被执行一次所以执行了2次)

    ============================= test session starts =============================
    collecting ... collected 4 items
    
    test_fixture_scope.py::Test_fixture::test_set_project1 
    test_fixture_scope.py::Test_fixture::test_set_project2 
    test_fixture_scope.py::Test_fixture2::test_set_project3 验证是否前置的执行
    PASSED            [ 25%]第一个类id1 335
    PASSED            [ 50%]第一个类id2 332
    
    test_fixture_scope.py::Test_fixture2::test_set_project4 
    
    ============================== 4 passed in 0.09s ==============================
    
    Process finished with exit code 0
    验证是否前置的执行
    PASSED           [ 75%]第二个类id1 335
    PASSED           [100%]第二个类id2 332

    重点来了

    此时我们将class换成module

    @pytest.fixture(scope='module')

    返回结果(在整个py文件中,不论多个类调用,只被运行了一次)

    ============================= test session starts =============================
    collecting ... collected 4 items
    
    test_fixture_scope.py::Test_fixture::test_set_project1 
    test_fixture_scope.py::Test_fixture::test_set_project2 
    test_fixture_scope.py::Test_fixture2::test_set_project3 验证是否前置的执行
    PASSED            [ 25%]第一个类id1 335
    PASSED            [ 50%]第一个类id2 332
    
    test_fixture_scope.py::Test_fixture2::test_set_project4 
    
    ============================== 4 passed in 0.08s ==============================
    
    Process finished with exit code 0
    PASSED           [ 75%]第二个类id1 335
    PASSED           [100%]第二个类id2 332

    四、session

    一般我们这种fixture都卸载目录下的conftest.py文件下,如果有2个py文件都调用了conftest.py下的fixture函数,如果fixture是session形式,多个py可以用这一个函数返回的数据,但是不会重复调用。

    新建conftest.py 文件,里面去放入我们的fixture函数

    import pytest
    @pytest.fixture(scope='module')
    def getsql_project():  #在我们需要设置的fxture上方增加@pytest.fixture()装饰器
        project_id = 335
        project_id2 = 332
        print('验证是否前置的执行')
        return project_id,project_id2

    然后多个py都同时调用getsql_project  实际只被调用一次

    test_fixture_scope.py::Test_fixture::test_set_project1 
    test_fixture_scope.py::Test_fixture::test_set_project2 
    test_fixture_scope.py::Test_fixture2::test_set_project3 验证是否前置的执行
    PASSED            [ 25%]第一个类id1 335
    PASSED            [ 50%]第一个类id2 332
    
    test_fixture_scope.py::Test_fixture2::test_set_project4 
    
    ============================== 4 passed in 0.08s ==============================
    
    Process finished with exit code 0
    PASSED           [ 75%]第二个类id1 335
    PASSED           [100%]第二个类id2 332
  • 相关阅读:
    转载:@Html.ValidationSummary(true)
    转载:SQL中Group By 的常见使用方法
    转载:SQL按照日、周、月、年统计数据的方法
    级联删除
    视图是什么?
    数据冗余与外键
    源码网站汇总(转载)
    SQL语句的增删改查(详细)--转载
    Map的四种遍历方式
    HBase表预分区
  • 原文地址:https://www.cnblogs.com/ztcbug/p/15501283.html
Copyright © 2011-2022 走看看