Skip to Content
Software Engineering at Google
book

Software Engineering at Google

by Titus Winters, Tom Manshreck, Hyrum Wright
March 2020
Intermediate to advanced
602 pages
18h 36m
English
O'Reilly Media, Inc.
Audiobook available
Content preview from Software Engineering at Google

Chapter 13. Test Doubles

Unit tests are a critical tool for keeping developers productive and reducing defects in code. Although they can be easy to write for simple code, writing them becomes difficult as code becomes more complex.

For example, imagine trying to write a test for a function that sends a request to an external server and then stores the response in a database. Writing a handful of tests might be doable with some effort. But if you need to write hundreds or thousands of tests like this, your test suite will likely take hours to run, and could become flaky due to issues like random network failures or tests overwriting one another’s data.

Test doubles come in handy in such cases. A test double is an object or function that can stand in for a real implementation in a test, similar to how a stunt double can stand in for an actor in a movie. The use of test doubles is often referred to as mocking, but we avoid that term in this chapter because, as we’ll see, that term is also used to refer to more specific aspects of test doubles.

Perhaps the most obvious type of test double is a simpler implementation of an object that behaves similarly to the real implementation, such as an in-memory database. Other types of test doubles can make it possible to validate specific details of your system, such as by making it easy to trigger a rare error condition, or ensuring a heavyweight function is called without actually ...

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.
Start your free trial

You might also like

Software Engineering at Google: Lessons Learned from Programming Over Time

Software Engineering at Google: Lessons Learned from Programming Over Time

Titus Winters, Tom Manshreck, Hyrum Wright
Head First Software Architecture

Head First Software Architecture

Raju Gandhi, Mark Richards, Neal Ford
Grokking Algorithms

Grokking Algorithms

Aditya Bhargava

Publisher Resources

ISBN: 9781492082781Errata Page