Django Django Testing & Debugging 2 — Questions and Answers
Question 1: When using `TestCase.setUpTestData()`, when is the data created relative to the test methods?
- Before every individual test method
- Once per test class, shared across all tests (Correct answer)
- Once per test module
- After every individual test method
Correct answer: Once per test class, shared across all tests
setUpTestData() creates database objects once at the class level, improving performance by avoiding repeated setup for every test method.
Question 2: Which tool is used in Django to mock database queries and avoid hitting the real database in unit tests?
- django.test.mock
- unittest.mock.patch (Correct answer)
- django.db.test.Mock
- pytest.monkeypatch only
Correct answer: unittest.mock.patch
Python's unittest.mock.patch is the standard way to mock functions, including Django ORM calls, in unit tests.
Question 3: What does Django's `--keepdb` flag do when running tests?
- Keeps test data after each test runs
- Reuses the existing test database instead of recreating it (Correct answer)
- Prevents rollback after each test
- Saves the test database to a file
Correct answer: Reuses the existing test database instead of recreating it
--keepdb skips the database destroy/create step between test runs, significantly speeding up subsequent test runs.
Question 4: What is the Django `DEBUG` setting typically set to in the test environment?
- True, so error details are visible
- False, to match production behavior (Correct answer)
- None, to disable error pages
- Depends on the DJANGO_ENV variable
Correct answer: False, to match production behavior
Django's test runner sets DEBUG=False by default so tests run in an environment closer to production, revealing issues hidden by debug mode.
Question 5: Which Django class allows you to write tests that start a live HTTP server for browser-based testing?
- django.test.TestCase
- django.test.SimpleTestCase
- django.test.LiveServerTestCase (Correct answer)
- django.test.TransactionTestCase
Correct answer: django.test.LiveServerTestCase
LiveServerTestCase starts a real HTTP server in a separate thread so external tools like Selenium can make real HTTP requests during tests.
Question 6: In Django's test client, how do you simulate a logged-in user without going through the login flow?
- self.client.login(user=user)
- self.client.force_login(user) (Correct answer)
- self.client.set_user(user)
- self.client.authenticate(user)
Correct answer: self.client.force_login(user)
force_login() bypasses authentication backends and directly sets the session to an authenticated state for the given user.
Question 7: What is the Django `django-debug-toolbar` primarily used for?
- Running automated tests
- Profiling SQL queries and request/response details during development (Correct answer)
- Monitoring production server health
- Linting Django code for errors
Correct answer: Profiling SQL queries and request/response details during development
django-debug-toolbar is a development tool that adds a sidebar panel showing SQL queries, cache hits, template rendering times, and other request details.
When using `TestCase.setUpTestData()`, when is the data created relative to the test methods?