Free preview.You're sampling one lesson — enroll free to unlock all 8 lessons and track your progress.
Enroll free
lesson

Why and What to Test

Why and What to Test

In this lesson — part of Testing Fundamentals — you'll learn why and what to test in Go and why it matters in real work.

Why it matters

Tests prove your code works and keep it working as you change it.

Key ideas

  • What to test
  • Arrange-Act-Assert
  • Running a test suite
  • Good vs. brittle tests

In practice

Here's how it looks in idiomatic Go:

package math

import "testing"

func Add(a, b int) int { return a + b }

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

Go note: Go's built-in testing package needs no assertions library: name tests TestXxx(t *testing.T) in a _test.go file and run go test.

Try it yourself

Exercise: In Go, write three tests for a function that reverses a string.

Recap

You now understand why and what to test and can apply it in Go. Mark this lesson complete and continue to the next one.