Testing with Go
Go has a built in test framework. It’s lightweight. Just go test
.
Simply:
- For the file of types, functions, etc. you want to test, create a file ending in
_test.go
- Import the standard library
testing
package. - In that file, write functions that start with
TestXXX
- Each function gets called with an argument called
t
. Thist
object is an instance oftesting.T
and is used to pass/fail your tests. - If
t.Error
ort.Fail
is called, the test is considered failed. - Run
go test
- you can also run benchmarking tests with the go tool, using the
--bench
flag - Due to the minimalistic nature of the go testing framework, manual setup/teardown is necessary and expected
Last modified: