Make silent debug info on tests (#1765)

* make silent log on tests

* fix coverage: check end-of-line at the end of debug msg
This commit is contained in:
Dmitry Kutakov 2019-02-18 05:10:45 +03:00 committed by 田欧
parent a768f064d5
commit 31bbb10f34
5 changed files with 27 additions and 19 deletions

View File

@ -39,7 +39,7 @@ func TestDebugPrint(t *testing.T) {
SetMode(TestMode) SetMode(TestMode)
debugPrint("DEBUG this!") debugPrint("DEBUG this!")
SetMode(DebugMode) SetMode(DebugMode)
debugPrint("these are %d %s\n", 2, "error messages") debugPrint("these are %d %s", 2, "error messages")
SetMode(TestMode) SetMode(TestMode)
}) })
assert.Equal(t, "[GIN-debug] these are 2 error messages\n", re) assert.Equal(t, "[GIN-debug] these are 2 error messages\n", re)

View File

@ -24,7 +24,9 @@ func TestBindWith(t *testing.T) {
Foo string `form:"foo"` Foo string `form:"foo"`
Bar string `form:"bar"` Bar string `form:"bar"`
} }
assert.NoError(t, c.BindWith(&obj, binding.Form)) captureOutput(t, func() {
assert.NoError(t, c.BindWith(&obj, binding.Form))
})
assert.Equal(t, "foo", obj.Bar) assert.Equal(t, "foo", obj.Bar)
assert.Equal(t, "bar", obj.Foo) assert.Equal(t, "bar", obj.Foo)
assert.Equal(t, 0, w.Body.Len()) assert.Equal(t, 0, w.Body.Len())

View File

@ -27,18 +27,23 @@ func formatAsDate(t time.Time) string {
func setupHTMLFiles(t *testing.T, mode string, tls bool, loadMethod func(*Engine)) *httptest.Server { func setupHTMLFiles(t *testing.T, mode string, tls bool, loadMethod func(*Engine)) *httptest.Server {
SetMode(mode) SetMode(mode)
router := New() defer SetMode(TestMode)
router.Delims("{[{", "}]}")
router.SetFuncMap(template.FuncMap{ var router *Engine
"formatAsDate": formatAsDate, captureOutput(t, func() {
}) router = New()
loadMethod(router) router.Delims("{[{", "}]}")
router.GET("/test", func(c *Context) { router.SetFuncMap(template.FuncMap{
c.HTML(http.StatusOK, "hello.tmpl", map[string]string{"name": "world"}) "formatAsDate": formatAsDate,
}) })
router.GET("/raw", func(c *Context) { loadMethod(router)
c.HTML(http.StatusOK, "raw.tmpl", map[string]interface{}{ router.GET("/test", func(c *Context) {
"now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC), c.HTML(http.StatusOK, "hello.tmpl", map[string]string{"name": "world"})
})
router.GET("/raw", func(c *Context) {
c.HTML(http.StatusOK, "raw.tmpl", map[string]interface{}{
"now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),
})
}) })
}) })

View File

@ -287,7 +287,7 @@ var githubAPI = []route{
func TestShouldBindUri(t *testing.T) { func TestShouldBindUri(t *testing.T) {
DefaultWriter = os.Stdout DefaultWriter = os.Stdout
router := Default() router := New()
type Person struct { type Person struct {
Name string `uri:"name" binding:"required"` Name string `uri:"name" binding:"required"`
@ -309,7 +309,7 @@ func TestShouldBindUri(t *testing.T) {
func TestBindUri(t *testing.T) { func TestBindUri(t *testing.T) {
DefaultWriter = os.Stdout DefaultWriter = os.Stdout
router := Default() router := New()
type Person struct { type Person struct {
Name string `uri:"name" binding:"required"` Name string `uri:"name" binding:"required"`
@ -331,7 +331,7 @@ func TestBindUri(t *testing.T) {
func TestBindUriError(t *testing.T) { func TestBindUriError(t *testing.T) {
DefaultWriter = os.Stdout DefaultWriter = os.Stdout
router := Default() router := New()
type Member struct { type Member struct {
Number string `uri:"num" binding:"required,uuid"` Number string `uri:"num" binding:"required,uuid"`
@ -361,7 +361,7 @@ func githubConfigRouter(router *Engine) {
func TestGithubAPI(t *testing.T) { func TestGithubAPI(t *testing.T) {
DefaultWriter = os.Stdout DefaultWriter = os.Stdout
router := Default() router := New()
githubConfigRouter(router) githubConfigRouter(router)
for _, route := range githubAPI { for _, route := range githubAPI {
@ -436,7 +436,7 @@ func BenchmarkParallelGithub(b *testing.B) {
func BenchmarkParallelGithubDefault(b *testing.B) { func BenchmarkParallelGithubDefault(b *testing.B) {
DefaultWriter = os.Stdout DefaultWriter = os.Stdout
router := Default() router := New()
githubConfigRouter(router) githubConfigRouter(router)
req, _ := http.NewRequest("POST", "/repos/manucorporat/sse/git/blobs", nil) req, _ := http.NewRequest("POST", "/repos/manucorporat/sse/git/blobs", nil)

View File

@ -43,6 +43,7 @@ func TestPanicInHandler(t *testing.T) {
assert.Equal(t, http.StatusInternalServerError, w.Code) assert.Equal(t, http.StatusInternalServerError, w.Code)
assert.Contains(t, buffer.String(), "GET /recovery") assert.Contains(t, buffer.String(), "GET /recovery")
SetMode(TestMode)
} }
// TestPanicWithAbort assert that panic has been recovered even if context.Abort was used. // TestPanicWithAbort assert that panic has been recovered even if context.Abort was used.