Chapter 21. Using Mocks for Test Isolation
In this chapter, we’ll finish up our login system. While doing so, we’ll explore an alternative use of mocks: to isolate parts of the system from each other. This enables more targeted testing, fights combinatorial explosion, and reduces duplication between tests.
Note
In this chapter, we start to drift towards what’s called “London-school TDD”, which is a variant on the “Classical” or “Detroit” style of TDD that I mostly show in the book. We won’t get into the details here, but London-school TDD places more emphasis on mocking and isolating parts of the system. As always, there are pros and cons! Read more at Online Appendix: Test Isolation and “Listening to Your Tests”.
Along the way, we’ll learn a few more useful features of unittest.mock,
and we’ll also have a discussion about how many tests are “enough”.
Using Our Auth Backend in the Login View
We got our auth backend ready in the last chapter; now we need use the backend in our login view. But first, as our scratchpad says, we need to add it to settings.py:
src/superlists/settings.py (ch21l001)
AUTH_USER_MODEL ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access