Chapter 19. Test Isolation, and “Listening to Your Tests”
In the last chapter, we made the decision to leave a unit test failing in the views layer while we proceeded to write more tests and more code at the models layer to get it to pass.
We got away with it because our app was simple, but I should stress that, in a more complex application, this would be a dangerous decision. Proceeding to work on lower levels while you’re not sure that the higher levels are really finished or not is a risky strategy.
Note
I’m grateful to Gary Bernhardt, who took a look at an early draft of the previous chapter, and encouraged me to get into a longer discussion of test isolation.
Ensuring isolation between layers does involve more effort (and more of the dreaded mocks!), but it can also help to drive out improved design, as we’ll see in this chapter.
Revisiting Our Decision Point: The Views Layer Depends on Unwritten Models Code
Let’s revisit the point we were at half-way through the last chapter, when we
couldn’t get the new_list view to work because lists didn’t have the .owner
attribute yet.
We’ll actually go back in time and check out the old codebase, so that we can see how things would have worked if we’d used more isolated tests.
$ git checkout -b more-isolation # a branch for this experiment $ git reset --hard revisit_this_point_with_isolated_tests
Here’s what our failing tests looks like:
lists/tests/test_views.py.
classNewListTest(TestCase):[...]deftest_list_owner_is_saved_if_user_is_authenticated ...
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