Unit Testing a Django Form with a FileField

Posted by Jason Christa on Stack Overflow See other posts from Stack Overflow or by Jason Christa
Published on 2010-03-18T21:16:31Z Indexed on 2010/03/18 21:21 UTC
Read the original article Hit count: 395

Filed under:
|
|

I have a form like:

#forms.py
from django import forms

class MyForm(forms.Form):
    title = forms.CharField()
    file = forms.FileField()


#tests.py
from django.test import TestCase
from forms import MyForm

class FormTestCase(TestCase)
    def test_form(self):
        upload_file = open('path/to/file', 'r')
        post_dict = {'title': 'Test Title'}
        file_dict = {} #??????
        form = MyForm(post_dict, file_dict)
        self.assertTrue(form.is_valid())

How do I construct the *file_dict* to pass *upload_file* to the form?

© Stack Overflow or respective owner

Related posts about django

Related posts about unit-testing