Express the rule with table-driven cases
For a fee calculation, define cases for an empty order and values around the threshold. Store each expected result explicitly and run each case as a named subtest. This keeps the rule visible and gives failures useful names without duplicating the same assertion and setup for every ordinary input.
Use t.Fatalf when the rest of the test cannot continue meaningfully after an unexpected result. Use t.Errorf when collecting additional independent failures is useful. Include expected and actual values in the message so the output remains actionable when it appears in a continuous integration log without local debugging context.
Control dependencies with small seams
Pass a clock or external lookup through a narrow interface or function when the rule depends on it. A small fake can return a fixed result or error. Avoid designing broad interfaces only for mocks; the seam should represent the behavior the production caller actually requires from that dependency.
Use t.Cleanup for state restoration and temporary resources where appropriate. Keep filesystem work under temporary directories and avoid mutating package globals across parallel cases. If subtests share a mutable fake, running them concurrently can change the test meaning and produce intermittent failures unrelated to the production behavior under examination.
Run the right package scope
Use the project's normal go test invocation to verify package discovery and build constraints. A test that passes only from a special working directory may depend on hidden paths or environment setup. Document intentional integration prerequisites rather than making ordinary unit checks reach a live service by default during local development.
When concurrency matters, add focused checks and use the race detector where supported and appropriate. It does not prove all concurrency behavior correct. Keep assertions about returned values and state transitions, and validate known failure paths so an error-handling branch cannot remain untested behind an otherwise successful table of cases.
Working sheet
| Need | Mechanism | Design note |
|---|---|---|
| Related boundaries | Table-driven subtests | Explicit expected values |
| Fatal setup failure | t.Fatalf | Stop misleading follow-up |
| State cleanup | t.Cleanup | Restore after failure |
| External lookup | Narrow interface or function | Return controlled error |
Questions about write Go unit tests with table-driven cases
Does Go require a third-party test framework?
No. The standard testing package provides the core runner and failure reporting.
Should every subtest run in parallel?
Only when setup and mutable state are independent. Parallel execution can expose accidental sharing.
How should I use the working sheet?
Download the CSV and replace the sample rows with evidence from your repository. Record a clear acceptance check and an owner for each item. Keep unresolved assumptions visible rather than treating the example as a completed assessment.
Sources and further reading
Examples are illustrative. They are not customer results, security guarantees, or a substitute for checking the documentation for your installed version.
