0
0
Djangoframework~20 mins

TestCase and SimpleTestCase in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Django Test Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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)
AThe test raises an error because SimpleTestCase does not set up the test database.
BThe test silently ignores the database call and passes.
CThe test runs successfully and counts the objects in the database.
DThe test runs but always returns zero regardless of database content.
Attempts:
2 left
💡 Hint
Think about which test class sets up the test database.
state_output
intermediate
2: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)
A2
B0
CRaises an error because the database is not available.
D1
Attempts:
2 left
💡 Hint
Remember how TestCase resets the database between tests.
📝 Syntax
advanced
2: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?
A
class MyTest(TestCase):
    def setUp(self):
        MyModel.objects.create(name='setup')

    def test_example(self):
        self.assertEqual(MyModel.objects.count(), 1)
B
class MyTest(TestCase):
    def setup(self):
        MyModel.objects.create(name='setup')

    def test_example(self):
        self.assertEqual(MyModel.objects.count(), 1)
C
class MyTest(TestCase):
    def SetUp(self):
        MyModel.objects.create(name='setup')

    def test_example(self):
        self.assertEqual(MyModel.objects.count(), 1)
D
class MyTest(TestCase):
    def set_up(self):
        MyModel.objects.create(name='setup')

    def test_example(self):
        self.assertEqual(MyModel.objects.count(), 1)
Attempts:
2 left
💡 Hint
Check the exact method name Django expects for setup.
🔧 Debug
advanced
2: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)
ASimpleTestCase does not provide a full test client with login support.
BThe test is missing a call to super().setUp() to initialize the client.
CThe client attribute is missing in SimpleTestCase.
DThe login method requires a database, which SimpleTestCase does not set up.
Attempts:
2 left
💡 Hint
Think about what login requires internally.
🧠 Conceptual
expert
2:00remaining
Why choose SimpleTestCase over TestCase for some tests?
Which reason best explains why you might use SimpleTestCase instead of TestCase in Django testing?
ASimpleTestCase automatically mocks all external APIs, making tests more isolated.
BSimpleTestCase supports testing with a real database, unlike TestCase which uses a mock database.
CSimpleTestCase runs faster because it skips database setup, ideal for tests that don't need the database.
DSimpleTestCase allows testing asynchronous views, which TestCase does not support.
Attempts:
2 left
💡 Hint
Consider the cost of database setup in tests.