0Pricing
Go Academy · Lesson

Writing Unit Tests with testing

TestXxx functions, t.Error, t.Fatal, t.Helper

Writing Unit Tests with testing is a free Go Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

testing package

Go's built-in testing package provides everything needed for unit tests: test functions, failure reporting, and test binaries. No external framework is required.

Test function signature

Test functions must start with Test, take *testing.T, and live in files ending with _test.go.

func TestAdd(t *testing.T) {
    got := Add(2, 3)
    if got != 5 {
        t.Errorf("Add(2,3) = %d; want 5", got)
    }
}

t.Error vs t.Fatal

t.Error marks failure and continues; t.Fatal marks failure and stops the current test immediately. Use Fatal when subsequent assertions depend on the failed one.

Running tests

Run all tests: go test ./.... Run a specific test: go test -run TestAdd. Add -v for verbose output.

go test -v -run TestAdd ./...

t.Helper

Mark assertion helpers with t.Helper() so error messages report the caller's line, not the helper's line.

func assertEqual(t *testing.T, got, want int) {
    t.Helper()
    if got != want {
        t.Errorf("got %d; want %d", got, want)
    }
}

TestMain

TestMain(m *testing.M) is an optional entry point for setup/teardown before and after the entire test suite runs (e.g., starting a test database).

func TestMain(m *testing.M) {
    setup()
    code := m.Run()
    teardown()
    os.Exit(code)
}

Subtests with t.Run

Group related cases with t.Run. Each subtest runs independently and can be targeted with -run TestFoo/SubcaseName.

func TestMath(t *testing.T) {
    t.Run("addition", func(t *testing.T) { /* ... */ })
    t.Run("subtraction", func(t *testing.T) { /* ... */ })
}

Testing files structure

Tests in the same package can access unexported identifiers. Tests in a _test package (black-box tests) can only access exported identifiers.

// white-box: package mylib
// black-box: package mylib_test

go test -cover

Measure code coverage with go test -cover. For a detailed HTML report: go test -coverprofile=c.out ./... && go tool cover -html=c.out.

Testing with interfaces

Design code against interfaces so tests can pass in fake implementations without starting real services.

t.Parallel

Call t.Parallel() at the start of a test to mark it as safe to run concurrently with other parallel tests, speeding up the test suite.

func TestRead(t *testing.T) {
    t.Parallel()
    // ...
}

Quick Check

What is the difference between t.Error and t.Fatal?

Recap: Unit Testing with testing

Key points:

  • Test functions: TestXxx(t *testing.T) in _test.go files
  • t.Error continues; t.Fatal stops the test
  • t.Helper() for clean error line numbers in helpers
  • t.Run for subtests; t.Parallel for concurrent tests

Frequently asked questions

Is the “Writing Unit Tests with testing” lesson free?

Yes — the full text of “Writing Unit Tests with testing” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.

What will I learn in “Writing Unit Tests with testing”?

TestXxx functions, t.Error, t.Fatal, t.Helper You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Go Academy?

No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Writing Unit Tests with testing” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Go Academy lesson?

Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Writing Unit Tests with testing
  2. Table-Driven Tests
  3. Test Doubles: Mocks and Stubs
  4. Test Coverage and testify
← Back to Go Academy