[Solved] File upload testing using unit test python [closed]


Start by rewriting the method to take an iterable as an argument:

def scene_upload(self, scene):        
    csv_reader = csv.reader(scene, delimiter=",", quotechar="|")
    next(csv_reader)  # Skip the header
    for line_count, row in enumerate(csv_reader, 1):
        self.time_stamp.append(int(row[0]))
        self.active_func.append(int(row[1]))
        self.active_func_output.append(row[2])
        self.dstream_func.append(int(row[3]))
        self.dstream_func_aspect.append(row[4])
        self.time_tolerance.append(row[5])

In production use, you make the caller responsible for opening the file:

filename_scene = filedialog.askopenfilename(initialdir="https://stackoverflow.com/", title="Select file")
print(filename_scene)
with open(filename_scene, newline="") as csv_file:
    x.scene_upload(csv_file)

In tests, though, you can pass a simple list of strings as your test data.

def test_upload(self):
    test_data = ["header", "1,2,foo,4,bar,baz"]
    x = MyClass()
    x.scene_upload(test_data)
    self.assertEqual(x.time_stamp, [1])
    self.assertEqual(x.active_func, [2])
    # etc

solved File upload testing using unit test python [closed]