0
0
Djangoframework~10 mins

Testing API endpoints 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'
ARequestFactory
BTestCase
CClient
DSimpleTestCase
Attempts:
3 left
💡 Hint
Common Mistakes
Importing TestCase instead of Client
Using RequestFactory which is different from Client
Forgetting to import anything
2fill in blank
medium

Complete the code to send a GET request to the '/api/items/' endpoint using the test client.

Django
client = Client()
response = client.[1]('/api/items/')
Drag options to blanks, or click blank then click option'
Apost
Bget
Cput
Ddelete
Attempts:
3 left
💡 Hint
Common Mistakes
Using post instead of get for retrieving data
Using put or delete which modify data
Calling a method that does not exist on Client
3fill in blank
hard

Fix the error in the assertion to check if the response status code is 200.

Django
self.assertEqual(response.status_code, [1])
Drag options to blanks, or click blank then click option'
A200
B404
C500
D201
Attempts:
3 left
💡 Hint
Common Mistakes
Using 201 which means created
Using 404 which means not found
Using 500 which means server error
4fill in blank
hard

Fill both blanks to send a POST request with JSON data to '/api/items/'.

Django
response = client.post('/api/items/', [1], content_type=[2])
Drag options to blanks, or click blank then click option'
A{'name': 'item1'}
B'application/json'
C'text/html'
D{'id': 1}
Attempts:
3 left
💡 Hint
Common Mistakes
Passing data as a string instead of a dictionary
Using wrong content type like 'text/html'
Not setting content_type at all
5fill in blank
hard

Fill all three blanks to check if the response JSON contains the expected 'name' field.

Django
data = response.json()
self.assertEqual(data.get([1]), [2])
self.assertTrue([3] in data)
Drag options to blanks, or click blank then click option'
A'name'
B'item1'
D'id'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong keys like 'id'
Checking for keys that don't exist
Mixing up keys and values