Skip to main content
deleted 5 characters in body
Source Link
blhsing
  • 109.2k
  • 9
  • 88
  • 132

You can add a assert_not_called_with method to unittest.mock.Mock on your own:

from unittest.mock import Mock def assert_not_called_with(self, *args, **kwargs): try: self.assert_called_with(*args, **kwargs) except AssertionError as e: return raise AssertionError('Expected %s to not have been called.' % self._format_mock_call_signature(args, kwargs)) Mock.assert_not_called_with = assert_not_called_with 

so that:

m = Mock() m.assert_not_called_with(1, 2, a=3) m(3, 4, b=5) m.assert_not_called_with(3, 4, b=5) 

outputs:

AssertionError: Expected mock(3, 4, b=5) to not have been called. 

You can add a assert_not_called_with method to unittest.mock.Mock on your own:

from unittest.mock import Mock def assert_not_called_with(self, *args, **kwargs): try: self.assert_called_with(*args, **kwargs) except AssertionError as e: return raise AssertionError('Expected %s to not have been called.' % self._format_mock_call_signature(args, kwargs)) Mock.assert_not_called_with = assert_not_called_with 

so that:

m = Mock() m.assert_not_called_with(1, 2, a=3) m(3, 4, b=5) m.assert_not_called_with(3, 4, b=5) 

outputs:

AssertionError: Expected mock(3, 4, b=5) to not have been called. 

You can add a assert_not_called_with method to unittest.mock.Mock on your own:

from unittest.mock import Mock def assert_not_called_with(self, *args, **kwargs): try: self.assert_called_with(*args, **kwargs) except AssertionError: return raise AssertionError('Expected %s to not have been called.' % self._format_mock_call_signature(args, kwargs)) Mock.assert_not_called_with = assert_not_called_with 

so that:

m = Mock() m.assert_not_called_with(1, 2, a=3) m(3, 4, b=5) m.assert_not_called_with(3, 4, b=5) 

outputs:

AssertionError: Expected mock(3, 4, b=5) to not have been called. 
Source Link
blhsing
  • 109.2k
  • 9
  • 88
  • 132

You can add a assert_not_called_with method to unittest.mock.Mock on your own:

from unittest.mock import Mock def assert_not_called_with(self, *args, **kwargs): try: self.assert_called_with(*args, **kwargs) except AssertionError as e: return raise AssertionError('Expected %s to not have been called.' % self._format_mock_call_signature(args, kwargs)) Mock.assert_not_called_with = assert_not_called_with 

so that:

m = Mock() m.assert_not_called_with(1, 2, a=3) m(3, 4, b=5) m.assert_not_called_with(3, 4, b=5) 

outputs:

AssertionError: Expected mock(3, 4, b=5) to not have been called.