Book Deep-Read · Software Engineering at Google · Chapter 13

Test Doubles

Software Engineering at Google · Ch 13 · Test Doubles · Andrew Trenk & Dillon Bly · 2020

中文 →

What is this chapter about?

You click "Pay" in an online store, and somewhere a piece of code asks the bank to charge your card. The engineers who need to check that code can't run a real card charge every time they check it. This chapter is about what to put in place of the things you can't easily bring into a check — and what goes wrong when you overdo it.

An analogy

Films use stunt doubles: the double does the dangerous jump so the star doesn't have to. Code is the same. Banks, remote servers and databases are slow, expensive and sometimes moody "stars", so during checks you bring in a stand-in. Stand-ins come in roughly three styles:

Why was the old world hard?

Without stand-ins, checks talk to real servers: hundreds or thousands of them take hours to run, and fail at random whenever the network hiccups. But once stand-ins became too easy to make, people overused them. Google learned this the hard way: when tools arrived that built a stand-in in one line, everyone treated them as a hammer for every nail. Years later they realised those checks were quick to write, tiring to maintain, and rarely caught real problems — nudge the code's insides and whole rows of them went red.

How it works, intuitively

What it buys you / how to choose

Pick in the order "real → realistic → scripted → watching moves" and your checks stay fast and steady, without going red every time the internals are rewritten. Google even labels some code "do not make a stand-in for this", pushing people to the real thing or an official realistic version. The honest cost: building a trustworthy realistic stand-in takes real effort, and it has to keep up with the star forever.

One line to remember

Stand-ins are a fallback: use the real thing first, a realistic copy second, and only last a stand-in that reads lines or just records how it was called.

Want the exact difference between fakes, stubs and interaction testing, what a "seam" is, how fast is fast enough for a real implementation, how fakes stay faithful through contract tests, and the only two cases where interaction testing pays off? → Switch to the deep read