aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/unittest/test/testmock/testmock.py
diff options
context:
space:
mode:
authorRobert Collins <rbtcollins@hp.com>2015-07-24 03:48:20 +1200
committerRobert Collins <rbtcollins@hp.com>2015-07-24 03:48:20 +1200
commitca647ef60aa74e3dea81a399ded20c648a69e742 (patch)
tree041db4c6d33f51e0965162ae4521838c8cf469dc /Lib/unittest/test/testmock/testmock.py
parent80e4f30e64e2e5f8506f8342f6fca63eab20a4ab (diff)
downloadcpython-ca647ef60aa74e3dea81a399ded20c648a69e742.tar.gz
cpython-ca647ef60aa74e3dea81a399ded20c648a69e742.zip
Issue #21750: Further fixup to be styled like other mock APIs.
Diffstat (limited to 'Lib/unittest/test/testmock/testmock.py')
-rw-r--r--Lib/unittest/test/testmock/testmock.py24
1 files changed, 23 insertions, 1 deletions
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
index 32703e6d7bc..976c40fc456 100644
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -1,5 +1,6 @@
import copy
import sys
+import tempfile
import unittest
from unittest.test.testmock.support import is_instance
@@ -1329,8 +1330,29 @@ class MockTest(unittest.TestCase):
def test_mock_open_reuse_issue_21750(self):
mocked_open = mock.mock_open(read_data='data')
f1 = mocked_open('a-name')
+ f1_data = f1.read()
f2 = mocked_open('another-name')
- self.assertEqual(f1.read(), f2.read())
+ f2_data = f2.read()
+ self.assertEqual(f1_data, f2_data)
+
+ def test_mock_open_write(self):
+ # Test exception in file writing write()
+ mock_namedtemp = mock.mock_open(mock.MagicMock(name='JLV'))
+ with mock.patch('tempfile.NamedTemporaryFile', mock_namedtemp):
+ mock_filehandle = mock_namedtemp.return_value
+ mock_write = mock_filehandle.write
+ mock_write.side_effect = OSError('Test 2 Error')
+ def attempt():
+ tempfile.NamedTemporaryFile().write('asd')
+ self.assertRaises(OSError, attempt)
+
+ def test_mock_open_alter_readline(self):
+ mopen = mock.mock_open(read_data='foo\nbarn')
+ mopen.return_value.readline.side_effect = lambda *args:'abc'
+ first = mopen().readline()
+ second = mopen().readline()
+ self.assertEqual('abc', first)
+ self.assertEqual('abc', second)
def test_mock_parents(self):
for Klass in Mock, MagicMock: