How to test a function that loads a pickle file without actual IO operations

Viewed 33

I have written a function that loads a pickled list of dictionaries and optionally filters the result:

def load_pickled_list(path_to_file, filter_key=None):
    with open(path_to_file, "rb") as file:
        loaded_list = pickle.load(file)

    if filter_key is not None:
        loaded_list = [entry for entry in loaded_list if loaded_list[filter_key] == filter_key]

    return loaded_list

How do I test this with pytest by providing two different lists of dictionaries in code? Especially, how do I implement a test double of pickle? I do not want to provide a file such as test_list.pkl so that the test would have to perform real disk IO operations.

1 Answers

If you have a sequence of bytes in mind, you can hard-code it directly into your program using io.BytesIO. The example in the module docs may be what you need, or at least provide a good starting point:

f = io.BytesIO(b"your pickle file here ")
loaded_list = pickle.load(f)

Better than hard-coding the data, have some part of your setup or a fixture generate it:

# make some objects
data = ...
f = io.BytesIO()
pickle.dump(data, f)
f.seek(0)

# now load `f`

You could also avoid the file interface entirely by using dumps/loads to work with bytes directly instead of doing I/O with dump/load.

Related