[Tutor] mock file reader object

Anshu Kumar anshu.kumar726 at gmail.com
Thu Feb 11 08:01:03 EST 2016


I need to mock file reader object , i tried using @patch('__builtin__.open')
but it will patch my all file open readers.

More precisely i have code something like below

def restore_segments_and_lines_json(segments_file_path,lines_file_path,deleted_segment_ids):
    with open(segments_file_path, "rb") as segments_reader:
        segments = segments_reader.read()

    with open(lines_file_path, "rb") as lines_reader:
        lines = lines_readers.read()

    """ Do some processing with lines and segments"""

    logger.info("updating segments file")
    with open(segments_file_path, "wb") as segments_writer:
        segments_writer.write(segments, segments_writer)

    logger.info("updating lines file")
    with open(lines_file_path, "wb") as lines_writer:
        lines_writer.write(lines, lines_writer)

I need to mock two different file read and file write opens, can i get
some suggestion.

Thanks in advance,


More information about the Tutor mailing list