最佳答案
                                        
                                                                        
                                我正在使用 Mock 库来测试我的应用程序,但是我想声明某个函数没有被调用。Mock 文档讨论了类似于 mock.assert_called_with和 mock.assert_called_once_with的方法,但是我没有找到类似于 mock.assert_not_called的方法或者与校验 Mock 是否为 没打电话相关的方法。
我可以用下面这样的东西,虽然它看起来既不酷也不蟒蛇:
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
有什么办法吗?
 
                                
                             
                                
                             
                                
                             
                                
                             
                                
                             
                                
                            