Testing with Go

Go has a built in test framework. It’s lightweight. Just go test.


  • 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. This t object is an instance of testing.T and is used to pass/fail your tests.
  • If t.Error or t.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: October 24, 2021
© 2024