with patch('__builtin__.open', mock_open(read_data='data')) as mock_file:
    assert open('path/to/open').read() == 'data'
mock_file.assert_called_with('path/to/open')
How do I mock an open used in a with statement (using the Mock framework in Python)?