samedi 25 avril 2020

how to mock reader/writing a file

I have a function which looks like this

def my_fun(input_csv, output_log, pubusb_project):
  f_in = open(input_csv)
  rows = csv.reader(f_in)
  f_out = open(output_log, 'w')
  # rest of the code including publishing to pubsub topic
  f_in.close()
  f_out.close()

How can i test this? I was thinking to use python-mock but it looks like that python mock can be used if i pass in actual file object, not the file path.

Is there a way to test this code with python mock? Or do i have to create a temporary file to test it first?

Aucun commentaire:

Enregistrer un commentaire