pytest-patch-with-lambda (PT008)
源自 flake8-pytest-style linter。
作用
检查模拟调用是否使用了虚拟 lambda
函数,而不是 return_value
。
为什么这不好?
在 patching 调用时,显式的 return_value
比 lambda
函数更好地传达意图,假设 lambda
不使用传递给它的参数。
return_value
对已 patching 的函数的签名更改也具有鲁棒性,并允许进行其他断言以验证行为。例如,return_value
允许通过 assert_called_once_with
和相关方法验证调用次数或传递给已 patching 函数的参数。
示例
建议改为
def test_foo(mocker):
mocker.patch("module.target", return_value=7)
# If the lambda makes use of the arguments, no diagnostic is emitted.
mocker.patch("module.other_target", lambda x, y: x)