liveBook · Manning

Integration test

As explained in Go by Example

An integration test verifies behavior involving interactions between a program and external systems or components.

An integration test verifies behavior across a program and external systems or components. In Go, a realistic test can connect a service to a unique SQLite test database, exercise its public method, and check both successful behavior and database-driven failures. section 10.0 section 10.2.3

Testing a database-backed service

Create a test database with DialTestDB(t), construct a Shortener with that database, and call Shorten using t.Context(). First verify that a link returns its expected key. Then submit the same link again and require link.ErrConflict rather than exposing SQLite's constraint error. Running the test initially reveals the leaking driver error; errors.As can identify that low-level error so the service can return an application-specific one. section 10.2.3

A practical integration test emphasizes:

  • Using a unique test database for isolation between tests. section 10.2.3
  • Passing t.Context() so cancellation follows the test lifetime. section 10.2.3
  • Checking application-level errors instead of coupling callers to database-driver details. section 10.2.3 section 10.6
  • Cleaning up resources with T.Cleanup or TB.Cleanup before the test finishes. section 10.6

How Go by Example covers it

How deep it goes
Builds a database-backed test with isolated setup, test-scoped context, assertions, failure diagnosis, and error translation.
Examples
Exercises link shortening, duplicate-key rejection, SQLite constraint failures, parallel tests, and cleanup.
What it assumes
Assumes Go testing and service methods, while demonstrating test databases, test-scoped contexts, and database error inspection.
What you can do after
Diagnose/debug with it by exposing integration failures and translating low-level database errors into stable application errors.
How it connects
Connects SQL databases, SQLite, test databases, contexts, error chains, and application-level error handling.

Also explained in

Compare how 31 books explain integration test →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go by Example. The section links point to the parts of the book it draws on.