3

我在 GitHub 上问了同样的问题

我从 s0undt3ch 那里了解到pytest-helpers-namespace在他非常有帮助的回答中。但是我发现了一个用例,我似乎找不到明显的解决方法。这是我在 GitHub 上的原始问题的粘贴。


如何在我的辅助函数中使用已在我的 conftest 中声明的固定装置?在所有测试中,我都有一个大的、内存重的配置对象(为简单起见,是一个字典),但我不想拆掉它并重建这个对象,因此将其限定为会话并重用。很多时候,我想从我的测试中的配置对象中获取值。

我知道在固定装置中重用固定装置,你必须传递一个参考

# fixtures
@pytest.fixture(scope="session")
def return_dictionary():
    return {
        "test_key": "test_value"
    }

@pytest.fixture(scope="session")
def add_random(return_dictionary):
    _temp = return_dictionary
    _temp["test_key_random"] = "test_random_value"
    return _temp

是不是因为pytest收集了类似的装饰器,一起分析?我希望有人对此提出意见。谢谢!

这是我创建的一些文件,用于演示我在寻找什么,以及我看到了什么错误。

# conftest.py
import pytest
from pprint import pprint
pytest_plugins = ["helpers_namespace"]

# fixtures
@pytest.fixture(scope="session")
def return_dictionary():
    return {
        "test_key": "test_value"
    }

# helpers
@pytest.helpers.register
def super_print(_dict):
    pprint(_dict)

@pytest.helpers.register
def super_print_always(key, _dict=return_dictionary):
    pprint(_dict[key])
# test_check.py
import pytest

def test_option_1(return_dictionary):
    print(return_dictionary)


def test_option_2(return_dictionary):
    return_dictionary["test_key_2"] = "test_value_2"
    pytest.helpers.super_print(return_dictionary)


def test_option_3():
    pytest.helpers.super_print_always('test_key')
key = 'test_key', _dict = <function return_dictionary at 0x039B6C48>

    @pytest.helpers.register
    def super_print_always(key, _dict=return_dictionary):
>       pprint(_dict[key])
E       TypeError: 'function' object is not subscriptable

conftest.py:30: TypeError
4

0 回答 0