Reviewer · Testing
Meaningful, idiomatic test coverage
Tests should genuinely exercise the behavior they claim to cover, and should be written using the project's established testing idioms and helpers rather than ad-hoc replacements.
Tests should genuinely exercise the behavior they claim to cover, and should be written using the project’s established testing idioms and helpers rather than ad-hoc replacements.
- Exercise the real code path, not a stand-in for it:
- Avoid mocking out so much of the system that the test no longer validates meaningful behavior (e.g. a handler test that mocks the backend entirely is better written as an integration test exercising the real API/backend).
- When fixing a bug/race (e.g. retry logic, error-swallowing branches, idempotent deletes), add a test that actually drives the code through the changed branch, not just a helper function in isolation. If a guard or branch would still pass with the fix “simplified away,” the test isn’t sufficient.
- Cover the scenarios the change claims to fix, including edge cases (e.g. multiple retries/truncations, not just one; both “named ref remains” and “named ref lost” cases; both container and host UTS/IPC conflict combinations).
- Don’t write tests whose only effect is to validate a third-party/stdlib/vendored dependency (e.g.
units.FromHumanSize, upstream name-generator libraries) — test your own code’s integration with it instead, or drop the test. - Prefer testing through the public API/CLI surface actually used in production (e.g. verify “intermediate images are hidden” via
docker build, not just via manual commit sequences) so the test doesn’t diverge from real behavior.
- Reuse existing test infrastructure instead of reinventing it:
- Use
testEnv.APIClient()(asapiClient) rather than constructing a new client by hand in integration tests. - Use existing fixtures/helpers (e.g.
internal/testutil/fixtures/plugin,container.Remove,d.StartWithBusybox(),testutil.RandomAlpha,pkg/process.Kill) instead of duplicating similar logic. - Default new tests to
t.Context()instead ofcontext.Background(), and uset.Helper()in test helper functions. - Pick the correct environment predicate for what’s actually being checked (e.g.
testEnv.IsRootlessfor daemon rootless state vs. a dedicated client-side check), and prefer the API client over scraping CLI output so sentinel errors can be asserted precisely (e.g.cerrdefs.IsPermissionDenied) rather than just matching error strings.
- Use
- Prefer rich, informative assertions:
- Use
gotest.tools/v3(assert/is) consistently rather than mixing intestifyor raw boolean checks. - Prefer
is.DeepEqual,is.Contains, andis.Equalover manual boolean comparisons so failures show the actual vs. expected values. - Use
t.Errorf/t.Fatalfappropriately (t.Errorfto keep a subtest running and surface all failures, per Go’s “keep going” testing guidance).
- Use
Example of turning a weak, over-mocked test into a meaningful one:
// Weak: mocks the backend entirely, so it doesn't exercise real exec/start behavior.
func TestPostContainerExecStartError(t *testing.T) { ... }
// Better: drive it through the real stack as an integration test.
func TestExecStartExecutableNotFound(t *testing.T) {
ctx := setupTest(t)
apiClient := testEnv.APIClient()
// ... create container, exec a nonexistent binary, assert exit code and
// stderr contain "executable file not found" ...
}
Applying this standard catches regressions that superficial or duplicated tests miss, and keeps the test suite consistent and easy to read across the codebase.