py3笔记3:pytest之fixture

1. fixture优势

相对于setup和teardown的优势:

  1. 命名方式灵活,不局限于setup和teardown
  2. conftest.py配置里可以实现数据共享,不需要import就可以自动找到一些配置
  3. scope='module'可以实现多个.py跨文件共享前置
  4. scope='session'以实现多个.py跨文件使用一个session来实现多个用例

2. fixture的使用

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,
)

Decorator to mark a fixture factory function.
    可以使用此装饰器(带或不带参数)来定义fixture功能
    This decorator can be used, with or without parameters, to define a
    fixture function.
    
    fixture的功能名称,可以在以后使用,引用它会在运行测试之前调用它
    The name of the fixture function can later be referenced to cause its
    invocation(调用) ahead of running tests: test modules or classes can use the
    ``pytest.mark.usefixtures(fixturename)`` marker.

    测试功能可以直接使用fixture名称作为输入参数,在这种情况下,fixture实例从fixture返回功能将被注入
    Test functions can directly use fixture names as input arguments in which
    case the fixture instance returned from the fixture function will be
    injected(注射).

    Fixtures can provide their values to test functions using ``return`` or
    ``yield`` statements. When using ``yield`` the code block after the
    ``yield`` statement is executed as teardown code regardless of the test
    outcome, and must yield exactly once.

    :param scope:四个级别参数
        The scope for which this fixture is shared; one of ``"function"``
        (default), ``"class"``, ``"module"``, ``"package"`` or ``"session"``.

        This parameter may also be a callable which receives ``(fixture_name, config)``
        as parameters, and must return a ``str`` with one of the values mentioned above.

        See :ref:`dynamic scope` in the docs for more information.

    :param params:
        An optional list of parameters which will cause multiple invocations(调用)
        of the fixture function and all of the tests using it. The current
        parameter is available in ``request.param``.

    :param autouse:
        If True, the fixture func is activated for all tests that can see it.
        If False (the default), an explicit(显式) reference is needed to activate
        the fixture.

    :param ids:
        List of string ids each corresponding(相应的) to the params so that they are
        part of the test id. If no ids are provided they will be generated
        automatically from the params.

    :param name:fixture的名称,默认装饰函数的名称
        The name of the fixture. This defaults to the name of the decorated
        function. If a fixture is used in the same module in which it is
        defined, the function name of the fixture will be shadowed by the
        function arg that requests the fixture; one way to resolve this is to
        name the decorated function ``fixture_<fixturename>`` and then use
        ``@pytest.fixture(name='<fixturename>')``.

3. 示例

通过scope参数控制setup级别

# -*- coding:utf-8 -*-
"""
@author:百草Lily
@file:test_1.py
@time:2021/6/13
"""
# 实现场景:用例1需要先登录,用例2不需要登录,用例3需要先登录
import pytest


@pytest.fixture()  # 不带参数,scope=function默认,即对函数有效
def login():
    print("login fist!")

def test_1(login):
    print("test 1:先登录")
def test_2():
    print("test 2:不需要登录")
def test_3(login):
    print("test 3:先登录")
if __name__ =="__main__":
    pytest.main(["-s", "test_1.py"])
执行结果

4. conftest.py配置

多个.py文件调用公共模块时,公共模块不能写到用例中
需要一个配置文件,单独管理一些预置的操作场景,pytest里面漠然读取conftest.py里面的配置

注意
  • conftest.py配置脚本名称是固定的,不能改名称
  • conftest.py与运行的用例要放置同一package下并且有__init__.py文件
  • 不需要import 导入conftest.py,pytest会自动查找
    不然无法找到对应的fixture
    conftest.py
# -*- coding:utf-8 -*-
"""
@author:百草Lily
@file:conftest.py
@time:2021/6/15
"""
import pytest
@pytest.fixture()
def login():
    print("fixture: login")

test_1.py

# -*- coding:utf-8 -*-
"""
@author:百草Lily
@file:test_1.py
@time:2021/6/13
"""
import pytest
def test_1(login):
    print("test 1:先登录")
def test_2():
    print("test 2:不需要登录")
def test_3(login):
    print("test 3:先登录")
if __name__ == "__main__":
    pytest.main(["-s", "test_1.py"])

fixture之 yield实现teardown

fixture中使用yield唤起teardown操作

5. scope='module'

  1. 参数 fixture参数scope='module',module作用是整个.py文件都会生效,用例调用时,参数写上函数名就行
    上述示例,conftest.py修改,添加传参scope='module'后执行结果如下:
    scope='module'时,执行结果

    注:它只会在第一个用例前执行一次
    如修改,test_1(),test_3()不调用,仅test_2(login)调用
    仅test_2调用时,执行结果

6. yield执行teardown

# -*- coding:utf-8 -*-
"""
@author:百草Lily
@file:conftest.py
@time:2021/6/15
"""
import pytest
@pytest.fixture(scope="module")
def login():
    print("fixture: login")
    yield  # yield唤起teardown
    print("yield: log out!")

执行结果如下:


yield唤起teardown

7. yeild遇到异常

#修改test_2(),抛出异常
def test_2():
    print("test 2:不需要登录")
    raise NameError
case异常时,yield唤起
  1. 用例异常,则不影响yield后面的teardown内容;运行结果互不影响,并且在用例全部执行完之后,会唤起teardown的内容(如上)
  2. 若setup时异常,则不会执行yield后面的teardown内容


    setup时error,则yield后teardown不执行
  3. yield也可以配合with语句使用

7. addfinalizer终结函数

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

addfinalizer VS yield
  • addfinalizer可以注册多个终结函数
  • addfinalizer中终结方法都会被执行,无论setup 部分是否报错。这个方法对于正确关闭所有的fixture创建的资源非常便利,即使其一在创建或获取时失败

参考1:https://mp.weixin.qq.com/s/OSVEhsMIzLBY04Nz-sbrPg
参考2:https://mp.weixin.qq.com/s/GJLIYRcd_CG0BsfdMlC9wQ

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前面一篇讲了setup、teardown可以实现在执行用例前或结束后加入一些操作,但这种都是针对整个脚本全局生效的...
    C1R2阅读 5,033评论 0 0
  • 目录: 安装及入门 使用和调用方法 原有TestSuite使用方法 断言的编写和报告 Pytest fixture...
    韩志超阅读 12,524评论 3 10
  • fixture 是在测试函数运行前后由 pytest 执行的外壳函数,可以定制包括定义传入测试中的数据集,配置测试...
    djz0306阅读 10,927评论 0 3
  • 表情是什么,我认为表情就是表现出来的情绪。表情可以传达很多信息。高兴了当然就笑了,难过就哭了。两者是相互影响密不可...
    Persistenc_6aea阅读 127,307评论 2 7
  • 16宿命:用概率思维提高你的胜算 以前的我是风险厌恶者,不喜欢去冒险,但是人生放弃了冒险,也就放弃了无数的可能。 ...
    yichen大刀阅读 11,296评论 0 4