如何在pytest固定装置中访问所有标记?

时间:2020-01-10 11:38:35

标签: python pytest

我正在使用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,但是即使我标记了测试,也只能给我一个空列表。

2 个答案:

答案 0 :(得分: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文档中有一些示例(没有一个使用request,但是item在这些示例中是相同的(它们使用pytest钩子))

答案 1 :(得分:0)

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