In this chapter I’d like to talk about a technique called Outside-In TDD. It’s pretty much what we’ve been doing all along. Our “double-loop” TDD process, in which we write the functional test first and then the unit tests, is already a manifestation of outside-in—we design the system from the outside, and build up our code in layers. Now I’ll make it explicit, and talk about some of the common issues involved.
The alternative to “outside-in” is to work “inside-out”, which is the way most people intuitively work before they encounter TDD. After coming up with a design, the natural inclination is sometimes to implement it starting with the innermost, lowest-level components first.
For example, when faced with our current problem, providing users with a
“My Lists” page of saved lists, the temptation is to start by adding an “owner”
attribute to the
List model object, reasoning that an attribute like this is
“obviously” going to be required. Once that’s in place, we would modify the
more peripheral layers of code, such as views and templates, taking advantage
of the new attribute, and then finally add URL routing to point to the new
It feels comfortable because it means you’re never working on a bit of code that is dependent on something that hasn’t yet been implemented. Each bit of work on the inside is a solid foundation on which to build the next layer out.
But working inside-out like this also ...