Table Driven Unit Test Go

Cover function branches with table-driven tests: declarative cases with names, parallel subtests, and precise failure output.

GoCli Tooling

Code

gosnippet
73 lines
package main

import (
	"errors"
	"testing"
)

var errNotFound = errors.New("order not found")

func ShippingCost(weightKg int, express bool, tier string) (int, error) {
	if weightKg <= 0 {
		return 0, errors.New("weight must be positive")
	}
	if weightKg > 30 {
		return 0, errors.New("weight exceeds courier limit")
	}

	base := 8
	if weightKg > 10 {
		base += (weightKg - 10) * 2
	}
	if express {
		base *= 2
	}
	if tier == "gold" {
		base -= 4
	}
	return base, nil
}

func TestShippingCost(t *testing.T) {
	t.Parallel()

	tests := []struct {
		name    string
		weight  int
		express bool
		tier    string
		want    int
		wantErr error
	}{
		{name: "light standard", weight: 2, express: false, tier: "", want: 8},
		{name: "light express", weight: 2, express: true, tier: "", want: 16},
		{name: "heavy adds per kg", weight: 15, express: false, tier: "", want: 18},
		{name: "heavy express gold", weight: 15, express: true, tier: "gold", want: 32},
		{name: "gold floor at zero", weight: 1, express: false, tier: "gold", want: 4},
		{name: "zero weight rejected", weight: 0, express: false, tier: "", wantErr: errors.New("weight must be positive")},
		{name: "over courier limit", weight: 31, express: false, tier: "", wantErr: errors.New("weight exceeds courier limit")},
	}

	for _, tt := range tests {
		tt := tt
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()

			got, err := ShippingCost(tt.weight, tt.express, tt.tier)
			if tt.wantErr != nil {
				if err == nil || err.Error() != tt.wantErr.Error() {
					t.Fatalf("ShippingCost(%d, %v, %q) error = %v, want %v",
						tt.weight, tt.express, tt.tier, err, tt.wantErr)
				}
				return
			}
			if err != nil {
				t.Fatalf("unexpected error: %v", err)
			}
			if got != tt.want {
				t.Errorf("ShippingCost(%d, %v, %q) = %d, want %d",
					tt.weight, tt.express, tt.tier, got, tt.want)
			}
		})
	}
}

How it works

Count the functions ShippingCost and TestShippingCost as the entire surface area of Table Driven Unit Test Go.

The one repetition in this snippet is the loop for _, tt := range tests. Every fallible call checks err != nil (1 site), so an error is handled where it happens rather than bubbling invisibly.

What to watch for

  • Error values are returned, not thrown — every call site must inspect err before using the other return values.

Applying it

Entry signature: func ShippingCost(weightKg int, express bool, tier string) (int, error)

Treat ShippingCost as the API: import it, and ignore the supporting code.

Statements that matter

return 0, errors.New("weight exceeds courier limit")
return 0, errors.New("weight must be positive")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

Interactive Tools for This Topic

FAQ

How does the code signal failure?
Through `error` values, not panics — the snippet checks `err != nil` at 1 site.
Are there third-party dependencies?
No. The dependency list is empty; the code stays inside the standard library. The file defines `ShippingCost`, `TestShippingCost` at load time.