Python内置固定装置

时间:2019-09-06 10:51:54

标签: python pytest


我正在尝试运行使用以下功能的pytest:

def storage_class(request):

    def fin():
        sc.delete()

    request.addfinalizer(fin)

    logger.info("Creating storage")
    data = {'api_version': 'v1', 'kind': 'namespace'}
    # data is ususally loaded from yaml template
    sc = OCS(**data)
    return sc

我在项目中找不到名为“ request”的灯具,因此我认为它是内置的灯具。但是,我在文档中进行了搜索,但是找不到“请求”内置装置:https://docs.pytest.org/en/latest/builtin.html 任何人都可以对这种(内置式)灯具有所了解吗? 谢谢!

1 个答案:

答案 0 :(得分:1)

request固定装置有助于获取有关上下文的信息。

more on request fixture

Example for request fixture

请求处理程序最常用的用法是addfinalizerconfig

如果只需要teardown功能,则只需使用yield并摆脱request固定装置。

@pytest.fixture()
def storage_class():

    logger.info("Creating storage")
    data = {'api_version': 'v1', 'kind': 'namespace'}
    sc = OCS(**data)
    yield sc

    # Any code after yield will give you teardown effect
    sc.delete()