You can use go test command for testing go packages very easily.


file names

For every go source file that you have ( this example main.go) you will need a <name>_test.go file that contains the tests (main_test.go for example).


function names

The functions within the <name>_test.go file will need to start with the keyword Test and then have the suffix of a function that you wish to test.

To test a function called AddNumbers in main.go, we create a function called TestAddNumbers in main_test.go.


Imports

We can use the testing library to help us out with testing go files, its in the go standard library has a nice Error() function, that we use to format output.

An example of the output it can generate is below.

--- FAIL: TestAddNumbers (0.00 seconds)
main_test.go:13: For Adding 1 + 2 expected 3 got 4
FAIL
exit status 1
FAIL	_/home/dannyla/code/github/go-examples	0.002s


Example

main.go

main_test.go