I'm trying to mock read from file. Using examples it can be done with a construction like:
with patch('__builtin__.open', mock_open(read_data='1'), create=True) as m:
with open('foo') as h:
result = h.read()
I wonder, is there a way to mock open function using my testcase annotation. Like:
@patch.object(__builtin__, 'open')
def test_check_status_running(self, m_open):
I don't find the correct way, because for me it works for int and doesn't work for strings:
@patch.object(__builtin__, 'open')
def test_check_status_running1(self, m_open):
m_open = mock_open(read_data='1')
pid = open('testfile').read().strip()
print type(pid) # <class 'mock.MagicMock'>
self.assertEqual(1, int(pid)) # Pass
self.assertEqual('1', pid) # Fails MismatchError: '1' != <MagicMock name='open().read()' id='39774928'>