Bird
0
0

Consider this Django test code: from django.test import TestCase class MyTests(TestCase): def test_user_count(self): from django.contrib.auth.models import User User.objects.create(username='user1') self.assertEqual(User.objects.count(), 1) What happens when this test runs multiple times?

medium📝 component behavior Q5 of 15
Django - Testing Django Applications
Consider this Django test code: from django.test import TestCase class MyTests(TestCase): def test_user_count(self): from django.contrib.auth.models import User User.objects.create(username='user1') self.assertEqual(User.objects.count(), 1) What happens when this test runs multiple times?
AEach test run starts with an empty database, so count is always 1
BUser count increases with each test run
CTest fails due to database errors
DTest is skipped because of database usage
Step-by-Step Solution
Solution:
  1. Step 1: Understand TestCase database behavior

    TestCase resets the database after each test, so no leftover data remains.
  2. Step 2: Analyze user creation and count

    Each test creates one user, so count is always 1 at test time.
  3. Final Answer:

    Each test run starts with an empty database, so count is always 1 -> Option A
  4. Quick Check:

    TestCase resets DB, count stays 1 [OK]
Quick Trick: TestCase resets DB each test, no data carryover [OK]
Common Mistakes:
MISTAKES
  • Assuming user count accumulates across tests
  • Thinking TestCase does not reset DB

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes