0
0
Djangoframework~20 mins

Testing views with Client in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Django Client Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Django test client code?

Consider this Django test code snippet using Client to test a view that returns JSON data:

from django.test import Client
client = Client()
response = client.get('/api/data/')
print(response.status_code)
print(response.json())

The view at /api/data/ returns {"name": "Alice", "age": 30} with status 200.

What will be printed?

Django
from django.test import Client
client = Client()
response = client.get('/api/data/')
print(response.status_code)
print(response.json())
A
404
{'error': 'Not found'}
B
200
{'name': 'Alice', 'age': 30}
C
200
"{'name': 'Alice', 'age': 30}"
D
500
{'error': 'Server error'}
Attempts:
2 left
💡 Hint

Check the status code and the JSON content returned by the view.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses Django test Client to POST JSON data?

You want to send JSON data {"username": "bob", "password": "123"} to the /login/ endpoint using Django's test Client. Which code snippet is correct?

Aclient.post('/login/', data='{"username": "bob", "password": "123"}', content_type='application/json')
Bclient.post('/login/', data={'username': 'bob', 'password': '123'})
Cclient.post('/login/', json={'username': 'bob', 'password': '123'})
Dclient.post('/login/', data='username=bob&password=123', content_type='application/json')
Attempts:
2 left
💡 Hint

Remember to set the content type to application/json and send data as a JSON string.

🔧 Debug
advanced
2:00remaining
Why does this Django test client GET request raise an error?

Given this test code:

response = client.get('/profile/', data={'user': 'alice'})
print(response.status_code)

The view expects the username as a URL parameter, not a query string. The test fails with a 404 error. Why?

Django
response = client.get('/profile/', data={'user': 'alice'})
print(response.status_code)
AThe data dictionary keys must be strings, not variables.
BThe client.get method does not accept a data argument.
CThe server requires POST requests for this endpoint, not GET.
DThe view expects the username in the URL path, but the test sends it as a query parameter.
Attempts:
2 left
💡 Hint

Think about how URL parameters and query strings differ.

state_output
advanced
2:00remaining
What is the value of response.context['user'].username after this test client request?

Assume the view at /dashboard/ requires login and sets user in context. The test logs in a user 'jane' and requests the dashboard:

client.login(username='jane', password='pass123')
response = client.get('/dashboard/')
print(response.context['user'].username)

What will be printed?

Django
client.login(username='jane', password='pass123')
response = client.get('/dashboard/')
print(response.context['user'].username)
Ajane
BNone
CAnonymousUser
Dpass123
Attempts:
2 left
💡 Hint

Think about what client.login does and how context is set in views.

🧠 Conceptual
expert
2:00remaining
Which option best explains why Django test Client resets session between requests?

When using Django's test Client, each test method starts with a fresh session. Why is this behavior important?

ATo speed up tests by avoiding session data loading.
BBecause Django does not support sessions in tests.
CTo ensure tests are independent and do not share state, preventing flaky tests.
DBecause the client automatically logs out users after each request.
Attempts:
2 left
💡 Hint

Think about test reliability and isolation.