Assert a function/method was not called using Mock

后端 未结 6 1627
深忆病人
深忆病人 2020-12-12 19:50

I\'m using the Mock library to test my application, but I want to assert that some function was not called. Mock docs talk about methods like mock.assert_called_with

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-12 20:21

    This should work for your case;

    assert not my_var.called, 'method should not have been called'
    

    Sample;

    >>> mock=Mock()
    >>> mock.a()
    
    >>> assert not mock.b.called, 'b was called and should not have been'
    >>> assert not mock.a.called, 'a was called and should not have been'
    Traceback (most recent call last):
      File "", line 1, in 
    AssertionError: a was called and should not have been
    

提交回复
热议问题