Skip to content

Commit

Permalink
bpo-41877 Check for asert, aseert, assrt in mocks (GH-23165)
Browse files Browse the repository at this point in the history
Currently, a Mock object which is not unsafe will raise an
AttributeError if an attribute with the prefix assert or assret is
accessed on it. This protects against misspellings of real assert
method calls, which lead to tests passing silently even if the tested
code does not satisfy the intended assertion.

Recently a check was done in a large code base (Google) and three
more frequent ways of misspelling assert were found causing harm:
asert, aseert, assrt. These are now added to the existing check.
  • Loading branch information
vabr-g committed Nov 5, 2020
1 parent 133aa2d commit 4662fa9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Lib/unittest/mock.py
Expand Up @@ -631,9 +631,9 @@ def __getattr__(self, name):
elif _is_magic(name):
raise AttributeError(name)
if not self._mock_unsafe:
if name.startswith(('assert', 'assret')):
if name.startswith(('assert', 'assret', 'asert', 'aseert', 'assrt')):
raise AttributeError("Attributes cannot start with 'assert' "
"or 'assret'")
"or its misspellings")

result = self._mock_children.get(name)
if result is _deleted:
Expand Down
11 changes: 10 additions & 1 deletion Lib/unittest/test/testmock/testmock.py
Expand Up @@ -1598,14 +1598,23 @@ def static_method(): pass
#Issue21238
def test_mock_unsafe(self):
m = Mock()
msg = "Attributes cannot start with 'assert' or 'assret'"
msg = "Attributes cannot start with 'assert' or its misspellings"
with self.assertRaisesRegex(AttributeError, msg):
m.assert_foo_call()
with self.assertRaisesRegex(AttributeError, msg):
m.assret_foo_call()
with self.assertRaisesRegex(AttributeError, msg):
m.asert_foo_call()
with self.assertRaisesRegex(AttributeError, msg):
m.aseert_foo_call()
with self.assertRaisesRegex(AttributeError, msg):
m.assrt_foo_call()
m = Mock(unsafe=True)
m.assert_foo_call()
m.assret_foo_call()
m.asert_foo_call()
m.aseert_foo_call()
m.assrt_foo_call()

#Issue21262
def test_assert_not_called(self):
Expand Down
@@ -0,0 +1,2 @@
Mock objects which are not unsafe will now raise an AttributeError if an attribute with the prefix asert, aseert,
or assrt is accessed, in addition to this already happening for the prefixes assert or assret.

0 comments on commit 4662fa9

Please sign in to comment.