python - How to test custom Django forms clean/save methods? -


most of time have change/extend default form save/clean methods. i'm not sure how test custom save/clean methods.

most of time tests like:

response = self.client.post(reverse('example:view_name'), kwargs={'example, self.example'}) self.assertequal(200, response.status_code) self.asserttemplateused('example.html', response) 

using self.client.post django's testcase class, capture response isn't enough , doesn't cover/test custom save , clean.

what's practice of testing forms? in opinion did above wrong, since it's more of integration test goes through view form.

create form directly in tests, , call is_valid method (clean called is_valid); check whether validate correctly. same save method.

for example:

from django.contrib.auth.forms import (usercreationform, ...)  ...  class usercreationformtest(testcase):      def test_user_already_exists(self):         data = {             'username': 'testclient',             'password1': 'test123',             'password2': 'test123',         }         form = usercreationform(data)         self.assertfalse(form.is_valid())         self.assertequal(             form["username"].errors,             [force_text(user._meta.get_field('username').error_messages['unique'])]) 

(above code came django source code - django/contrib/auth/tests/test_forms.py).


btw, parameters asserttemplateused response, template_name, ..., not template_name, response, .....

the last line in code in question should be:

self.asserttemplateused(response, 'example.html') 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -