英文:
What is the use of test mode in Gin
问题
我已经查看了文档,但它没有解释如何使用 gin 的测试模式。
gin.SetMode(gin.TestMode)
这个测试模式有什么作用?在我的测试中设置和不设置这个模式时,我没有看到任何区别。
英文:
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.
答案1
得分: 2
gin.DebugMode
标志用于控制gin.IsDebugging()
的输出,它会添加一些额外的日志输出并将HTML渲染器更改为调试结构HTMLDebug
。
gin.TestMode
在Gin自己的单元测试中用于切换调试模式(和额外的日志记录)的开启和关闭,以及调试HTML渲染器的使用。
除此之外,它没有其他用途(来源)。
然而,该标志可以通过环境变量GIN_MODE=test
进行控制。由于Mode()
是公开的,您可以在应用程序代码中使用它,例如声明测试路由。如果您计划运行端到端测试套件或其他集成测试,这可能会有一些优点:
r := gin.New()
if gin.Mode() == gin.TestMode {
r.GET("/test", func(c *gin.Context) {
c.String(418, "I don't exist in production")
})
}
英文:
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")
})
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论