pytest fixture yield list of dict throws“yield_fixture函数有多个‘yield’:”

2024-05-12 12:33:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我定义了一个固定装置,如:

    @pytest.fixture
    def mock_yield_data(self):
        for data in [
            {
                1: 2, 2: 3
            },
            {
                2: 4, 4: 5
            },
        ]:
            yield data

以及一种测试方法,如:

^{pr2}$

断言成功,但teardown抛出yield_fixture function has more than one 'yield':。在

==================================================================================================== ERRORS ====================================================================================================
_________________________________________________________________________ ERROR at teardown of TestClass.test_fixture _________________________________________________________________________
yield_fixture function has more than one 'yield':

Tags: selfdata定义pytestdefmorefunctionmock
1条回答
网友
1楼 · 发布于 2024-05-12 12:33:43

pytest.yieldfixture文档的最后一节中:

  • usually yield is used for producing multiple values. But fixture functions can only yield exactly one value. Yielding a second fixture value will get you an error. It’s possible we can evolve pytest to allow for producing multiple values as an alternative to current parametrization. For now, you can just use the normal fixture parametrization mechanisms together with yield-style fixtures.

内存占用这么小,您应该只返回整个内容:

@pytest.fixture
def mock_yield_data(self):
    return [
        {
            1: 2, 2: 3
        },
        {
            2: 4, 4: 5
        },
    ]

相关问题 更多 >