01
Testing and Measurement
Setting up test functions and helpers with the language's own package, raising coverage with a case list, server-side and client-side test helpers, faking resting on interface separation, only one of a benchmark's two numbers being reproducible, and what coverage data does and does not say.
- 01 Test Package Basics The same test file states how many test functions the source contains by counting the text, but states the test's verdict only by running it: against a correct source both tests pass, against a broken source the verdict reverses while the test count stays the same. The limiting measurement runs a test function that carries no assertion and shows that it passes in exactly the same way.
- 02 Table-Driven Tests A five-case table's case count is counted from the source, and the number of running subtests is read only by running go test -v — the two match at 5. The limiting measurement shows that adding a sixth case that runs the same lines as an existing one leaves the coverage ratio unchanged at 100.0%.
- 03 HTTP Tests The same handler is tested with three requests without ever opening a real port: an existing item returns 200, a nonexistent one 404, a request without a parameter 400 — all three run information learned only by calling handler.ServeHTTP. The limiting measurement reads that the request's address is a fixed address RFC 5737 reserves for documentation purposes, showing that the test exercises the handler, not the network.
- 04 Faking and Interface Separation The same ReduceStock body is run with both a recording and a spy Notifier implementation: in a normal reduction both give the same verdict, and the spy object's call count reads 1. The limiting measurement shows that in a call reducing more than the stock, the real implementation rejects the negative remainder while the spy object silently records the same call.
- 05 Benchmarking Eleven pieces of information are gathered about one program and split among three tellers — 2 by the source, 5 by a tool with no run needed, 4 only by running it. The benchmark tool produces one of these four run pieces of information: the allocation count of two writings of the same work gives the same value across five runs; the limiting measurement shows that even this number cannot be compared without saying what it counts.
- 06 Coverage Reporting The same function tested with three separate test sets gives three separate coverage ratios — 57.1%, 85.7%, 100.0% — and the uncovered block count drops correspondingly to 3, 1, and 0. The limiting measurement shows, by running the third set — the one that gives 100% coverage — against a broken source, that it catches no real defect at all in a branch whose return value it never checks.