Issue 37008: make unittest.mock.mock_open honor next() (original) (raw)

While we can iterate on a mock_open handle, it is not actually possible to perform a next() on it.

My use case is the following: I have a file with a one-line header that I want to skip. So, roughly, my code is like

with open(filename, 'r') as my_file:
    confirm_header(next(my_file))
    for line in my_file:
        do_something(line)

And while writing a unit test on this code, the handle returned by mock_open(read_data=my_read_data) returns a mock instead of the first line of my_read_data.