New submission from fumihiko kakuma: It seems that stopall doesn't work when do start patch.dict to sys.modules. I show sample scripts the following.
Using stopall test case seems to always refer the first mock foo object. But using stop it refers a new mock foo object. $ cat test_sampmod.py import foo def myfunc(): print "myfunc foo=%s" % foo return foo $ cat test_samp.py import mock import sys import unittest class SampTestCase(unittest.TestCase): def setUp(self): self.foo_mod = mock.Mock() self.m = mock.patch.dict('sys.modules', {'foo': self.foo_mod}) self.p = self.m.start() print "foo_mod=%s" % self.foo_mod __import__('test_sampmod') self.test_sampmod = sys.modules['test_sampmod'] def tearDown(self): if len(sys.argv) > 1: self.m.stop() print ">>> stop patch" else: mock.patch.stopall() print ">>> stopall patch" def test_samp1(self): self.assertEqual(self.foo_mod, self.test_sampmod.myfunc()) def test_samp2(self): self.assertEqual(self.foo_mod, self.test_sampmod.myfunc()) def test_samp3(self): self.assertEqual(self.foo_mod, self.test_sampmod.myfunc()) if __name__ == '__main__': suite = unittest.TestSuite() suite.addTest(unittest.TestLoader().loadTestsFromTestCase(SampTestCase)) unittest.TextTestRunner(verbosity=2).run(suite) $ python test_samp.py stop test_samp1 (__main__.SampTestCase) ... foo_mod=<Mock id='41504336'> myfunc foo=<Mock id='41504336'> >>> stop patch ok test_samp2 (__main__.SampTestCase) ... foo_mod=<Mock id='41504464'> myfunc foo=<Mock id='41504464'> >>> stop patch ok test_samp3 (__main__.SampTestCase) ... foo_mod=<Mock id='41504720'> myfunc foo=<Mock id='41504720'> >>> stop patch ok ---------------------------------------------------------------------- Ran 3 tests in 0.004s OK $ python test_samp.py test_samp1 (__main__.SampTestCase) ... foo_mod=<Mock id='19152464'> myfunc foo=<Mock id='19152464'> >>> stopall patch ok test_samp2 (__main__.SampTestCase) ... foo_mod=<Mock id='19152592'> myfunc foo=<Mock id='19152464'> FAIL >>> stopall patch test_samp3 (__main__.SampTestCase) ... foo_mod=<Mock id='19182096'> myfunc foo=<Mock id='19152464'> FAIL >>> stopall patch ====================================================================== FAIL: test_samp2 (__main__.SampTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "test_samp.py", line 27, in test_samp2 self.assertEqual(self.foo_mod, self.test_sampmod.myfunc()) AssertionError: <Mock id='19152592'> != <Mock id='19152464'> ====================================================================== FAIL: test_samp3 (__main__.SampTestCase) ---------------------------------------------------------------------- Traceback (most recent call last): File "test_samp.py", line 30, in test_samp3 self.assertEqual(self.foo_mod, self.test_sampmod.myfunc()) AssertionError: <Mock id='19182096'> != <Mock id='19152464'> ---------------------------------------------------------------------- Ran 3 tests in 0.003s FAILED (failures=2) $ ---------- components: Tests messages: 219331 nosy: kakuma priority: normal severity: normal status: open title: mock.patch.stopall doesn't work with patch.dict to sys.modules type: behavior versions: Python 2.7 _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue21600> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com