0
0
Djangoframework~10 mins

Testing forms in Django - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Django test client.

Django
from django.test import [1]
Drag options to blanks, or click blank then click option'
ATestCase
BClient
CRequestFactory
DSimpleTestCase
Attempts:
3 left
💡 Hint
Common Mistakes
Importing TestCase instead of Client
Using RequestFactory which is for lower-level testing
Confusing SimpleTestCase with Client
2fill in blank
medium

Complete the code to create a test case class for form testing.

Django
class MyFormTest([1]):
    pass
Drag options to blanks, or click blank then click option'
AClient
BSimpleTestCase
CTestCase
DForm
Attempts:
3 left
💡 Hint
Common Mistakes
Using Client as a base class instead of TestCase
Using SimpleTestCase which does not support database
Using Form which is not a test class
3fill in blank
hard

Fix the error in the test method to check if the form is valid.

Django
def test_form_valid(self):
    form = MyForm(data={'name': 'Alice'})
    self.assertTrue(form.[1]())
Drag options to blanks, or click blank then click option'
Avalid
Bcheck_valid
Cvalidate
Dis_valid
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'valid' which is not a method
Using 'validate' which is not a form method
Using 'check_valid' which does not exist
4fill in blank
hard

Fill both blanks to create a form instance with POST data and check validity.

Django
form = [1](data=[2])
self.assertTrue(form.is_valid())
Drag options to blanks, or click blank then click option'
AMyForm
B{'username': 'bob'}
CUserForm
D{'email': 'bob@example.com'}
Attempts:
3 left
💡 Hint
Common Mistakes
Passing data without the 'data=' keyword
Using wrong form class name
Passing data as a list instead of a dictionary
5fill in blank
hard

Fill all three blanks to test form errors when required fields are missing.

Django
form = [1](data=[2])
self.assertFalse(form.is_valid())
self.assertIn('[3]', form.errors)
Drag options to blanks, or click blank then click option'
AMyForm
B{}
Cname
Demail
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-empty data which makes form valid
Checking for a field name not required by the form
Forgetting to check form.errors