diff options
author | Susan Su <susansu.software@gmail.com> | 2019-02-13 18:22:29 -0800 |
---|---|---|
committer | Chris Withers <chris@withers.org> | 2019-04-30 08:39:55 +0100 |
commit | 3830cd5a0d3b10d95c7b717505f266556202a518 (patch) | |
tree | a435501a715ebf7173a9411e74789076a6cde065 | |
parent | dde37a2af22d6bae81d4c3736567c24ebed46fa7 (diff) | |
download | mock-3830cd5a0d3b10d95c7b717505f266556202a518.tar.gz |
bpo-35500: align expected and actual calls on mock.assert_called_with error message. (GH-11804)
Backports: 2bdd5858e3f89555c8de73a0f307d63536129dbd
Signed-off-by: Chris Withers <chris@withers.org>
-rw-r--r-- | NEWS.d/2019-02-10-00-00-13.bpo-35500.1HOMmo.rst | 1 | ||||
-rw-r--r-- | mock/mock.py | 7 | ||||
-rw-r--r-- | mock/tests/testmock.py | 15 |
3 files changed, 14 insertions, 9 deletions
diff --git a/NEWS.d/2019-02-10-00-00-13.bpo-35500.1HOMmo.rst b/NEWS.d/2019-02-10-00-00-13.bpo-35500.1HOMmo.rst new file mode 100644 index 0000000..16b0fbf --- /dev/null +++ b/NEWS.d/2019-02-10-00-00-13.bpo-35500.1HOMmo.rst @@ -0,0 +1 @@ +Write expected and actual call parameters on separate lines in :meth:`unittest.mock.Mock.assert_called_with` assertion errors. Contributed by Susan Su. diff --git a/mock/mock.py b/mock/mock.py index bbf2314..8d18e9b 100644 --- a/mock/mock.py +++ b/mock/mock.py @@ -874,7 +874,7 @@ class NonCallableMock(Base): def _format_mock_failure_message(self, args, kwargs): - message = 'Expected call: %s\nActual call: %s' + message = 'expected call not found.\nExpected: %s\nActual: %s' expected_string = self._format_mock_call_signature(args, kwargs) call_args = self.call_args if len(call_args) == 3: @@ -944,7 +944,10 @@ class NonCallableMock(Base): self = _mock_self if self.call_args is None: expected = self._format_mock_call_signature(args, kwargs) - raise AssertionError('Expected call: {}\nNot called'.format(expected)) + actual = 'not called.' + error_message = ('expected call not found.\nExpected: %s\nActual: %s' + % (expected, actual)) + raise AssertionError(error_message) def _error_message(cause): msg = self._format_mock_failure_message(args, kwargs) diff --git a/mock/tests/testmock.py b/mock/tests/testmock.py index b496a36..c35ae6e 100644 --- a/mock/tests/testmock.py +++ b/mock/tests/testmock.py @@ -749,7 +749,7 @@ class MockTest(unittest.TestCase): def test_assert_called_with_message(self): mock = Mock() - self.assertRaisesRegexp(AssertionError, 'Not called', + self.assertRaisesRegexp(AssertionError, 'not called', mock.assert_called_with) @@ -978,10 +978,11 @@ class MockTest(unittest.TestCase): def test_assert_called_with_failure_message(self): mock = NonCallableMock() + actual = 'not called.' expected = "mock(1, '2', 3, bar='foo')" - message = 'Expected call: %s\nNot called' + message = 'expected call not found.\nExpected: %s\nActual: %s' self.assertRaisesWithMsg( - AssertionError, message % (expected,), + AssertionError, message % (expected, actual), mock.assert_called_with, 1, '2', 3, bar='foo' ) @@ -994,7 +995,7 @@ class MockTest(unittest.TestCase): for meth in asserters: actual = "foo(1, '2', 3, foo='foo')" expected = "foo(1, '2', 3, bar='foo')" - message = 'Expected call: %s\nActual call: %s' + message = 'expected call not found.\nExpected: %s\nActual: %s' self.assertRaisesWithMsg( AssertionError, message % (expected, actual), meth, 1, '2', 3, bar='foo' @@ -1004,7 +1005,7 @@ class MockTest(unittest.TestCase): for meth in asserters: actual = "foo(1, '2', 3, foo='foo')" expected = "foo(bar='foo')" - message = 'Expected call: %s\nActual call: %s' + message = 'expected call not found.\nExpected: %s\nActual: %s' self.assertRaisesWithMsg( AssertionError, message % (expected, actual), meth, bar='foo' @@ -1014,7 +1015,7 @@ class MockTest(unittest.TestCase): for meth in asserters: actual = "foo(1, '2', 3, foo='foo')" expected = "foo(1, 2, 3)" - message = 'Expected call: %s\nActual call: %s' + message = 'expected call not found.\nExpected: %s\nActual: %s' self.assertRaisesWithMsg( AssertionError, message % (expected, actual), meth, 1, 2, 3 @@ -1024,7 +1025,7 @@ class MockTest(unittest.TestCase): for meth in asserters: actual = "foo(1, '2', 3, foo='foo')" expected = "foo()" - message = 'Expected call: %s\nActual call: %s' + message = 'expected call not found.\nExpected: %s\nActual: %s' self.assertRaisesWithMsg( AssertionError, message % (expected, actual), meth ) |