微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – 有没有一种用pytest测试回调的首选方法?

我无法找到在文档,谷歌或此处使用pytest测试回调的具体示例.我发现了这个:What is the right way to test callback invocation using Python unittest?;但这是为了单位测试.我猜测pytest的monkeypatch功能是我应该看的地方,但我是新手自动测试,我正在寻找一个例子.

def foo(callback):
    callback('Buzz', 'Lightyear')

#--- pytest code ----
def test_foo():
    foo(hello)
    # how do I test that hello was called?
    # how do I test that it was passed the expected arguments?

def hello(first, last):
    return "hello %s %s" % first, last

先感谢您.

解决方法:

这个想法仍然是一样的.

您需要将hello()函数替换Mock,或者换句话说,“模拟”该函数.

然后,您可以使用assert_called_with()检查是否使用您需要的特定参数调用它.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐