【发布时间】:2022-01-16 22:51:09
【问题描述】:
例如, 我将数据列表传递给函数。如何对 get_message、foo_function 和 yield 进行 pytest?
我认为我可以自行对 foo_function 进行 pytest,但很难找到如何测试一般迭代函数和专门使用 yield 的示例。
def get_message(records):
for record in records:
data = foo_function(record)
if data['message'] == 'BAR':
yield {
'result': 'OK',
'message': data['message']
}
【问题讨论】:
-
您可以将结果转换为列表(例如
list(get_message(records)))并针对结果列表进行测试。根据foo_function的复杂性,您可能还想模拟它。 -
你有时间看看我的answer吗?很高兴收到您的反馈。
标签: pytest python-3.7