TestCase and SimpleTestCase help you check if your Django app works correctly by running small tests automatically.
When you want to test your Django models and database interactions.
When you need to test views or templates that require database access.
When you want to test simple functions or views that do not use the database.
When you want to quickly check if your code changes break anything.
When you want to automate testing to save time and avoid manual errors.
Syntax
Django
from django.test import TestCase, SimpleTestCase
class MyTest(TestCase):
def test_something(self):
self.assertEqual(1 + 1, 2)
class MySimpleTest(SimpleTestCase):
def test_simple(self):
self.assertTrue(True)
TestCase is used when your test needs database access.
SimpleTestCase is faster and used when no database is needed.
Examples
This test checks if adding 2 and 3 equals 5 using TestCase.
Django
from django.test import TestCase
class MathTest(TestCase):
def test_addition(self):
self.assertEqual(2 + 3, 5)
This test checks a simple true condition without database using SimpleTestCase.
Django
from django.test import SimpleTestCase
class BasicTest(SimpleTestCase):
def test_truth(self):
self.assertTrue(3 > 1)
This test creates a database object and checks its name using TestCase.
Django
from django.test import TestCase
from myapp.models import Item
class ItemTest(TestCase):
def test_create_item(self):
item = Item.objects.create(name='Book')
self.assertEqual(item.name, 'Book')
Sample Program
This example shows two tests: one simple math test without database and one test creating a user in the database.
Django
from django.test import TestCase, SimpleTestCase
class SimpleMathTest(SimpleTestCase):
def test_multiply(self):
self.assertEqual(3 * 4, 12)
class ModelTest(TestCase):
def test_database(self):
from django.contrib.auth.models import User
user = User.objects.create_user(username='testuser')
self.assertEqual(user.username, 'testuser')
OutputSuccess
Important Notes
Use TestCase when your test needs to read or write to the database.
Use SimpleTestCase for faster tests that do not touch the database.
Tests help catch bugs early and keep your app working well as you add features.
Summary
TestCase is for tests that use the database.
SimpleTestCase is for tests without database needs.
Both help you write automated tests to check your Django app works correctly.
Practice
(1/5)
1. Which Django test class should you use when your test needs to access the database?
easy
A. StaticLiveServerTestCase
B. TestCase
C. SimpleTestCase
D. TransactionTestCase
Solution
Step 1: Understand the purpose of TestCase
TestCase is designed for tests that require database access and setup.
Step 2: Compare with SimpleTestCase
SimpleTestCase is used when no database interaction is needed.
Final Answer:
TestCase -> Option B
Quick Check:
Database tests = TestCase [OK]
Hint: Use TestCase if your test touches the database [OK]
Common Mistakes:
Using SimpleTestCase for database tests
Confusing TransactionTestCase with TestCase
Assuming SimpleTestCase can access the database
2. Which of the following is the correct way to import SimpleTestCase in a Django test file?
easy
A. from django.test import SimpleTestCase
B. import SimpleTestCase from django.test
C. from django.test.simple import SimpleTestCase
D. from django.test import simpletestcase
Solution
Step 1: Recall Django test imports
Django provides SimpleTestCase in the django.test module.
Step 2: Check syntax correctness
Correct Python import syntax is from module import ClassName.
Final Answer:
from django.test import SimpleTestCase -> Option A
Quick Check:
Correct import syntax = from django.test import SimpleTestCase [OK]
Hint: Use 'from django.test import SimpleTestCase' to import [OK]
Common Mistakes:
Using wrong import syntax
Trying to import from non-existent submodules
Incorrect capitalization in import statements
3. What will be the output when running this test code?
from django.test import SimpleTestCase
class MyTests(SimpleTestCase):
def test_addition(self):
self.assertEqual(2 + 3, 5)
def test_database(self):
from myapp.models import Item
self.assertEqual(Item.objects.count(), 0)
medium
A. Both tests pass successfully
B. Both tests fail
C. test_addition passes, test_database raises an error
D. test_addition fails, test_database passes
Solution
Step 1: Analyze SimpleTestCase behavior
SimpleTestCase does not set up the database, so database queries will fail.
Step 2: Check each test method
test_addition is a simple math check and will pass. test_database tries to query the database, causing an error.
Final Answer:
test_addition passes, test_database raises an error -> Option C
Quick Check:
SimpleTestCase blocks DB access = test_addition passes, test_database raises an error [OK]
Hint: SimpleTestCase blocks DB; DB queries cause errors [OK]
Common Mistakes:
Assuming SimpleTestCase allows database queries
Expecting all tests to pass
Ignoring import errors from models
4. You wrote a test class inheriting from TestCase but your tests fail with errors about database access. What is a likely cause?
medium
A. You forgot to run migrations before testing
B. You used SimpleTestCase instead of TestCase
C. You did not import TestCase correctly
D. Your test methods are missing the self parameter
Solution
Step 1: Understand database setup in tests
TestCase requires the test database to be migrated before running tests.
Step 2: Identify common causes of DB errors
Failing to run migrations causes database errors even if TestCase is used.
Final Answer:
You forgot to run migrations before testing -> Option A
Quick Check:
DB errors often mean missing migrations [OK]
Hint: Run migrations before tests using TestCase [OK]
Common Mistakes:
Confusing SimpleTestCase with TestCase
Ignoring migration commands
Missing self in test method signatures
5. You want to write tests that check both simple logic and database queries in your Django app. How should you organize your test classes?
hard
A. Use TestCase for logic tests and SimpleTestCase for database tests
B. Use only SimpleTestCase for all tests
C. Use only TestCase for all tests
D. Use SimpleTestCase for logic tests and TestCase for database tests
Solution
Step 1: Separate tests by database need
Logic-only tests do not need database setup, so SimpleTestCase is faster and sufficient.
Step 2: Use TestCase for database tests
Tests that query or modify the database require TestCase to set up the test database.
Final Answer:
Use SimpleTestCase for logic tests and TestCase for database tests -> Option D
Quick Check:
Split tests by DB need: SimpleTestCase vs TestCase [OK]
Hint: Use SimpleTestCase for logic, TestCase for DB tests [OK]
Common Mistakes:
Using TestCase for all tests unnecessarily
Trying to run DB tests with SimpleTestCase
Mixing test types in one class
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.