Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b9b08cd

Browse files
ZackerySpytzkushaldas
authored andcommitted
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.
1 parent 6bd8173 commit b9b08cd

2 files changed

Lines changed: 5 additions & 3 deletions

File tree

Lib/unittest/mock.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,8 @@ def __getattr__(self, name):
572572
raise AttributeError(name)
573573
if not self._mock_unsafe:
574574
if name.startswith(('assert', 'assret')):
575-
raise AttributeError(name)
575+
raise AttributeError("Attributes cannot start with 'assert' "
576+
"or 'assret'")
576577

577578
result = self._mock_children.get(name)
578579
if result is _deleted:

Lib/unittest/test/testmock/testmock.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,9 +1453,10 @@ def static_method(): pass
14531453
#Issue21238
14541454
def test_mock_unsafe(self):
14551455
m = Mock()
1456-
with self.assertRaises(AttributeError):
1456+
msg = "Attributes cannot start with 'assert' or 'assret'"
1457+
with self.assertRaisesRegex(AttributeError, msg):
14571458
m.assert_foo_call()
1458-
with self.assertRaises(AttributeError):
1459+
with self.assertRaisesRegex(AttributeError, msg):
14591460
m.assret_foo_call()
14601461
m = Mock(unsafe=True)
14611462
m.assert_foo_call()

0 commit comments

Comments
 (0)