Not exploring all the Go test features

Code coverage

go test -coverpkg=./... -coverprofile=coverage.out ./...
go tool cover -html=coverage.out

Black box testing

package counter
package counter_test

Utility functions

func TestCustomer(t *testing.T) {
  customer := createCustomer(t, "foo")
  // ...
}

func createCustomer(t *testing.T, name string) Customer {
  // ...
  if err != nil {
    t.Fatal(err)
  }

  return customer
}

Setup and tear-down

Per function

func TestMySQLIntegration(t *testing.T) {
  // ...
  db := createConnection(t, "tcp(localhost:3306)/db")
  // ...
}

func createConnection(t *testing.T, dsn string) *sql.DB {
  db, err := sql.Open("mysql", dsn)
  if err != nil {
    t.FailNow()
  }

  t.Cleanup(
    func() {
      _ = db.Close()
    }
  )

  return db
}

Per package

func TestMain(m *testing.Main) {
  setupMySQL()
  code := m.Run()
  teardownMySQL()
  os.Exit(code)
}

References