如何访问 pytest 夹具中的所有标记? [英] How can I access al the markers in a pytest fixture?

查看:54
本文介绍了如何访问 pytest 夹具中的所有标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using pytest and I want to tag my tests with markers which will specify to a fixture which page to load in my driver. This works easily with the behave context object, but I cannot find how to do it with 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,但这只是给了我一个空列表,即使我标记了测试.

what should X be so that I can pass this test? I tried request.node.own_markers, but that just gives me an empty list, even though I marked the test.

推荐答案

我通过玩弄找到了答案.夹具被标记为scope=module",而我唯一的测试函数有标记.因此它超出了夹具的范围,因此是空列表.当我使夹具具有默认范围时,找到了标记.

I found the answer by playing around. The fixture was marked 'scope=module' while my only the test function had the marker. It was therefore out of scope for the fixture, hence the empty list. When I made the fixture have default scope, the marker was found.

这篇关于如何访问 pytest 夹具中的所有标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆