Testing Gin HTTP handlers
So far, we have learned how to design, build, and scale a distributed web application with Gin framework. In this chapter, we will cover how to integrate different types of tests to eliminate possible errors at release. We will start with unit testing.
Note
It's worth mentioning that you need to adopt a test-driven development (TDD) approach beforehand to get a head start in writing testable code.
To illustrate how to write a unit test for a Gin web application, you need to dive right into a basic example. Let's take the hello world
example covered in Chapter 2, Setting up API Endpoints. The router declaration and HTTP server setup have been extracted from the main
function to prepare for the tests, as illustrated in the following code snippet:
package main import ( Â Â Â "net/http" Â Â Â "github.com/gin-gonic/gin" ) func IndexHandler(c *gin.Context) { Â Â Â c.JSON(http.StatusOK...