首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >标记Pytest fixture而不是使用fixture的所有测试

标记Pytest fixture而不是使用fixture的所有测试
EN

Stack Overflow用户
提问于 2019-10-22 03:12:12
回答 1查看 697关注 0票数 3

有没有办法在PyTest夹具中定义标记?

当我在pytest中指定-m "not slow"时,我正在尝试禁用缓慢的测试。

我可以禁用单个测试,但不能禁用用于多个测试的fixture。

我的fixture代码如下所示:

代码语言:javascript
复制
@pytest.fixture()
@pytest.mark.slow
def postgres():
    # get a postgres connection (or something else that uses a slow resource)
    yield conn 

几个测试都有这样的一般形式:

代码语言:javascript
复制
def test_run_my_query(postgres):
    # Use my postgres connection to insert test data, then run a test
    assert ...

我在https://docs.pytest.org/en/latest/mark.html (updated link)上找到了下面的评论:

标记只能应用于测试,对夹具没有影响。这个评论的原因是,fixture本质上是函数调用,而标记只能在编译时指定吗?

有没有一种方法可以指定使用特定fixture (在本例中为postgres)的所有测试都可以标记为慢速,而无需在每个测试中指定@pytest.mark.slow

EN

回答 1

Stack Overflow用户

发布于 2019-10-22 03:42:20

看起来你已经在文档中找到了答案。订阅https://github.com/pytest-dev/pytest/issues/1368观看此功能,可能会在较新的pytest版本中添加。

现在,你可以采取一些办法来解决这个问题:

代码语言:javascript
复制
# in conftest.py

def pytest_collection_modifyitems(items):
    for item in items:
        if 'postgres' in getattr(item, 'fixturenames', ()):
            item.add_marker("slow")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58492764

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档