tri*_*Ant 7 python fixtures pytest
我定义了一个固定装置,例如:
@pytest.fixture
def mock_yield_data(self):
for data in [
{
1: 2, 2: 3
},
{
2: 4, 4: 5
},
]:
yield data
Run Code Online (Sandbox Code Playgroud)
和一个测试方法,如:
def test_fixture(self, mock_yield_data):
for data in mock_yield_data:
assert data
Run Code Online (Sandbox Code Playgroud)
断言成功,但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':
Run Code Online (Sandbox Code Playgroud)
您的灯具可能只有一个yield。但是,您可以将参数传递给固定装置,固定装置将为每个参数运行固定装置。
all_data = [
{
1: 2, 2: 3
},
{
2: 4, 4: 5
}
]
@pytest.fixture(params=all_data)
def mock_yield_data(self, request):
yield request.param
Run Code Online (Sandbox Code Playgroud)
在pytest.yieldfixture文档的最后一部分中:
\n\n\n\n\n\n
\n- 通常,yield 用于产生多个值。但固定功能只能产生一个值。产生第二个固定值将会导致错误。我们可能可以进化 pytest 以允许生成多个值作为当前参数化的替代方案。目前,您可以将普通的夹具参数化机制与屈服式夹具一起使用。
\n
由于内存占用这么小,您应该返回整个内容:
\n\n@pytest.fixture\ndef mock_yield_data(self):\n return [\n {\n 1: 2, 2: 3\n },\n {\n 2: 4, 4: 5\n },\n ]\nRun Code Online (Sandbox Code Playgroud)\n