【问题标题】:How can I access al the markers in a pytest fixture?如何访问 pytest 夹具中的所有标记?
【发布时间】:2020-04-28 01:28:19
【问题描述】:

我正在使用 pytest,我想用标记来标记我的测试,这些标记将指定在我的驱动程序中加载哪个页面的夹具。这很容易与行为上下文对象一起使用,但我找不到如何使用 pytest。

以这段代码为例

import pytest

@pytest.fixture
def text(request):
    if 'hello' in X:
        return 'found it!'
    return 'did not find it :('

@pytest.mark.hello
def test_hello(text):
    assert text == 'found it!'

X 应该是什么才能通过这个测试? 我试过request.node.own_markers,但这只是给了我一个空列表,即使我标记了测试。

【问题讨论】:

    标签: python pytest


    【解决方案1】:

    request.node.own_markersrequest.node.iter_markers() 可以让您访问节点的标记

    例如:

    (Pdb) request.node.own_markers
    [Mark(name='hello', args=(), kwargs={})]
    (Pdb) request.node.iter_markers()
    <generator object Node.iter_markers.<locals>.<genexpr> at 0x7f3a601a60a0>
    (Pdb) p list(request.node.iter_markers())
    [Mark(name='hello', args=(), kwargs={})]
    

    如果标记应用于更高的范围,这两个会有所不同(例如)

    markers 文档中有一些示例(没有使用 requestitem 在这些示例中相同(使用 pytest 挂钩))

    【讨论】:

      【解决方案2】:

      我通过玩耍找到了答案。夹具被标记为“范围=模块”,而我唯一的测试功能有标记。因此它超出了夹具的范围,因此是空列表。当我使夹具具有默认范围时,找到了标记。

      【讨论】:

        猜你喜欢
        • 2020-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-11
        • 1970-01-01
        • 1970-01-01
        • 2020-08-19
        相关资源
        最近更新 更多