Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Django test client.
Django
from django.test import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing TestCase instead of Client
Importing models by mistake
✗ Incorrect
The Client class lets you simulate user requests in tests.
2fill in blank
mediumComplete the code to define a test case class inheriting from Django's test case.
Django
class MyTest([1]): pass
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Client as base class
Using Model or View instead
✗ Incorrect
Test classes should inherit from TestCase to get testing features.
3fill in blank
hardFix the error in the test method name to make it run automatically.
Django
def [1](self): self.assertEqual(1 + 1, 2)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not starting method name with 'test_'
Using other prefixes
✗ Incorrect
Test methods must start with test_ to be discovered by Django.
4fill in blank
hardFill both blanks to create a test that checks the home page status code.
Django
def test_home_page(self): response = self.client.[1]('/') self.assertEqual(response.[2], 200)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using post instead of get
Checking content instead of status_code
✗ Incorrect
Use get to request the home page and check status_code for 200 OK.
5fill in blank
hardFill all three blanks to write a test that creates a user and checks its username.
Django
def test_create_user(self): user = User.objects.[1](username='alice') self.assertEqual(user.[2], 'alice') self.assertTrue(user.[3]())
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter instead of create
Checking wrong attributes
✗ Incorrect
Create a user with create, check the username, and verify the user is active.