首页 > 其他分享 >Pytest - Fixture(6) - 作用域混用/执行顺序/依赖关系

Pytest - Fixture(6) - 作用域混用/执行顺序/依赖关系

时间:2023-05-29 09:33:30浏览次数:33  
标签:作用域 Fixture fixture pytest test Pytest print def

Pytest - Fixture作用域混用

  • 若测试用例调用多个不同级别的作用域,都会同时生效:

conftest.py

import pytest

"""会话级别fixture,作用域当前目录"""
@pytest.fixture(scope="session")
def login_session():
    """作用于整个py文件"""
    print("\n*** session级别的作用域前置操作 ***")
    yield
    print("*** session级别的作用域后置操作 ***")

test_py.py

import pytest

"""用例级别fixture,作用域单个用例"""
@pytest.fixture(scope="function")
def login_func():
    print("\n--- function级别的作用域前置操作 ---")
    yield
    print("--- function级别的作用域后置操作 ---")

    
"""类级别fixture,作用域整个类"""
@pytest.fixture(scope="class", autouse=True)
def login_cls():
    print("\n=== class级别的作用域前置操作 ===")
    yield
    print("=== class级别的作用域后置操作 ===")

    
"""模块级别fixture,作用域整个py文件"""
@pytest.fixture(scope="module")
def login_module():
    print("\n^^^ module级别的作用域前置操作 ^^^")
    yield
    print("^^^ module级别的作用域后置操作 ^^^")
    

def test_s2(login_session, login_module, login_cls, login_func):
    print("\n用例test_s2:在类外的测试用例")

class Test_cls():
    def test_s1(self, login_func, login_cls, login_module, login_session):
        print("\n用例test_s1:在类中的测试用例")

    def test_s3(self, login_func):
        print("\n用例test_s3:在类中的测试用例")

if __name__ == '__main__':
    pytest.main(['-s', 'test_py.py'])


从执行结果中可以看出(测试用例test_s1,test_s2),不管测试用例调用Fixture函数顺序如何,执行顺序均为(从左到右分别为:先执行 > 后执行):

session > module > class > function


  • 执行结果如图所示各级别作用域:
    • 黄色框: function
    • 紫色框: class
    • 蓝色框: module
    • 红色框: session

image-20220922120921976



作用域执行顺序

  • 如果有多个不同作用域的需要执行,要弄清楚它们将执行的顺序;

  • pytest首先执行范围更高的fixtrue;

    • 在请求fixture函数中,先执行较高作用域范围的fixture(session,package,module);

    • 再执行在较低作用域的fixture(class,function);

test_py.py

import pytest

@pytest.fixture
def func(scope="function"):
    print("Function")

@pytest.fixture(scope="class")
def cls():
    print("Class")

@pytest.fixture(scope="module")
def mod():
    print("Module")

@pytest.fixture(scope="package")
def pack():
    print("Package")

@pytest.fixture(scope="session")
def sess():
    print("\nSession")

class TestClass:
    # 将作用域的顺序打乱,查看执行结果
    def test_s1(self, cls, mod, sess, func, pack):
        print("\n测试用例")

if __name__ == "__main__":
    pytest.main("-s", "test_py.py")
  • 执行顺序结果如图,按照作用域的高至低的顺序为:

    • Seesion > Package > Module > Class > Function

    image-20230306152055734



作用域依赖关系

  • 添加了 @pytest.fixture ,如果fixture还想依赖其他fixture,需要用函数传参的方式:

    • 当一个函数请求另一个函数时,首先执行另一个函数。
    • 如果函数 b 请求函数 a,函数 a 将首先执行,因为 b依赖于 a ,没有 a 就无法运行。
    • 即使 b 不需要 a的结果 ,它仍然可以请求 a 是否需要,确保在之后执行 a

    test_py.py

    import pytest
    
    @pytest.fixture()
    def a():
        print("\n第一个Fixture:a")
    
    @pytest.fixture()
    def b(a):
        print("第二个Fixture:b")
    
    @pytest.fixture()
    def c(a, b):
        print("第三个Fixture:c")
    
    @pytest.fixture()
    def d(a):
        print("第四个Fixture:d")
    
    @pytest.fixture()
    def z(d, c):
        print("第五个Fixture:z")
    
    # 测试用例只调用 函数z
    def test_s1(z):
        print("\n用例test_s1:Fixture 相互调用")
    
    if __name__ == '__main__':
        pytest.main(['-s', 'test_py.py'])
    
  • 执行依赖关系结果如图:

    • 单个fixture被多个fixture调用,只会执行一次;例如函数a 被函数b、c、d调用,但只执行了一次;

    • 优先执行顺序靠前的fixture;例如def z(d, c):,优先执行函数 d

    • 优先执行最先被调用的fixture;例如上述,函数a 为最先被 函数 d 调用的,函数d 为最先被 函数 z 调用;

    image-20230306175754122


标签:作用域,Fixture,fixture,pytest,test,Pytest,print,def
From: https://www.cnblogs.com/mzline/p/17439486.html

相关文章

  • pytest
    一、web自动化内容UI-userinterfaceweb网页、客户端(PC,app)1、pytest2、seleniumselenium介绍、元素定位、元素操作python+selenium:框架:PageObject模式优化和封装-selenium二次封装、失败截图、执行过程输出、异常信息输出selenium原理appium##################......
  • Pytest - Fixture(5) - 作用域(scope)
    Pytest-Fixture作用域(scope)之前讲过,fixture里面有个scop参数,这个参数就是填写fixture作用域的;function:每一个函数或方法都会调用;class:每一个类调用一次,一个类中可以有多个方法;module:每一个.py文件调用一次,该文件内又有多个function和class;session:是多个文......
  • Pytest - Fixture(3) - yield遇到异常
    Pytest-yield遇到异常如果yield前面的代码,即setup部分已经抛出异常了,则不会执行yield后面的teardown内容;如果测试用例抛出异常,yield后面的teardown内容还是会正常执行;test_py.pyimportpytest#配置自动运行的[email protected](scope="function",autouse=Tr......
  • python 名称空间与作用域(笔记整理)
    一、名称空间什么是名称空间:名称空间就是存放变量名和变量值绑定关系的地方就是内存地址在程序执行期间最多会存在三种名称空间:内置名称空间:是Python解释器默认预定义大量内置函数和内置异常的名称空间,就是存放解释器自带函数方法的名称空间可以通过dir(builtins)来......
  • 【深度剖析】JavaScript中块级作用域与函数作用域
    前言系列首发于公众号『前端进阶圈』,若不想错过更多精彩内容,请“星标”一下,敬请关注公众号最新消息。面试官必问系列:深入理解JavaScript块和函数作用域在JavaScript中,究竟是什么会生成一个新的作用域,只有函数才会生成新的作用域吗?那JavaScript其他结构能生成新的作用域吗?3.1......
  • Pytest - Fixture(2) - 实现setup和teardown(yield)
    Fixture实现setup和teardown(yield)用fixture实现teardown并不是一个独立的函数,而是用yield函数来开启teardown操作;具体yield是什么,可以去度娘一下~test_py.pyimportpytest#配置自动运行的[email protected](scope="function",autouse=True)defo......
  • Pytest - Fixture(1) - 入门&概述
    Pytest-Fixture入门&概述之前讲过测试用例的前置和后置的方法,可以使用setup和teardown函数实现,但是这种方法是用于全部测试用例的;当我有部分测试用例不需要setup的方法该怎么办?pytest提供了fixture方法,让我们可以自定义测试用例的前置及后置条件;还可以根据配置......
  • Pytest - setup 和 teardown
    Pytest-setup和teardown执行用例肯定有些需要前置条件或后置操作,例如前置的用户登陆,后置的清理数据等操作;unittest提供了两种前置(setup、setupClass)和两种后置(teardown、teardownClass);相比之下,pytest提供了十种setup和teardown方法:模块级别:setup_module、te......
  • Pytest - 断言判断(2) - 断言失败继续执行(pytest-assume)
    断言失败继续执行前言一般情况下我们在使用assert断言失败后,后面的代码就不会继续运行;如果我们想要在断言失败后想要继续运行代码,就不能使用assert进行验证;一个可以允许pytest测试用例中,执行多个失败的断言的插件:多重断言pytest-assume安装pipinstallpytest......
  • Pytest - pytest 命令(1) - 命令执行方法
    命令执行方法讲解下pytest分别在Windows,Linux,Pycharm中,执行pytest的方法;追加的参数可以参考:Pytest-pytest命令(2)-命令参数及含义Pytest-pytest命令(3)-常用命令的使用Windows执行Windows下执行pytest测试脚本没什么难度,在用例的目录下打开cmd窗口......