Challenge - 5 Problems
Django Test Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
Difference in database usage between TestCase and SimpleTestCase
What happens when you run a test method inside a Django
SimpleTestCase that tries to access the database?Django
from django.test import SimpleTestCase from myapp.models import MyModel class MySimpleTest(SimpleTestCase): def test_db_access(self): count = MyModel.objects.count() self.assertEqual(count, 0)
Attempts:
2 left
💡 Hint
Think about which test class sets up the test database.
✗ Incorrect
SimpleTestCase does not set up or use the test database. Any database access inside it will raise an error.
Only TestCase sets up a test database and allows database queries.
❓ state_output
intermediate2:00remaining
Effect of TestCase on database state between tests
Given this Django
TestCase class, what will be the count of MyModel objects in the second test method?Django
from django.test import TestCase from myapp.models import MyModel class MyTest(TestCase): def test_create(self): MyModel.objects.create(name='test') self.assertEqual(MyModel.objects.count(), 1) def test_count(self): count = MyModel.objects.count() print(count)
Attempts:
2 left
💡 Hint
Remember how TestCase resets the database between tests.
✗ Incorrect
Django's TestCase resets the test database after each test method, so the second test sees an empty database.
📝 Syntax
advanced2:00remaining
Correct way to write a Django TestCase with setup
Which option correctly defines a Django
TestCase with a setup method that creates a model instance before each test?Attempts:
2 left
💡 Hint
Check the exact method name Django expects for setup.
✗ Incorrect
Django expects the setup method to be named exactly setUp with camel case.
Other variations like setup, SetUp, or set_up are ignored.
🔧 Debug
advanced2:00remaining
Why does this SimpleTestCase test fail with AttributeError?
Consider this test code. Why does it raise
AttributeError: 'Client' object has no attribute 'login'?Django
from django.test import SimpleTestCase class MySimpleTest(SimpleTestCase): def test_login(self): logged_in = self.client.login(username='user', password='pass') self.assertTrue(logged_in)
Attempts:
2 left
💡 Hint
Think about what login requires internally.
✗ Incorrect
The login method requires database access to check user credentials.
SimpleTestCase does not set up the database, so login fails with AttributeError.
🧠 Conceptual
expert2:00remaining
Why choose SimpleTestCase over TestCase for some tests?
Which reason best explains why you might use
SimpleTestCase instead of TestCase in Django testing?Attempts:
2 left
💡 Hint
Consider the cost of database setup in tests.
✗ Incorrect
SimpleTestCase is faster because it does not set up or use the test database.
It is suitable for tests that do not require database access.