• pytest之fixture


    1、定义

    fixture修饰器来标记固定的工厂函数,在其他函数,模块,类或整个工程调用它时会被激活并优先执行,
    通常会被用于完成预置处理和重复操作。

    方法:fixture(scope="function", params=None, autouse=False, ids=None, name=None)
    常用参数:
       scope:被标记方法的作用域
             "function" (default):作用于每个测试方法,每个test都运行一次
              "class":作用于整个类,每个class的所有test只运行一次
              "module":作用于整个模块,每个module的所有test只运行一次
              "session:作用于整个session(慎用),每个session只运行一次
       params:(list类型)提供参数数据,供调用标记方法的函数使用
       autouse:是否自动运行,默认为False不运行,设置为True自动运行

    2、调用fixture的三种方法

    1.函数或类里面方法直接传fixture的函数参数名称

    2.使用装饰器@pytest.mark.usefixtures()修饰需要调用fixture的用例

      (1)叠加usefixtures

      如果一个方法或者一个class用例想要同时调用多个fixture,可以使用@pytest.mark.usefixture()进行叠加。注意叠加顺序,先执行的放底层,后执行的放上层。

      (2)usefixtures与传fixture区别

       如果fixture有返回值,那么usefixture就无法获取到返回值,这个是装饰器usefixture与用例直接传fixture参数的区别。

      当fixture需要用到return出来的参数时,只能讲参数名称直接当参数传入,不需要用到return出来的参数时,两种方式都可以。

    3.fixture自动使用autouse=True

    3、实例

    • 1.fixture第一个例子(通过参数引用)
        示例:
            import pytest
            class Test_ABC:
                @pytest.fixture()
                def before(self):
                    print("------->before")
                def test_a(self,before): # ⚠️ test_a方法传入了被fixture标识的函数,已变量的形式
                    print("------->test_a")
                    assert 1
            if __name__ == '__main__':
                pytest.main("-s  test_abc.py")
    
        执行结果:
            test_abc.py 
            ------->before # 发现before会优先于测试函数运行
            ------->test_a
            .
    
    • 2.fixture第二个例子(通过usefixture装饰器引用)
        示例:
            import pytest
            @pytest.fixture() # fixture标记的函数可以应用于测试类外部
            def before():
                print("------->before")
            @pytest.mark.usefixtures("before")
            class Test_ABC:
                def setup(self):
                    print("------->setup")
                def test_a(self):
                    print("------->test_a")
                    assert 1
            if __name__ == '__main__':
                pytest.main("-s  test_abc.py")
      
        执行结果:
            test_abc.py 
            ------->before # 发现before会优先于测试类运行
            ------->setup
            ------->test_a
            .
      
    • 3.fixture第三个例子(默认设置为运行)
        示例:
            import pytest
            @pytest.fixture(autouse=True) # 设置为默认运行
            def before():
                print("------->before")
            class Test_ABC:
                def setup(self):
                    print("------->setup")
                def test_a(self):
                    print("------->test_a")
                    assert 1
            if __name__ == '__main__':
                pytest.main("-s  test_abc.py")
    
        执行结果:
            test_abc.py 
            ------->before # 发现before自动优先于测试类运行
            ------->setup
            ------->test_a
            .
    
    • 4.fixture第四个例子(设置作用域为function)
        示例:
            import pytest
            @pytest.fixture(scope='function',autouse=True) # 作用域设置为function,自动运行
            def before():
                print("------->before")
            class Test_ABC:
                def setup(self):
                    print("------->setup")
                def test_a(self):
                    print("------->test_a")
                    assert 1
                def test_b(self):
                    print("------->test_b")
                    assert 1
            if __name__ == '__main__':
                pytest.main("-s  test_abc.py")
    
        执行结果:
            test_abc.py
            ------->before # 运行第一次
            ------->setup
            ------->test_a
            .------->before # 运行第二次
            ------->setup
            ------->test_b
            .
    
    • 5.fixture第五个例子(设置作用域为class)
        示例:
            import pytest
            @pytest.fixture(scope='class',autouse=True) # 作用域设置为class,自动运行
            def before():
                print("------->before")
            class Test_ABC:
                def setup(self):
                    print("------->setup")
                def test_a(self):
                    print("------->test_a")
                    assert 1
                def test_b(self):
                    print("------->test_b")
                    assert 1
            if __name__ == '__main__':
                pytest.main("-s  test_abc.py")
    
        执行结果:
            test_abc.py
            ------->before # 发现只运行一次
            ------->setup
            ------->test_a
            .
            ------->setup
            ------->test_b
            .

    4、使用yield执行teardown

    5、Addfinalizer终结函数

    除了yield可以实现teardown,在request-context对象中注册addfinalizer方法也可以实现终结函数。

     AddFinalizer的好处?

    (1)他可以注册多个终结函数。
    (2)这些终结方法总是会被执行,无论在之前的setup code有没有抛出错误。这个方法对于正确关闭所有的fixture创建的资源非常便利,即使其一在创建或获取时失败

    多个终结函数

  • 相关阅读:
    (68)zabbix windows性能计数器使用详解
    xenserver挂载新硬盘
    Centos7配置TiDB监控
    centos7下zabbix4.0配置磁盘IO监控
    XenServer7.6命令行导出导入虚拟机(迁移)
    Centos7修改分区空间
    Centos7安装xenserver tools
    Centos7配置TiDB集群
    Xenserver7.6修改root密码
    网络流量测试
  • 原文地址:https://www.cnblogs.com/crystal1126/p/12402961.html
Copyright © 2020-2023  润新知