【发布时间】:2012-08-24 14:32:10
【问题描述】:
我正在使用 Mock 库来测试我的应用程序,但我想断言某些函数没有被调用。 Mock 文档讨论了 mock.assert_called_with 和 mock.assert_called_once_with 之类的方法,但我没有找到类似 mock.assert_not_called 或与验证 mock 是否未调用相关的任何内容。
我可以使用类似以下的内容,尽管它看起来既不酷也不像 Python:
def test_something:
# some actions
with patch('something') as my_var:
try:
# args are not important. func should never be called in this test
my_var.assert_called_with(some, args)
except AssertionError:
pass # this error being raised means it's ok
# other stuff
任何想法如何做到这一点?
【问题讨论】:
-
正如@Ahmet 在他的回答中指出的那样,现在支持 assert_not_call ,也在后端 (docs.python.org/3/library/…) 中。
标签: python unit-testing mocking python-mock