Bird
0
0

You wrote this test but it raises an error: from django.test import SimpleTestCase class MyTests(SimpleTestCase): def test_db_access(self): from django.contrib.auth.models import User User.objects.create(username='user1') What is the cause of the error?

medium📝 Debug Q6 of 15
Django - Testing Django Applications
You wrote this test but it raises an error: from django.test import SimpleTestCase class MyTests(SimpleTestCase): def test_db_access(self): from django.contrib.auth.models import User User.objects.create(username='user1') What is the cause of the error?
AUser model is not imported correctly
BSimpleTestCase does not support database access
CTest method name is incorrect
DMissing call to super().__init__() in test class
Step-by-Step Solution
Solution:
  1. Step 1: Identify database usage in test

    The test tries to create a User, which requires database access.
  2. Step 2: Recall SimpleTestCase limitations

    SimpleTestCase does not set up database, so DB operations cause errors.
  3. Final Answer:

    SimpleTestCase does not support database access -> Option B
  4. Quick Check:

    DB access fails on SimpleTestCase [OK]
Quick Trick: Use TestCase for DB tests, not SimpleTestCase [OK]
Common Mistakes:
MISTAKES
  • Assuming SimpleTestCase supports DB
  • Blaming import or method name

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes