Unit testing in Go

It's now time to explore unit testing in Go and make use of the MockDBLayer type that we built in the previous section. 

The first step for writing unit tests in Go is to create a new file in the same folder as the package you would like to test. The filename has to end with _test.go. In our case, since we seek to test the GetProducts() method in the rest package, we'll create a new file in the same folder and call it handler_test.go.

This file will only build and execute when you run unit tests, but not during regular builds. You may be wondering, how would I run unit tests in Go? The answer is simple—you utilize the go test command! Whenever you run go test, only the files that end with _test.go will build and run. 

Get Hands-On Full Stack Development with Go now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.