Django Django Testing & Debugging 1 — Questions and Answers
Question 1: Which class should a Django test case inherit from to get database access and automatic rollback between tests?
- unittest.TestCase
- django.test.TestCase (Correct answer)
- django.test.SimpleTestCase
- django.test.LiveServerTestCase
Correct answer: django.test.TestCase
django.test.TestCase wraps each test in a transaction that is rolled back after the test, providing database isolation.
Question 2: What does the Django test Client's `self.client.get('/path/')` method return?
- A requests.Response object
- A HttpResponse object (Correct answer)
- A TestResponse object
- A JSON dict
Correct answer: A HttpResponse object
The test Client's get() method returns an HttpResponse object whose status_code, content, and context can be inspected.
Question 3: Which Django management command runs the test suite?
- python manage.py runtest
- python manage.py test (Correct answer)
- python manage.py unittest
- python manage.py check
Correct answer: python manage.py test
python manage.py test discovers and runs all tests in the project using Django's test runner.
Question 4: What is the purpose of `django.test.SimpleTestCase`?
- Running tests with a live server
- Running tests that require multiple databases
- Running tests that do not require database access (Correct answer)
- Running tests with async support only
Correct answer: Running tests that do not require database access
SimpleTestCase is for tests that don't touch the database, making it faster since no database setup is needed.
Question 5: How do you set up test-specific settings in Django without modifying the main settings file?
- Use a conftest.py file
- Use the @override_settings decorator (Correct answer)
- Edit settings.py before each test
- Create a test_settings.env file
Correct answer: Use the @override_settings decorator
@override_settings temporarily changes Django settings for a single test or test class, restoring them afterwards.
Question 6: Which assertion method should you use to verify that a Django view returns a redirect response?
- self.assertEqual(response.status_code, 301)
- self.assertRedirects(response, '/expected-url/') (Correct answer)
- self.assertContains(response, 'redirect')
- self.assertTemplateUsed(response, 'redirect.html')
Correct answer: self.assertRedirects(response, '/expected-url/')
assertRedirects checks both the redirect status code and the final redirect target URL in one call.
Question 7: What does `self.assertTemplateUsed(response, 'home.html')` verify?
- That home.html exists on disk
- That the view rendered using the home.html template (Correct answer)
- That home.html has no syntax errors
- That home.html was cached by Django
Correct answer: That the view rendered using the home.html template
assertTemplateUsed checks that a specific template was used to render the response, useful for verifying view logic.
Which class should a Django test case inherit from to get database access and automatic rollback between tests?