3👍
I have done this slightly differently in the past, using a file generated in the test case, without touching the filesystem:
fake_file = ContentFile(b"Some file content")
fake_file.name = 'myfile.xml'
post_data = {
'title': "Test document",
'file': fake_file,
}
url = '/add/question/'+ str(self.subj1.id) +'/' + str(self.topc1.id) + '/'
response = self.client.post(url, post_data)
Failing that, it’s important that you submit the form with content_type='multipart/form-data'
. If you use self.client rather than Client then this is done automatically if you include a data
argument. See https://docs.djangoproject.com/en/1.10/topics/testing/tools/#django.test.Client.post
Source:stackexchange.com