Home > database >  What is the use of test mode in Gin
What is the use of test mode in Gin

Time:03-17

I've checked the documentation but it does not explain the use of setting test mode for gin

gin.SetMode(gin.TestMode)

What is this test mode provided for? I do not see any difference when setting & not setting this mode in my tests.

CodePudding user response:

The flag gin.DebugMode is used to control the output of gin.IsDebugging(), which adds some additional log output and changes the HTML renderer to the debug struct HTMLDebug.

The gin.TestMode is used in Gin's own unit tests to toggle the debug mode (and the additional logging) on and off, and the usage of the debug HTML renderer.

Other than that, it doesn't have other uses (source).

However, the flag can be controlled with the environment variable GIN_MODE=test. Then, since Mode() is exported, you can use it in application code to, for example, declare testing routes. This might have some merit if you plan to run an E2E test suite, or some other integration test:

    r := gin.New()
    if gin.Mode() == gin.TestMode {
        r.GET("/test", func(c *gin.Context) {
            c.String(418, "I don't exist in production")
        })
    }
  • Related