diff options
author | Robert Collins <rbtcollins@hp.com> | 2015-07-17 08:10:23 (GMT) |
---|---|---|
committer | Robert Collins <rbtcollins@hp.com> | 2015-07-17 08:10:23 (GMT) |
commit | 4838717b53eaaa686f301458d298751893e58055 (patch) | |
tree | c5b8fbaecbdd6a7c299ed4002e1134228ebf5b04 /Lib/unittest/test | |
parent | 2237bdc595e3d33f941c40108eddfa262c19d960 (diff) | |
parent | 5329aaa74bfe48a3be4fcd15d070bacfde682267 (diff) | |
download | cpython-4838717b53eaaa686f301458d298751893e58055.zip cpython-4838717b53eaaa686f301458d298751893e58055.tar.gz cpython-4838717b53eaaa686f301458d298751893e58055.tar.bz2 |
Issue #21750: mock_open.read_data can now be read from each instance, as it
could in Python 3.3.
Diffstat (limited to 'Lib/unittest/test')
-rw-r--r-- | Lib/unittest/test/testmock/testmock.py | 5 | ||||
-rw-r--r-- | Lib/unittest/test/testmock/testwith.py | 18 |
2 files changed, 21 insertions, 2 deletions
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index f4fb228..683c767 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -1371,6 +1371,11 @@ class MockTest(unittest.TestCase): self.assertEqual(m.mock_calls, [call.__int__(), call.__float__()]) self.assertEqual(m.method_calls, []) + def test_mock_open_reuse_issue_21750(self): + mocked_open = mock.mock_open(read_data='data') + f1 = mocked_open('a-name') + f2 = mocked_open('another-name') + self.assertEqual(f1.read(), f2.read()) def test_mock_parents(self): for Klass in Mock, MagicMock: diff --git a/Lib/unittest/test/testmock/testwith.py b/Lib/unittest/test/testmock/testwith.py index f54e051..ddcfe77 100644 --- a/Lib/unittest/test/testmock/testwith.py +++ b/Lib/unittest/test/testmock/testwith.py @@ -141,7 +141,6 @@ class TestMockOpen(unittest.TestCase): def test_mock_open_context_manager(self): mock = mock_open() - handle = mock.return_value with patch('%s.open' % __name__, mock, create=True): with open('foo') as f: f.read() @@ -149,8 +148,23 @@ class TestMockOpen(unittest.TestCase): expected_calls = [call('foo'), call().__enter__(), call().read(), call().__exit__(None, None, None)] self.assertEqual(mock.mock_calls, expected_calls) - self.assertIs(f, handle) + # mock_open.return_value is no longer static, because + # readline support requires that it mutate state + def test_mock_open_context_manager_multiple_times(self): + mock = mock_open() + with patch('%s.open' % __name__, mock, create=True): + with open('foo') as f: + f.read() + with open('bar') as f: + f.read() + + expected_calls = [ + call('foo'), call().__enter__(), call().read(), + call().__exit__(None, None, None), + call('bar'), call().__enter__(), call().read(), + call().__exit__(None, None, None)] + self.assertEqual(mock.mock_calls, expected_calls) def test_explicit_mock(self): mock = MagicMock() |