testing-cabal / mock

The Python mock library

Home Page:https://docs.python.org/dev/library/unittest.mock.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add assert_not_called_with

valkheim opened this issue · comments

This would be nice to integrate an assert_not_called_with feature.

I had to implement it to test a publish/subscribe patterns where multiple
subscibers got called but not with the same arguments.

Here is my implementation:

def assert_not_called_with(self, *args, **kwargs):
    """assert that the mock was never called with the specified arguments.
    """
    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)
    )

An alternative would had been to iterate the call_args_list but it wouldn't result in a clean one-line assert

This repo is only for issues that are specific to the backport.
Please report issues with mock in the upstream bug tracker at https://bugs.python.org/.
Once issues are fixed upstream, they can be backported here.