• Home
  • Blog
  • Schedule
  • Syllabus

Contents

  • Overview
  • Testing Overview
    • Why Do We Write Tests?
    • The Story of the Google Web Server
    • Write, Run, React
    • Benefits of Testing Code
    • Test Size
    • Flaky Tests Are Expensive
    • Properties Every Test Should Have
    • Test Scope and the Test Pyramid
    • The Beyoncé Rule
    • A Note on Code Coverage
    • Testing at Google Scale
    • How Google Changed Its Testing Culture
    • The Limits of Automated Testing
  • Unit Testing
    • Unit Tests and Engineering Productivity
    • Brittle and Unclear Tests
    • Preventing Brittle Tests
    • Writing Clear Tests
  • Action Items

Testing Overview and Unit Testing

post
professional development
software engineering
Why does Google treat automated testing as everyone’s job?
Authors

Lukas Deemer

Alexander Goddard

Gregory M. Kapfhammer

Published

November 30, 2026

Overview

This blog post discusses the Testing Overview and Unit Testing chapters in the Software Engineering at Google book. Chapter 11, written by Adam Bender, explains why Google made developer-driven, automated testing a core part of its engineering culture and how it organizes a test suite by the size and scope of each test. Chapter 12, written by Erik Kuefler, looks closely at the most common kind of test at Google, the unit test, and at the habits that keep unit tests maintainable. Along the way, we connect these ideas to our work on GatorGrade and introduce testpyramid, a command-line tool that checks a pytest suite against Chapter 11’s advice.

Testing Overview

Why Do We Write Tests?

Catching bugs is only part of the reason to test. The chapter argues that an equally important reason is to make change safe. When a project has a suite of automated tests, engineers can add features, refactor, or redesign a system and find out within minutes whether they broke something. Writing tests also improves design, since a test is the first “user” of new code. If code is hard to test, it often has too many responsibilities or depends too tightly on something like a database.

The simplest test has four parts: a single behavior to check, a specific input, an observable output, and a controlled environment such as one isolated process. Hundreds or thousands of these simple tests together form a test suite that tells a team whether the whole product still works as intended.

The Story of the Google Web Server

In Google’s early days, engineers mostly relied on being smart to get software right. The Google Web Server (GWS), which serves Google Search queries, showed where that leads. By 2005, more than 80% of its production pushes contained user-facing bugs that had to be rolled back. The tech lead responded by requiring that every new code change include tests that would run continuously. Within a year, emergency pushes dropped by half, even though the team was making more changes than ever.

The key lesson is that programmer ability alone cannot prevent defects at scale. The chapter imagines a team of 100 excellent engineers who each write only one bug a month. Together, they still create about five new bugs every workday. A test, unlike a debugging session, is a shared resource: once one engineer writes it, everyone on the team benefits every time it catches a problem.

How does the GWS story relate to the bugs we have fixed in GatorGrade?

Click to Expand for the Answer
  • when our team fixed issue #226, the bug only appeared on Windows, so none of us would have caught it by running the tool by hand on our own laptops
  • adding tests for the fix means the next contributor who touches path handling will find out right away if they bring the bug back

Write, Run, React

The chapter describes automated testing as three activities:

  1. Write tests as small pieces of code that set up an environment, call the system with a known input, and check the result.
  2. Run those tests often, ideally on every change, through continuous integration. Unlike human testers, machines never get bored of repeating the same checks thousands of times a day.
  3. React to failures quickly. A team that lets broken tests pile up loses the value of its suite, while a team that fixes a failing test within minutes keeps confidence high.

Benefits of Testing Code

Writing tests can take as long as writing the feature itself, so it may seem like testing slows a team down. Google found the opposite. The chapter lists these benefits:

BenefitWhat it means for a team
Less debuggingBugs are caught before code is submitted, and a test keeps paying off for the life of the project.
Confidence in changesRefactoring is safe because important behaviors are checked continuously.
Better documentationA clear, focused test shows exactly what the code does in one case.
Simpler reviewsReviewers can check that each case has a passing test instead of tracing it in their heads.
Thoughtful designCode that is hard to test is a sign of tight coupling or too many responsibilities.
Fast releasesTeams can release with confidence, sometimes every day.

The “simpler reviews” benefit connects to our SE9 post on Code Review. When a pull request includes tests for its edge cases, a reviewer can spend their attention on design instead of on whether the code runs at all.

Test Size

Google classifies every test along two separate dimensions. Size is about the resources a test needs to run, like memory, processes, and time. Scope is about how much code a test is meant to verify. These are related, but they are not the same thing.

Google encourages engineers to write the smallest test that can check a piece of functionality. A test’s size depends on how it runs, not on how many lines it has:

SizeWhere it runsWhat it may do
Smalla single process (often a single thread)no sleeping, no disk or network access, no other blocking calls
Mediuma single machinemultiple processes and threads, blocking calls, network calls to localhost only
Largeanywhere, including multiple machinesnetwork calls to remote systems

Google uses sizes instead of the usual labels “unit” and “integration” because the qualities it cares about most are speed and determinism. A small test cannot reach the main sources of slowness and randomness, such as the network or the disk, so it is very hard to accidentally make it slow or flaky. When a small test needs a heavy dependency like a database, it uses a test double instead. Google even enforces these limits in its infrastructure. For example, Java tests tagged as small fail if they try to open a network connection.

Is a test that uses pytest’s tmp_path fixture to write a file small or medium?

Click to Expand for the Answer
  • by Google’s definition, it is medium, because it performs disk I/O
  • the chapter does allow one exception: a small test may use a file system if it is a hermetic, in-memory implementation

Flaky Tests Are Expensive

A flaky test fails sometimes even though nothing about the code changed. The chapter gives a memorable example: if each of 10,000 tests has just a 0.1% chance of failing when it should pass, a team will investigate about 10 flakes every day. Rerunning failed tests can hide the problem for a while, but it only trades CPU time for engineering time and delays fixing the root cause.

The bigger danger is lost trust. Google’s experience is that as flakiness approaches 1%, engineers stop reacting to failures and the suite loses its value. Google’s own flaky rate hovers around 0.15%, which still means thousands of flakes every day, so it actively spends engineering hours fixing them. Most flakes come from nondeterminism such as clock time, thread scheduling, and network latency.

Properties Every Test Should Have

Whatever its size, a test should be hermetic. It should contain everything it needs to set up, run, and tear down its environment, and it should not depend on outside state such as a shared database or the order in which tests run. A test should also be “obvious upon inspection.” Because nobody writes tests for their tests, the chapter strongly discourages conditionals and loops inside a test, since any extra logic is a place for a bug to hide.

Test Scope and the Test Pyramid

Scope describes how much code a test validates. Narrow-scoped tests, usually called unit tests, check one class or method. Medium-scoped tests, or integration tests, check how a few components work together, like a server and its database. Large-scoped tests, such as end-to-end or system tests, check how many parts of a system behave together.

As a rough guideline, Google aims for about 80% unit tests, 15% integration tests, and 5% end-to-end tests. This mix forms a pyramid, a version of the test pyramid that Mike Cohn described. Unit tests make a strong base because they are fast, stable, and make failures easy to diagnose. The chapter warns about two shapes to avoid:

flowchart LR
    P["Pyramid<br/>many unit tests,<br/>fewer integration tests,<br/>few end-to-end tests"]
    C["Ice cream cone<br/>many end-to-end tests,<br/>few unit tests"]
    H["Hourglass<br/>many unit and end-to-end tests,<br/>few integration tests"]

  • the ice cream cone often appears in prototypes rushed to production, and its suites are slow, unreliable, and hard to work with
  • the hourglass often comes from tight coupling that makes it hard to set up individual dependencies, so failures show up only in slow end-to-end tests

Our testpyramid tool, described at the end of this post, measures this shape for a real suite. When we ran it on GatorGrade’s tests, it found 844 test functions. About 77% were small and about 23% were medium, with no large tests:

Found 844 test functions

size     count   share  target  bar
large        0    0.0%      5%  ..............................
medium     192   22.7%     15%  #######.......................
small      652   77.3%     80%  #######################.......

Shape: pyramid

That is close to Google’s target, which matches our experience that GatorGrade’s suite runs quickly on our laptops. Almost all of the medium tests use pytest’s tmp_path fixture to create files, which makes sense for a tool whose job is to run checks on the files in a project.

The Beyoncé Rule

When new hires ask what they should test, Google’s answer is to test everything you do not want to break. The chapter calls this the Beyoncé Rule: “If you liked it, then you shoulda put a test on it.” This includes correctness, performance, accessibility, and security. It also includes how a system handles failure. Instead of waiting for a real outage, teams should write tests that simulate errors, such as exceptions in unit tests or injected delays and failures in integration tests.

The rule matters most for infrastructure teams that change code across the whole company. If their change passes all of your tests but still breaks your product, the missing test is your responsibility.

A Note on Code Coverage

Our FB2 post on Coverage showed how to measure which lines a test suite executes. The chapter warns against treating that number as the goal. Coverage only shows that a line ran, not that a test checked its result. Coverage targets also tend to backfire: a team that sets an 80% bar often starts treating it as a ceiling instead of a floor.

Instead, the chapter suggests asking broader questions. Will everything our users expect to work keep working? Will we catch breaking changes in our dependencies? Are our tests stable? Our FB4 post on Mutation Analysis offers one more concrete check, since a mutant that survives shows code that runs during tests but is never really verified.

If our project has 100% coverage, does that mean it is well tested?

Click to Expand for the Answer
  • no, because a test can run every line without asserting anything useful
  • coverage is best used to find code that is not tested at all, not to prove that tested code is correct

Testing at Google Scale

Google keeps almost all of its code, more than two billion lines, in a single monorepo. Close to 25 million lines change every week, about half of them from automated systems, and almost no teams use branches. Every change goes straight to the head of the repository, so Google relies on its continuous integration system and its Test Automation Platform (TAP) to keep the codebase working.

At this scale, poorly written tests become a burden. Brittle tests that over-specify results, often through overuse of mock objects, can break when someone makes an unrelated change. Slow tests get run less often, and some engineers even skip them entirely. One common cause of slowness is calling functions like sleep() to wait for something to happen. The chapter suggests polling for the expected state with a timeout instead. Its overall advice is to treat tests like production code: reward engineers for solid tests and invest in tools that make bad tests harder to write.

How Google Changed Its Testing Culture

Until 2005, testing at Google was more of a curiosity than a practice. A group of volunteers called the Testing Grouplet changed that with three initiatives:

  1. Orientation classes taught every new hire about automated testing and presented it as standard practice. Within a year or two, engineers who had learned to test outnumbered those who had not.
  2. Test Certified gave teams five levels of concrete steps to improve, starting with setting up a continuous build and classifying tests by size. A dashboard showing each team’s level created friendly competition, and the program helped more than 1,500 projects before a tool called Project Health (pH) replaced it in 2015.
  3. Testing on the Toilet started as a joke in 2006, with one-page testing tips posted in restroom stalls. It became the longest-running and most influential of the three initiatives.

Notably, the Grouplet chose not to ask leadership for a testing mandate. It believed that successful ideas would spread on their own, and that engineers who chose to write tests would keep doing so without being forced.

Why might a mandate have worked worse than orientation classes and Testing on the Toilet?

Click to Expand for the Answer
  • a mandate would have clashed with Google’s engineering culture and could have slowed progress
  • engineers who understand why tests help keep writing good ones, while engineers who are only following a rule may write tests that just satisfy it

The Limits of Automated Testing

Not everything can be automated. Judging the quality of search results or of audio and video still needs people, and humans are better at creative work like finding complex security vulnerabilities. The chapter calls this kind of work exploratory testing: treating the application as a puzzle to break. Once an exploratory tester finds a problem, the team should add an automated test so the problem never returns. Automating the well-understood behaviors frees human testers to focus on the areas where they add the most value.

Unit Testing

Unit Tests and Engineering Productivity

A unit test validates a relatively narrow part of a system, such as a class or method. Although unit tests are usually small and fast, Google emphasizes that scope and size are different: a unit test can still consume significant time or exercise more code than one might expect. The main benefit is a short feedback loop. Fast, deterministic tests let engineers run them frequently, isolate problems, and make changes with greater confidence.

Unit tests also improve test coverage, demonstrate intended behavior, and can serve as executable examples. Because maintainability becomes a larger concern as a codebase grows, the chapter treats test quality as engineering work rather than as a task to complete after implementation.

Brittle and Unclear Tests

A brittle test fails after an unrelated or harmless change. For example, a test may depend on private state or the exact order of internal method calls. After a refactoring, that test fails even though public behavior has not changed. Individual brittle tests are manageable in a small project, but in a large suite they create repeated maintenance work and undermine trust.

An unclear test may pass or fail, but an engineer cannot quickly explain why. It may hide important setup in helpers, omit meaningful input values, or produce a failure message without enough context. Clarity is especially important because tests often outlive their original authors.

What makes an ideal unit test?

Click to Expand for the Answer

The ideal test changes only when the system’s required behavior changes. It survives refactoring and the addition of unrelated features, but it still detects new bugs and missing behaviors. It is also fast, deterministic, and focused enough that a failure identifies a real problem rather than a test implementation detail.

Preventing Brittle Tests

The chapter divides common production changes into refactorings, new features, bug fixes, and behavior changes. Existing tests should normally remain unchanged after a refactoring, new feature, or bug fix. A bug fix should add a missing test, while a feature or behavior change should add or update tests only when the required behavior actually changes.

Google recommends testing through public APIs when possible. Public APIs provide a stable description of behavior, so tests do not need to know about private fields or implementation structure. Tests should also verify resulting state, such as the value a method returns or data visible to a caller, instead of merely checking which internal methods were called. Interaction testing is useful only when the interaction itself is the behavior under test.

Writing Clear Tests

A clear test immediately communicates its purpose and explains the reason for a failure. The chapter suggests that test code should be complete but concise: include all necessary setup and inputs, but omit irrelevant details that hide what matters. A useful test often follows a descriptive Arrange-Act-Assert structure, without adding control flow or complex logic that makes the test difficult to check.

Descriptive test names matter, especially when a suite contains many cases. A name such as rejects_negative_balance_when_account_is_frozen communicates more than test_account. Failure messages should include the actual and expected values and, where useful, the relevant inputs. This turns a red test into a diagnostic rather than a puzzle.

The chapter also distinguishes between test behavior and test methods. Tests should describe externally meaningful behavior instead of mirroring a method’s code structure. This supports more stable tests and makes the suite useful as behavioral documentation.

When should tests duplicate setup code instead of using an abstraction?

Click to Expand for the Answer

A test can benefit from DAMP—descriptive and meaningful phrases—even when doing so repeats setup code that would be avoided in production. DRY helps production code share implementation, but test code needs to remain easy to read on its own. A small, meaningful helper is useful when it removes irrelevant setup; a helper that hides the input or expected behavior makes a test less clear.

Action Items

Chapter 11 suggests several changes to how we test our class projects:

  • Start running our full test suite on every pull request in continuous integration and fixing a failing test before merging anything else, so that failures never pile up.
  • Start adding a regression test with every bug fix, as we did for GatorGrade issue #226, so that the Beyoncé Rule protects the behaviors we care about.
  • Stop using sleep() to wait for results in tests. Instead, use test doubles or poll for the expected state with a timeout.
  • Stop treating a coverage percentage as proof that our code is tested well, and check what our tests actually assert.
  • Continue writing mostly small, fast unit tests, and use testpyramid to check that our suites keep a pyramid shape as the projects grow.
NoteOpen-Source Tool for Software Engineers

Our team built testpyramid, a dependency-free Python command-line tool that checks a pytest suite against the ideas in Chapter 11.

The scan command reads test files with Python’s ast module, without running them, and sorts each test into one of Google’s three sizes. A test that calls a remote URL is large, while one that touches the disk, sleeps, or starts a subprocess is medium. The tool then compares the suite with Google’s 80/15/5 mix and reports whether it is shaped like a pyramid, an ice cream cone, or an hourglass. It also lists tests that call sleep() and tests that contain loops or conditionals. The flakes command uses the chapter’s flaky test math to estimate how many flakes a suite will produce each day and how many engineering hours they will cost.

You can view the project here: testpyramid on GitHub.

Return to Blog Post Listing

DevDev

Top