Search⌘ K
AI Features

Design with Tests

Explore how designing with tests helps clarify what your code should do before and during development. Learn to mock components, write meaningful unit tests, and use tests as specifications to improve software quality and maintainability.

Testing

Where one of our previous lessons, Insist on Correctness, focused on verifying that our code does what it’s supposed to do. In this lesson, we will focus on the meta-question, “What should this code do?”

On the surface, it might seem puzzling that a programmer would write code without knowing what it’s supposed to do ahead of time. Yet, we do it all the time. Faced with a problem, we charge off writing code and figure things out as we go. Programming is a creative act, not a mechanical one, and this process is akin to a painter charging off on a blank canvas without knowing exactly what the finished painting will look like.

Yet, programming also requires rigor. Testing gives us tools for both design and rigor at the same time.

Designing with tests

Due to the frameworks for test doubles discussed in Interactions, we can start with a large programming problem and start attacking it from whatever angle makes sense first. Perhaps our program needs to grab an XML ...