Skip to content

Check Expected Results

The Course
•
18 min study

Running a program and reading its output works while a program is small. As it grows, you cannot reread every line of output after every change, and a mistake in a rarely used path can sit unnoticed for weeks. The fix is to let the program check itself: write down what a function should return for a few inputs, and have code compare that with what it actually returns. A small check compares an actual value with an expected one. panic stops the program with a message when the comparison fails. That suits a check program, where a failure means a broken assumption. Ordinary invalid input should still return an error.

Predict and run

Does the check reach its success message?

Predict, then run Go
package main

import (
	"fmt"
)

func remaining(goal, read int) int {
	return goal - read
}

func main() {
	if remaining(30, 25) != 5 {
		panic("remaining: expected 5")
	}
	fmt.Println("Check passed")
}
Check your prediction
Text
Check passed

Temporarily change the expected 5 to 6 and read the failure.

What panic does

panic stops the program immediately. It prints panic: followed by your message, then a list of the function calls that were running, and the program ends with a failure status. Nothing after the failed check runs, so Check passed is never printed. For a check program that is exactly right: once an assumption is broken, carrying on would only produce confusing results.

Do not use panic for problems the user can cause, such as a mistyped line of input. Those are expected, and they deserve an error and a helpful message. A panic means "the code itself is wrong", which is what a failed check tells you.

Say what you got, not just that it failed

A message such as remaining: expected 5 tells you which check failed, but not what actually came back. Including the actual value saves a round of debugging, because you can often see the mistake from the number alone. Store the result once, compare it, and build the message with fmt.Sprintf, which formats like Printf but returns a string.

A message with got and want Go
package main

import (
	"fmt"
)

func remaining(goal, read int) int {
	if read >= goal {
		return 0
	}
	return goal - read
}

func main() {
	got := remaining(30, 45)
	want := 0
	if got != want {
		panic(fmt.Sprintf("remaining(30, 45) = %d, want %d", got, want))
	}
	fmt.Println("Check passed")
}
Text
Check passed

Remove the if read >= goal block and run it again. The panic message now reads remaining(30, 45) = -15, want 0, which points straight at the missing rule.

Choosing what to check

A handful of well-chosen checks catches most mistakes. Check an ordinary case, so you know the main path works. Check every boundary, and the values just outside it, because that is where comparisons go wrong. And check the cases the rules reject, such as reading more than the goal above. A check that only ever tries happy, middle-of-the-range values will pass even when the edges are broken.

Checks in real Go projects

Go has a built-in testing tool, go test, which runs functions in files ending in _test.go and reports every failure instead of stopping at the first. The editor on this site cannot run go test, so the course writes checks as small programs that panic instead. The reading log you download on Day 7 includes real tests too, so you can run both kinds locally.

Checks protect future changes

The best time to write a check is just after you have convinced yourself a function works. It costs a few lines then, and it pays off later: the next time you change the function, the check tells you at once whether you broke the old behavior. On Day 7 you will see this directly, when a single changed comparison makes a project check fail.

Practice

Exercise

Your turn

The starter defines remaining. Check it for a goal of 30 with 25 minutes read and with 30 minutes read, then print Checks passed.

Your working space Go
package main

func remaining(goal, read int) int {
	return goal - read
}

func main() {
	// Write your code here.
}
Hint 1: the idea

The exact goal should leave zero minutes.

Hint 2: a first step

Work out each expected value yourself, then compare each call with it.

Worked solution
Run the solution Go
package main

import (
	"fmt"
)

func remaining(goal, read int) int {
	return goal - read
}

func main() {
	if remaining(30, 25) != 5 || remaining(30, 30) != 0 {
		panic("unexpected remaining duration")
	}
	fmt.Println("Checks passed")
}
Text
Checks passed

Reaching the last line means both checks passed. Change remaining to return goal - read + 1 and the panic shows the check doing its job.

Was this article helpful?

Your response is saved on this device.