summaryrefslogtreecommitdiff
path: root/Lib/unittest/test/testmock/testmock.py
diff options
context:
space:
mode:
authorPablo Galindo <Pablogsal@gmail.com>2019-01-21 08:57:46 +0000
committerChris Withers <chris@withers.org>2019-01-21 08:57:46 +0000
commit222d303ade8aadf0adcae5190fac603bdcafe3f0 (patch)
treebd1a8b9366321becda7c6cfd1e46f51803de9f2d /Lib/unittest/test/testmock/testmock.py
parente8239b8e8199b76ef647ff3bf080ce2eb7733e04 (diff)
downloadcpython-git-222d303ade8aadf0adcae5190fac603bdcafe3f0.tar.gz
bpo-20239: Allow repeated deletion of unittest.mock.Mock attributes (#11057)
* Allow repeated deletion of unittest.mock.Mock attributes * fixup! Allow repeated deletion of unittest.mock.Mock attributes * fixup! fixup! Allow repeated deletion of unittest.mock.Mock attributes
Diffstat (limited to 'Lib/unittest/test/testmock/testmock.py')
-rw-r--r--Lib/unittest/test/testmock/testmock.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
index 193ae9f9ac..64e2fcf61c 100644
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -1769,6 +1769,33 @@ class MockTest(unittest.TestCase):
self.assertRaises(AttributeError, getattr, mock, 'f')
+ def test_mock_does_not_raise_on_repeated_attribute_deletion(self):
+ # bpo-20239: Assigning and deleting twice an attribute raises.
+ for mock in (Mock(), MagicMock(), NonCallableMagicMock(),
+ NonCallableMock()):
+ mock.foo = 3
+ self.assertTrue(hasattr(mock, 'foo'))
+ self.assertEqual(mock.foo, 3)
+
+ del mock.foo
+ self.assertFalse(hasattr(mock, 'foo'))
+
+ mock.foo = 4
+ self.assertTrue(hasattr(mock, 'foo'))
+ self.assertEqual(mock.foo, 4)
+
+ del mock.foo
+ self.assertFalse(hasattr(mock, 'foo'))
+
+
+ def test_mock_raises_when_deleting_nonexistent_attribute(self):
+ for mock in (Mock(), MagicMock(), NonCallableMagicMock(),
+ NonCallableMock()):
+ del mock.foo
+ with self.assertRaises(AttributeError):
+ del mock.foo
+
+
def test_reset_mock_does_not_raise_on_attr_deletion(self):
# bpo-31177: reset_mock should not raise AttributeError when attributes
# were deleted in a mock instance