Chapter 3: The Testing Mentality
A test is a falsifiable claim plus a cheap experiment that tries to break it. The testing mentality generalizes that beyond unit tests: a type signature tests a claim about shapes; a code review tests a claim about readability; an alert tests a claim about health; a canary tests a claim about a release; an ADR’s “Consequences” section tests a claim about the future. Engineers with the mentality reflexively ask, “what would prove this wrong, and how cheaply can I find out?”, and they ask it about their own beliefs first. Testing is a mentality, not a phase: it is the habit of treating every claim about code, infrastructure, docs, data, or a teammate’s assumption as a hypothesis you can cheaply falsify.
Concepts from other sections (15)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- Contract test: a test of the agreement between two services that runs neither against the other. Read more: 3.6 Contract tests: consumer-driven tests for gateway to engine
- Resilience test: a test that injects a fault at a chosen moment and checks a promise of the system afterwards. Read more: 3.7 Resilience tests
- Test pyramid: the target shape of a test suite, with many small tests, fewer medium tests, and few large tests. Read more: Chapter 2: Software Engineering at Google
- Hyrum’s Law: with enough users, every observable behavior of a system will be depended on by somebody. Read more: Chapter 2: Software Engineering at Google
- Error budget: the bad events an objective allows over its period, as a full outage. Read more: 1.1 Design review: the tracer and the serving platform in Systems Engineering
- Semantic mutant: a hand-written mutant that encodes one known pitfall. Read more: 3.1 TDD, unit tests, and mutation testing
- Idempotence: the property that normalizing twice changes nothing. Read more: 5.2 Extract, normalize, and quality filters in Data Engineering
- Minimum estimator: the smallest repetition’s time, the best estimate of the work when noise only adds time. Read more: 3.4 Benchmarks and perf gates
- Provider verification: checking every interaction against the provider or its published contract. Read more: 3.6 Contract tests: consumer-driven tests for gateway to engine
- task queue: the durable server’s routing key; only pollers of queue
agentreceiveAgentRuntasks. Read more: 10.8 The agent image and chart in Systems Engineering - Exactly-once: the guarantee that each record affects state exactly once. Read more: Chapter 3: Batch and Streaming Processing in Data Engineering
- Bits per byte (bpb): total predicted bits divided by total predicted bytes; independent of the tokenizer. Read more: 3.8 Model evals as tests
- Sign-flip test: an exact permutation test that counts sign vectors whose flipped sum reaches the observed sum. Read more: 3.8 Model evals as tests
- Agent eval suite: a fixed, versioned set of observations, scorers, and a comparison design used as a release gate. Read more: 3.9 Agent evals as tests
- Confidence interval: a procedure that, over repeated samples, produces intervals covering the fixed true parameter at the stated rate. Read more: 13.11 Probability problem set, part c: limit theorems and confidence intervals in Mathematics
This chapter opens with the ideas that shape any test strategy (the pyramid, testing behavior rather than implementation, fakes over mocks, and testing in production) and then turns into a ladder of practice sections, one per kind of test. Each section takes a piece of the real system the other books build, not toy code, shows a reference test suite for it, and explains why each test is there and which faults it kills. Throughout, a suite is judged by mutation testing: which planted faults it catches. The ladder starts with unit tests and mutation scoring, adds property-based tests, oracles, benchmarks, deeper mutation analysis, and contract tests, and ends with resilience tests and evals for models and agents, where the oracle is a statistic rather than an exact value.
Learning objectives
- Shape a test strategy by cost with the test pyramid, and recognize the ice-cream-cone anti-pattern.
- Write tests that assert behavior through the contract, and prefer fakes to mocks.
- Judge any test suite by the planted faults it catches, and argue each surviving mutant.
- Choose the right kind of test (property, oracle, benchmark, contract, resilience, eval) for a given claim.
- Treat production monitoring, canaries, and chaos experiments as tests run against the real system.
Before you begin
Section titled “Before you begin”This chapter builds on Software Engineering at Google, whose testing chapters are the source of the pyramid, fakes-first, and Hyrum’s Law, and on Documentation and technical writing in Software Engineering, because tests are executable documentation. The practice sections use Python, Go, and C; the primers in Programming and Algorithms cover what you need. Each section names its own prerequisites from AI Engineering, AI Engineering, and Mathematics.
Shape your confidence by cost: the test pyramid
Section titled “Shape your confidence by cost: the test pyramid”The goal of a test suite is confidence per unit cost. That argues for many fast, isolated tests and few slow, broad ones:
graph TD
E2E["End-to-End / System<br/>few · slow · brittle · highest fidelity"]
INT["Integration / Medium<br/>some · real dependencies via fakes/containers"]
UNIT["Unit / Small<br/>many · fast · isolated · deterministic"]
UNIT --> INT --> E2E
style UNIT fill:#1168bd,color:#fff
style INT fill:#438dd5,color:#fff
style E2E fill:#85bbf0,color:#000
Small (unit) tests have no I/O, no clock, and no network; they take milliseconds, run on every save, and should be most of your tests. Medium (integration) tests use real dependencies through fakes or Testcontainers (a real Kafka or Postgres in Docker) and take seconds. Large (end-to-end) tests exercise the whole system; they take minutes and are flaky by nature, so keep few of them and treat flakes as bugs.
The anti-pattern is the ice-cream cone: mostly end-to-end tests, few unit tests. Such a suite is slow and flaky, and it tells you something broke without telling you what. Invert it.
Test behavior, not implementation
Section titled “Test behavior, not implementation”The single highest-leverage rule is that a good test states what the code promises, so it survives refactoring and fails only on real regressions. A test that breaks when you refactor without changing behavior is a liability, not an asset:
# BAD -- couples to implementation. Refactoring the cache breaks this test# even though behavior is unchanged.def test_calls_redis_setnx_once(): worker.process(msg) redis_mock.setnx.assert_called_once()
# GOOD -- asserts the promise: a duplicate message is processed exactly once.def test_duplicate_message_processed_once(): worker.process(msg); worker.process(msg) # same message twice assert db.count(order_id=msg.order_id) == 1 # observable behaviorThis is Hyrum’s Law turned to your advantage: depend only on the contract you intend to keep.
Test doubles: fakes over mocks
Section titled “Test doubles: fakes over mocks”The three common doubles differ in how much they couple a test to the implementation:
| Double | What it is | Google’s stance |
|---|---|---|
| Fake | A working lightweight impl (in-memory DB, in-memory Kafka) | Preferred — behaves correctly, tests survive refactors |
| Stub | Returns canned answers | Fine for simple inputs |
| Mock | Asserts how it was called | Use sparingly — mock-heavy tests are brittle and test implementation |
Mock-heavy suites are the most common reason a test breaks during a no-op refactor. Reach for a fake first.
Beyond example-based tests
Section titled “Beyond example-based tests”Examples are only the start. Property-based testing asserts invariants over generated inputs (Hypothesis, proptest, QuickCheck): “encode then decode returns the original,” “the consumer never skips an offset.” It finds the edge cases you would never enumerate by hand, and invariants are properties of the algorithm, which ties it to Algorithms. Fuzzing feeds random or adversarial bytes to parsers and decoders; it targets the bug class behind a huge share of CVEs (cargo fuzz, go test -fuzz, libFuzzer). Golden or snapshot tests pin a complex output (a rendered diagram, an API response) and diff future runs, a cheap regression net for serializers and generators. Contract tests have the producer and consumer of an API each test against a shared contract, so a breaking change is caught in CI rather than in production (Pact, schema registries for Kafka).
Testing things that aren’t application code
Section titled “Testing things that aren’t application code”The mentality applies everywhere knowledge can be wrong. For infrastructure, run terraform plan in CI, validate manifests with kubeconform or kubeval, and write policy tests (OPA/Conftest) for rules such as “no container runs as root”; a misconfigured manifest is a bug, and validating YAML before it reaches the cluster is the subject of Containers, Kubernetes and workloads in Systems Engineering. For data, write pipeline tests with Great Expectations or dbt tests (“no nulls in order_id,” “row count within 3σ of yesterday”); bad data is a production incident, and dbt tests gate the pipeline in Orchestration and modeling in Data Engineering. For documentation, run the code samples, as Documentation and technical writing recommends; an example that doesn’t compile is a failing test. For diagrams, run d2 *.d2 in CI so the build fails if a diagram no longer compiles (Diagramming and the C4 model).
Testing in production, because you already are
Section titled “Testing in production, because you already are”You cannot fully reproduce production, so test against it deliberately rather than pretending staging is enough. A canary or progressive rollout ships to 1% of traffic, watches the metrics, then ramps: a controlled experiment on the real system. Synthetic monitoring has a bot continuously exercise the critical path, so the first to know is you, not the customer. Chaos engineering injects failure (kill a pod, add latency, partition the network) to test the hypothesis “the system tolerates this”; start with a small blast radius and a defined steady-state metric. SLOs and error budgets make reliability a testable claim, and burning the error budget is the test failing in slow motion (see Observability in Systems Engineering, which shows how you test in production through telemetry).
The practice is widespread in industry. Google runs presubmit testing at scale and teaches it one page at a time in Testing on the Toilet; Netflix invented chaos engineering with Chaos Monkey; Amazon relies on operational readiness reviews and canary deploys; and AI labs such as Anthropic run rigorous evals and safety tests to falsify claims about models before shipping. At Staff+ levels you set a team’s testing bar, and the mentality matters more than any coverage percentage.
Choosing a technique
Section titled “Choosing a technique”The techniques map onto situations as follows:
| Technique | When to apply |
|---|---|
| Shape the test pyramid | Designing any service’s test strategy |
| Test behavior, not implementation | Every test you write |
| Prefer fakes over mocks | Any test needing a dependency |
| Property-based testing | Encoders, parsers, invariant-heavy logic |
| Fuzzing | Any code that parses untrusted input |
| Contract tests | Any producer/consumer or service boundary |
| Manifest/policy tests | Any IaC or Kubernetes change |
| Data quality tests | Any data pipeline |
| Canary + synthetic monitoring | Every production release |
| Chaos experiments | Systems claiming resilience — prove it |
To practice the mentality on your own work, read a few Testing on the Toilet posts (each is a five-minute mental model), take one function with example-based tests and add a property-based test to watch it find an edge case you didn’t think of, and, for a system you run, write down the failure hypothesis you are most afraid of and design the smallest experiment that would confirm or refute it.
In this chapter
Section titled “In this chapter”The practice sections form a ladder, each placed where the system first needs that kind of test:
- TDD, unit tests, and mutation testing introduces test-first development and the mutation-testing primer (what a mutant is, killed versus survived, how the score and required semantic mutants work) on a failpoint kata the training CLI uses.
- Property-based tests with Hypothesis, proptest, rapid, and ol_prop.h states laws (roundtrip, idempotence, model, fixpoint, metamorphic) for a normalizer and a mini BPE, and shows why roundtrip alone misses faults.
- Oracles, golden and differential tests, gradcheck as a test tests hand-written backward passes with three oracles and shows why a shape check is not one.
- Benchmarks and perf gates measures C kernels with a minimum estimator and builds a gate that compares base and head on the same machine.
- Mutation testing in depth triages ten survivors of the sequence-metrics suite into equivalent and killable, and writes new semantic mutants from pitfalls.
- Contract tests writes a consumer-driven pact for the gateway’s client of the engine, with a two-sided mock and static provider verification.
- Resilience tests crashes a durable task queue at its worst moments with a fake disk and a manual clock, and checks durability, exclusivity, and exactly-once effects.
- Model evals as tests turns held-out bits per byte and a seeded quality score into a regression test with a margin and an exact sign-flip test.
- Agent evals as tests designs a repeatable agent eval suite with disjoint splits, per-property scorers, and paired confidence intervals.
Key terms
Section titled “Key terms”- Mutation testing: judging a test suite by planting small faults (mutants) in the code and counting how many the suite catches; it measures whether tests check behavior, which line coverage cannot.
- Ice-cream cone: the inverted test pyramid, a suite of mostly end-to-end tests and few unit tests; it is slow and flaky and reports that something broke without saying what.
- Test double: a stand-in for a real dependency in a test: a fake (a working lightweight implementation), a stub (canned answers), or a mock (asserts how it was called). Fakes are preferred because mock-heavy tests couple to the implementation and break on refactors.
- Property-based testing: asserting an invariant over many generated inputs (Hypothesis, proptest, QuickCheck) instead of a few hand-picked examples, which finds edge cases nobody would enumerate.
- Fuzzing / fuzz testing: feeding random or adversarial bytes to parsers and decoders to find crashes and memory errors; it targets the bug class behind a large share of CVEs.
- Testing in production: deliberately testing against the real system with canaries, synthetic monitoring, and chaos experiments, because no staging environment fully reproduces production.
- Synthetic monitoring: a bot that continuously exercises a system’s critical path in production, so the team learns of a failure before customers do.
- Chaos engineering: deliberately injecting failures (killing a pod, adding latency, partitioning the network) to test the hypothesis that a system tolerates them; it starts with a small blast radius and a defined steady-state metric, and needs good observability to be safe.
Further reading
Section titled “Further reading”- Software Engineering at Google, testing chapters (11-14): testing at scale.
- Google Testing Blog / Testing on the Toilet: one-page lessons.
- Hypothesis documentation: property-based testing.
- Principles of Chaos Engineering.
- Martin Fowler, Contract Tests.
- Foundations of Software Testing notes.