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.