• pytest之fixture使用详解


    简介:

      fixture区别于unnitest的传统单元测试(setup/teardown)有显著改进:

      1.有独立的命名,并通过声明它们从测试函数、模块、类或整个项目中的使用来激活。

      2.按模块化的方式实现,每个fixture都可以互相调用。

      3.fixture的范围从简单的单元测试到复杂的功能测试,可以对fixture配置参数,或者跨函数function,类class,模块module或整个测试session范围。

     fixture可以当做参数传入

    定义fixture跟定义普通函数差不多,唯一区别就是在函数上加个装饰器@pytest.fixture(),fixture命名不要以test开头,跟用例区分开。fixture是有返回值得,没有返回值默认为None。用例调用fixture的返回值,直接就是把fixture的函数名称当做变量名称。

    # 作者         :admin   
    import pytest
    @pytest.fixture()
    def test_01():
        a = 5
        return a
    
    def test_02(test_01):
        assert test_01 == 5
        print("断言成功")
    
    
    
    ============================= test session starts =============================
    platform win32 -- Python 3.7.2, pytest-4.0.2, py-1.8.0, pluggy-0.12.0
    rootdir: E:daimaProject_test	est_demo, inifile:
    plugins: allure-adaptor-1.7.10, html-1.22.0, metadata-1.8.0, rerunfailures-7.0
    collected 1 item
    
    test_demo_pytest_fixture.py 断言成功
    .
    
    ========================== 1 passed in 0.17 seconds ===========================

    使用多个fixture

    如果用例需要用到多个fixture的返回数据,fixture也可以返回一个元祖,list或字典,然后从里面取出对应数据。

    # 作者         :admin   
    import pytest
    @pytest.fixture()
    def test_01():
        a = 5
        b = 6
        return (a, b)
    
    def test_02(test_01):
        a = test_01[0]
        b = test_01[1]
        assert a < b
        print("断言成功")
    
    
    
    
    
    Launching py.test with arguments E:/daima/Project_test/test_demo/test_demo_pytest_fixture.py in E:daimaProject_test	est_demo
    
    ============================= test session starts =============================
    platform win32 -- Python 3.7.2, pytest-4.0.2, py-1.8.0, pluggy-0.12.0
    rootdir: E:daimaProject_test	est_demo, inifile:
    plugins: allure-adaptor-1.7.10, html-1.22.0, metadata-1.8.0, rerunfailures-7.0collected 1 item
    
    test_demo_pytest_fixture.py .断言成功
                                                [100%]
    
    ========================== 1 passed in 0.01 seconds ===========================
    Process finished with exit code 0
    

      

    fixture的作用范围(scope)

    ixture里面有个scope参数可以控制fixture的作用范围:session>module>class>function

    -function:每一个函数或方法都会调用

    -class:每一个类调用一次,一个类中可以有多个方法

    -module:每一个.py文件调用一次,该文件内又有多个function和class

    -session:是多个文件调用一次,可以跨.py文件调用,每个.py文件就是module

    代码示例:

    # 作者         :admin   
    import pytest
    @pytest.fixture(scope="class")
    def test_01():
        a = 5
        b = 6
        return (a, b)
    
    
    class TestNum:
        def test_02(self,test_01):
            a = test_01[0]
            b = test_01[1]
            assert a < b
            print("断言成功")
    
    
    Launching py.test with arguments E:/daima/Project_test/test_demo/test_demo_pytest_fixture.py in E:daimaProject_test	est_demo
    
    ============================= test session starts =============================
    platform win32 -- Python 3.7.2, pytest-4.0.2, py-1.8.0, pluggy-0.12.0
    rootdir: E:daimaProject_test	est_demo, inifile:
    plugins: allure-adaptor-1.7.10, html-1.22.0, metadata-1.8.0, rerunfailures-7.0collected 1 item
    
    test_demo_pytest_fixture.py .断言成功
                                                [100%]
    
    ========================== 1 passed in 0.02 seconds ===========================
    Process finished with exit code 0
    

      

    调用fixture的三种方法

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

    注释:代码详见上图

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

    # 作者         :admin   
    import pytest
    @pytest.fixture(scope="class")
    def test_01():
        a = 5
        b = 6
        return (a, b)
    
    @pytest.mark.usefixtures("test_01")
    class TestNum:
        def test_02(self,test_01):
            a = test_01[0]
            b = test_01[1]
            assert a < b
            print("断言成功")
    
    
    Launching py.test with arguments E:/daima/Project_test/test_demo/test_demo_pytest_fixture.py in E:daimaProject_test	est_demo
    
    ============================= test session starts =============================
    platform win32 -- Python 3.7.2, pytest-4.0.2, py-1.8.0, pluggy-0.12.0
    rootdir: E:daimaProject_test	est_demo, inifile:
    plugins: allure-adaptor-1.7.10, html-1.22.0, metadata-1.8.0, rerunfailures-7.0collected 1 item
    
    test_demo_pytest_fixture.py .断言成功
                                                [100%]
    
    ========================== 1 passed in 0.02 seconds ===========================
    Process finished with exit code 0

    3.叠加usefixtures

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

    # 作者         :admin   
    import pytest
    @pytest.fixture(scope="class")
    def test_01():
        a = 5
        b = 6
        return (a, b)
    
    @pytest.fixture(scope="class")
    def test_02():
        print("你是第二个执行")
    
    
    @pytest.mark.usefixtures("test_02")
    @pytest.mark.usefixtures("test_01")
    class TestNum:
        def test_03(self,test_01):
            a = test_01[0]
            b = test_01[1]
            assert a < b
            print("断言成功")
    
    
    Launching py.test with arguments E:/daima/Project_test/test_demo/test_demo_pytest_fixture.py in E:daimaProject_test	est_demo
    
    ============================= test session starts =============================
    platform win32 -- Python 3.7.2, pytest-4.0.2, py-1.8.0, pluggy-0.12.0
    rootdir: E:daimaProject_test	est_demo, inifile:
    plugins: allure-adaptor-1.7.10, html-1.22.0, metadata-1.8.0, rerunfailures-7.0collected 1 item
    
    test_demo_pytest_fixture.py 你是第二个执行
    .断言成功
                                                [100%]
    
    ========================== 1 passed in 0.02 seconds ===========================
    Process finished with exit code 0
    

      

  • 相关阅读:
    忍者X2简介+安装包+安装环境说明 [复制链接]
    拖拽的功能,可以看看这个
    openNI驱动控制kinect马达
    室内机器人漫游
    石头剪子布 C++多态实现
    PCL的KinectFusion开源实现
    Microsoft Kinect SDK中的Event Model
    小说下载阅读器_章节保存为XML并显示
    面试题:猫叫、老鼠跑、人醒的一点看法
    JQuery EasyUI之treegrid级联勾选的简单改进版
  • 原文地址:https://www.cnblogs.com/666666pingzi/p/11508152.html
Copyright © 2020-2023  润新知