summaryrefslogtreecommitdiffstats
path: root/Lib/unittest
diff options
context:
space:
mode:
authorAnirudha Bose <ani07nov@gmail.com>2018-12-07 23:30:42 (GMT)
committerBrett Cannon <brettcannon@users.noreply.github.com>2018-12-07 23:30:42 (GMT)
commit3cf74384b53b998fa846dc2590cedf9ad2a0d5fd (patch)
tree3064357ada90643ab450860ec4cf1a558bd9bb2e /Lib/unittest
parent028f0ef4f3111d2b3fc5b971642e337ba7990873 (diff)
downloadcpython-3cf74384b53b998fa846dc2590cedf9ad2a0d5fd.zip
cpython-3cf74384b53b998fa846dc2590cedf9ad2a0d5fd.tar.gz
cpython-3cf74384b53b998fa846dc2590cedf9ad2a0d5fd.tar.bz2
bpo-33747: Avoid mutating the global sys.modules dict in unittest.mock tests (GH-8520)
Diffstat (limited to 'Lib/unittest')
-rw-r--r--Lib/unittest/test/testmock/testpatch.py27
1 files changed, 16 insertions, 11 deletions
diff --git a/Lib/unittest/test/testmock/testpatch.py b/Lib/unittest/test/testmock/testpatch.py
index fe4ecef..f052257 100644
--- a/Lib/unittest/test/testmock/testpatch.py
+++ b/Lib/unittest/test/testmock/testpatch.py
@@ -9,6 +9,7 @@ import unittest
from unittest.test.testmock import support
from unittest.test.testmock.support import SomeClass, is_instance
+from test.test_importlib.util import uncache
from unittest.mock import (
NonCallableMock, CallableMixin, sentinel,
MagicMock, Mock, NonCallableMagicMock, patch, _patch,
@@ -1660,20 +1661,19 @@ class PatchTest(unittest.TestCase):
def test_patch_imports_lazily(self):
- sys.modules.pop('squizz', None)
-
p1 = patch('squizz.squozz')
self.assertRaises(ImportError, p1.start)
- squizz = Mock()
- squizz.squozz = 6
- sys.modules['squizz'] = squizz
- p1 = patch('squizz.squozz')
- squizz.squozz = 3
- p1.start()
- p1.stop()
- self.assertEqual(squizz.squozz, 3)
+ with uncache('squizz'):
+ squizz = Mock()
+ sys.modules['squizz'] = squizz
+ squizz.squozz = 6
+ p1 = patch('squizz.squozz')
+ squizz.squozz = 3
+ p1.start()
+ p1.stop()
+ self.assertEqual(squizz.squozz, 3)
def test_patch_propogrates_exc_on_exit(self):
class holder:
@@ -1696,7 +1696,12 @@ class PatchTest(unittest.TestCase):
def test(mock):
raise RuntimeError
- self.assertRaises(RuntimeError, test)
+ with uncache('squizz'):
+ squizz = Mock()
+ sys.modules['squizz'] = squizz
+
+ self.assertRaises(RuntimeError, test)
+
self.assertIs(holder.exc_info[0], RuntimeError)
self.assertIsNotNone(holder.exc_info[1],
'exception value not propgated')