diff options
| author | vabr-g <vabr@google.com> | 2020-11-05 18:04:38 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-05 09:04:38 -0800 |
| commit | 4662fa9bfe4a849fe87bfb321d8ef0956c89a772 (patch) | |
| tree | 82067c4fbc0e0d76413b2fc8de1f73c777c82efa /Lib/unittest/test/testmock/testmock.py | |
| parent | 133aa2d5816b69d8ee755e1a9d2d1977b9205736 (diff) | |
| download | cpython-git-4662fa9bfe4a849fe87bfb321d8ef0956c89a772.tar.gz | |
bpo-41877 Check for asert, aseert, assrt in mocks (GH-23165)
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.
Diffstat (limited to 'Lib/unittest/test/testmock/testmock.py')
| -rw-r--r-- | Lib/unittest/test/testmock/testmock.py | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index ce674e713e..194ce3f61b 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -1598,14 +1598,23 @@ class MockTest(unittest.TestCase): #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): |
