By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
So far you have found bugs by running Penny and reading what it prints. That works while the program is small. It stops working when a program has twenty functions and you change one line: how do you check that every other rule still behaves?
A test is a small Go function whose job is to call your code and compare the result with what you expected. You write it once, and from then on go test checks it in under a second. A machine that checks your work every time is faster and more reliable than a tired person checking it by hand.
Tests live in files whose names end in _test.go, and every test function follows three rules:
TestAdd.t *testing.T, from the testing package.Here is a small add function and its test. The // ==== file: ... lines are only labels for this lesson: each label starts a real file in the same folder.
// ==== file: add.go ====
package main
import "fmt"
func add(a, b int) int {
return a + b
}
func main() {
fmt.Println(add(2, 3))
}
// ==== file: add_test.go ====
package main
import "testing"
func TestAdd(t *testing.T) {
got := add(2, 3)
want := 5
if got != want {
t.Errorf("add(2, 3) = %d; want %d", got, want)
}
}| Piece | What it does |
|---|---|
func TestAdd(t *testing.T) | A test function: the name starts with Test and a capital. |
add(2, 3) | The function under test, called with this case's values. |
if got != want | The check: got versus want. |
t.Errorf(...) | Records a failure with a message; the test continues. |
add_test.go | The file name convention: this file holds tests. |
A passing test prints nothing at all. A failing test prints a file, a line and the message you wrote. Change want := 5 to want := 6 on purpose and run the command:
$ go test ./...
--- FAIL: TestAdd (0.00s)
add_test.go:9: add(2, 3) = 5; want 6
FAIL
exit status 1
FAIL penny 0.002sRead a failure in three parts: which test failed, which line noticed, and what the two values were. Then open that line in your editor. You already know this rhythm from reading compiler errors in step 4.
t.Errorf records a failure and lets the test continue, so one run can report several problems in the same function. t.Fatalf records the failure and stops this test immediately. Use Fatalf when continuing would crash or produce a confusing second error, for example when a call should have returned an error and did not.
# Run every test in the current module: the one command to remember.
go test ./...
# Add -v when you want to see each test run by name.
go test -v ./...
# Run only the tests whose name matches a pattern.
go test -run TestAdd ./...When you see FAIL, scroll up to the first failure. Fix that one first: a later failure is often a consequence of the earlier one, exactly like a second compiler error after a missing brace.
Tests are for the rules your project depends on. Penny stores money in integer cents and shows it with formatCents, so a wrong format would appear in every screen of the app. That rule deserves a test of its own.
// ==== file: money.go ====
package main
import "fmt"
func formatCents(cents int) string {
return fmt.Sprintf("$%d.%02d", cents/100, cents%100)
}
func main() {
fmt.Println(formatCents(1250))
}
// ==== file: money_test.go ====
package main
import "testing"
func TestFormatCents(t *testing.T) {
got := formatCents(1250)
want := "$12.50"
if got != want {
t.Errorf("formatCents(1250) = %q; want %q", got, want)
}
}Run go test ./... again. Two tests now run, and both stay silent when they pass. That silence is the goal of every run.
Go treats files ending in _test.go differently: they are compiled only when you run go test, never when you build or run the program. That one rule gives you four things at once:
go test ./... discovers every test file.add directly, with no import path.The naming rule is not decoration. It is how the toolchain knows which files are part of the program and which files are part of the check.
Add a function subtract(a, b int) int to add.go and a test TestSubtract in add_test.go. Run go test ./... once. Then change want to a wrong number, run the test again, read the failure, and fix it. Finally write a second test for formatCents(0) and decide what the answer should be before you run it.
Key takeaways
A test is a TestXxx function in a file ending in _test.go.
t.Errorf records a failure; t.Fatalf also stops the test.
go test ./... runs every test; passing tests print nothing.
Tests call your code directly and never ship in the binary.
Test the rule you rely on, not an accidental value.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.