liveBook · Manning

Atomic test

As explained in Shipping Go: Develop, deliver, discuss, design, and go again

An atomic test is a narrowly scoped test that exercises an independent unit of behavior with minimal reliance on other components.

An atomic test checks one behavior through the smallest useful boundary. In a translation service, call Translate directly instead of routing through the HTTP handler, so failures point more clearly to service logic. section 6.3

Use the boundary that matches the behavior under test:

  • Test translation behavior directly with the service and a table of word-language cases. section 6.3
  • Test request handling separately through the handler when the behavior involves HTTP translation requests. section 6.3
  • Inject the service into the handler so each test can exercise the intended component without changing the surrounding design. section 6.3

Service test versus API test

TestTranslate constructs a StaticService and calls Translate for each table row. TestTranslateAPI instead constructs a handler and exercises the HTTP-facing path. The first test isolates translation behavior; the second checks the assembled API boundary. section 6.3

Listing 6.9 translate_test.go
func TestTranslate(t *testing.T) {
...
 
    underTest := translation.NewStaticService()                   #1
    for _, test := range tt {
        // Act
        res := underTest.Translate(test.Word, test.Language)      #2
...
    }
}

The table-driven service test provides a compact template for exercising one operation across multiple inputs. section 6.3

How Shipping Go: Develop, deliver, discuss, design, and go again covers it

How deep it goes
Distinguishes direct service testing from API-handler testing but does not develop a formal isolation checklist.
Examples
Uses table-driven Translate tests and a separate Translate API handler test.
What it assumes
Assumes familiarity with Go services, handlers, and dependency injection.
What you can do after
Practice focused Go tests by calling a service directly and reserving handler tests for API behavior.
How it connects
Connects atomic tests with dependency injection, interfaces, services, handlers, and API tests.

This explainer was generated by Manning's knowledge graph from the text of Shipping Go: Develop, deliver, discuss, design, and go again. The section links point to the parts of the book it draws on.