From b9b08cd948de97d756a199b60becce8397a8c882 Mon Sep 17 00:00:00 2001 From: Zackery Spytz Date: Wed, 8 May 2019 11:32:24 -0600 Subject: bpo-24758: Improve the error msg for unittest.mock.Mock()'s unsafe mode (#12991) * bpo-24758: Improve the error msg for unittest.mock.Mock()'s unsafe mode * Make the requested changes. --- Lib/unittest/mock.py | 3 ++- Lib/unittest/test/testmock/testmock.py | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index 1e8057d..47ed06c 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -572,7 +572,8 @@ class NonCallableMock(Base): raise AttributeError(name) if not self._mock_unsafe: if name.startswith(('assert', 'assret')): - raise AttributeError(name) + raise AttributeError("Attributes cannot start with 'assert' " + "or 'assret'") result = self._mock_children.get(name) if result is _deleted: diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index 5f917dd..b20b8e2 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -1453,9 +1453,10 @@ class MockTest(unittest.TestCase): #Issue21238 def test_mock_unsafe(self): m = Mock() - with self.assertRaises(AttributeError): + msg = "Attributes cannot start with 'assert' or 'assret'" + with self.assertRaisesRegex(AttributeError, msg): m.assert_foo_call() - with self.assertRaises(AttributeError): + with self.assertRaisesRegex(AttributeError, msg): m.assret_foo_call() m = Mock(unsafe=True) m.assert_foo_call() -- cgit v0.12