[PYTHON] [Django] How to test Form [TDD]

How about testing file uploads ...

Because of CSRF, even if I send a file by POST, an error is returned! I thought, but I knew I should test the form. ~~ I really wanted to test the context to POST and come back, but I didn't know. There may be a way, so I will add it when I understand it. ~~

Since it has been resolved, it is summarized in this article.

Form test

Let's say you want to test the Form below.

forms.py


from django import forms

class FormTest(forms.Form):
    table_name=forms.CharField(max_length=30)
    file_data= forms.FileField()

The file upload test for this is written as below.

tests.py


from django.core.files import File
from ..forms import DocumentForm

class Test_Model_Create(TestCase):
    def setUp(self):
        self.file_data=File(open('./path/test.csv'))

    def test_step1_form(self):        
        data = {
            "table_name":"name_test"
        }
        files={                
            "file_data":self.file_data
        }
        form = DocumentForm(data,files)
        self.assertTrue(form.is_valid())

If you throw the correct data, ʻis_valid ()` will return True.

Clogged points and countermeasures

As I wrote at the beginning, I couldn't POST the file correctly, probably because of CSRF. After that, I changed it as above, but it didn't go through easily. In conclusion ** Custom validation was bad. ** ** Well, it worked fine rather than bad, but the data I wrote in the test didn't match it. To avoid being like yourself, please make sure that you show validation errors when testing your form.

tests.py


print(form.errors)

Now you can see the contents of the validation error. Please check if the error is as expected.

Recommended Posts

[Django] How to test Form [TDD]
[Django] Test standard LoginForm [TDD]
Django test
How to reflect CSS in Django
How to get started with Django
How to write Django1.9 environment-independent wsgi.py
How to authenticate with Django Part 2
How to authenticate with Django Part 3
[Django] I wanted to test when POSTing a large file [TDD]
How to check the version of Django
How to delete expired sessions in Django
How to use Google Test in C
How to study deep learning G test
How to do portmanteau test with python
How to test on a Django-authenticated page
How to do Server-Sent Events in Django
How to convert DateTimeField format in Django
Django Contact Form 2
Django Suggested Form
Django Form Gleaning
Django contact form
[Python] How to test command line parser click
Dynamically add fields to Form objects in Django
How to implement Rails helper-like functionality in Django
[Django] How to resolve errors when installing mysqlclient
How to set Django DB to mongodb visual studio 2019
How to develop a cart app with Django
How to reflect ImageField in Django + Docker (pillow)
How to run some script regularly in Django
[Django] How to get data by specifying SQL.
How to print debug messages to the Django console
How to implement "named_scope" of RubyOnRails with Django
How to create a Rest Api in Django
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
[Python] [Django] How to use ChoiceField and how to add options
Scraping 2 How to scrape
How to use Seaboan
How to use image-match
How to use shogun
How to install Python
How to use Pandas 2
How to read PyPI
How to install pip
How to use Virtualenv
How to use numpy.vectorize
How to update easy_install
How to install archlinux
How to deploy a Django application on Alibaba Cloud
How to use pytest_report_header
How to restart gunicorn
How to install python
How to virtual host
How to debug selenium
How to use Azure Table storage from Django (PTVS)
How to use partial
How to install Google Test / Google Mock in Visual Studio 2019
How to use Bio.Phylo
How to read JSON
How to use SymPy