Testing in Go

Knowledge Videos

Short animated explainers, organised by subject.

Testing in Go

A test is a small program that checks your code. _test.go files, Test functions, go test with PASS and FAIL, and table tests with many cases.

Transcript

A test is a small program that checks that your code gives the right answer. Instead of trying things by hand again and again, you write the check once, and run it any time.

Tests live in a file whose name ends in underscore test dot go. A test function starts with the word Test. It calls your function, and compares the answer with what you expect.

Run all the tests with go test. A passing test prints pass. A failing test prints what it got, what it wanted, and the line.

A table test puts many inputs and expected answers in one slice, and loops over them. To add a new case, you add one line.

Remember: a test checks your code for you. Name the file with underscore test, start the function with Test, and run go test.

More in this series