【问题标题】:How are pytest fixure scopes intended to work?pytest fixture 作用域是如何工作的?
【发布时间】:2022-11-30 01:42:06
【问题描述】:

我想使用 pytest fixtures 来准备一个我想在一组测试中使用的对象。 我跟随 documentation 并在 something_fixture.py 中创建一个夹具,其范围设置为会议像这样:

import pytest

@pytest.fixture(scope="session")
def something():
    return 'something'

然后在 test_something.py 中,我尝试像这样使用夹具:

def test_something(something):
    assert something == 'something'

哪个不起作用,但是如果我像这样导入夹具:

from tests.something_fixture import something


def test_something(something):
    assert something == 'something'

测试通过...

这个是进口的吗必要的?因为对我来说,根据文档,这还不清楚。

【问题讨论】:

    标签: python pytest fixtures


    【解决方案1】:

    something_fixture.py中创建一个夹具

    你已经在某个地方定义了夹具,它没有被“注意到”,因为 Python 没有理由导入这个模块。默认收集模式考虑名称匹配 these glob patterns 的测试文件:

    - test_*.py
    - *_test.py
    

    由于这是一个会话范围的功能,请在 conftest.py 文件中定义它,这样它将在测试收集时创建。

    【讨论】:

      猜你喜欢
      • 2022-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-27
      • 1970-01-01
      • 2018-01-25
      • 2018-09-13
      相关资源
      最近更新 更多