[Answered ]-How to test uploadfile in django

1👍

Your self.client.post(…) will already exhaust the file handler and read the entire content of the file, this thus means that when you call file.read(), the cursor already moved to the end of the file, and thus returns an empty string.

You should reopen the file and read the file from the beginning, so:

def test_if_can_upload_file(self):
    with open('app_blog/tests/test.txt') as file:
        self.client.post(reverse('csv_blog'), {'attachment': file})
    with open('app_blog/tests/test.txt') as file:
        test_file = file.read()
        self.assertEqual(test_file, 'test file')

Leave a comment