diff options
author | Berker Peksag <berker.peksag@gmail.com> | 2016-03-28 00:30:40 +0300 |
---|---|---|
committer | Berker Peksag <berker.peksag@gmail.com> | 2016-03-28 00:30:40 +0300 |
commit | e2982efa17f951cf5d3afc0d927ac45e8e2efd8d (patch) | |
tree | f687ffe3721a1c4cbfc4a58a9f778b23c086ef85 /Lib/unittest | |
parent | fd4e059645345fa0f4339dd44c09bfa3c76a82c5 (diff) | |
parent | fd8f7439f7071b95fb26c5fb84b4c73a259afb6f (diff) | |
download | cpython-e2982efa17f951cf5d3afc0d927ac45e8e2efd8d.tar.gz |
Issue #25195: Fix a regression in mock.MagicMock
_Call is a subclass of tuple (changeset 3603bae63c13 only works
for classes) so we need to implement __ne__ ourselves.
Patch by Andrew Plummer.
Diffstat (limited to 'Lib/unittest')
-rw-r--r-- | Lib/unittest/mock.py | 20 | ||||
-rw-r--r-- | Lib/unittest/test/testmock/testmock.py | 21 |
2 files changed, 40 insertions, 1 deletions
diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index cabae15214..7400fb7a64 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -772,6 +772,24 @@ class NonCallableMock(Base): (self._mock_name or 'mock', self.call_count)) raise AssertionError(msg) + def assert_called(_mock_self): + """assert that the mock was called at least once + """ + self = _mock_self + if self.call_count == 0: + msg = ("Expected '%s' to have been called." % + self._mock_name or 'mock') + raise AssertionError(msg) + + def assert_called_once(_mock_self): + """assert that the mock was called only once. + """ + self = _mock_self + if not self.call_count == 1: + msg = ("Expected '%s' to have been called once. Called %s times." % + (self._mock_name or 'mock', self.call_count)) + raise AssertionError(msg) + def assert_called_with(_mock_self, *args, **kwargs): """assert that the mock was called with the specified arguments. @@ -820,7 +838,7 @@ class NonCallableMock(Base): if expected not in all_calls: raise AssertionError( 'Calls not found.\nExpected: %r\n' - 'Actual: %r' % (calls, self.mock_calls) + 'Actual: %r' % (_CallList(calls), self.mock_calls) ) from cause return diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index 03c95de6cd..526eeab714 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -1239,6 +1239,27 @@ class MockTest(unittest.TestCase): with self.assertRaises(AssertionError): m.hello.assert_not_called() + def test_assert_called(self): + m = Mock() + with self.assertRaises(AssertionError): + m.hello.assert_called() + m.hello() + m.hello.assert_called() + + m.hello() + m.hello.assert_called() + + def test_assert_called_once(self): + m = Mock() + with self.assertRaises(AssertionError): + m.hello.assert_called_once() + m.hello() + m.hello.assert_called_once() + + m.hello() + with self.assertRaises(AssertionError): + m.hello.assert_called_once() + #Issue21256 printout of keyword args should be in deterministic order def test_sorted_call_signature(self): m = Mock() |