How do you print in a Go test using the “testing” package?

后端 未结 6 1242
既然无缘
既然无缘 2020-12-12 12:07

I\'m running a test in Go with a statement to print something (i.e. for debugging of tests) but it\'s not printing anything.

func TestPrintSomething(t *testi         


        
6条回答
  •  盖世英雄少女心
    2020-12-12 13:00

    The *_test.go file is a Go source like the others, you can initialize a new logger every time if you need to dump complex data structure, here an example:

    // initZapLog is delegated to initialize a new 'log manager'
    func initZapLog() *zap.Logger {
        config := zap.NewDevelopmentConfig()
        config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
        config.EncoderConfig.TimeKey = "timestamp"
        config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
        logger, _ := config.Build()
        return logger
    }
    

    Then, every time, in every test:

    func TestCreateDB(t *testing.T) {
        loggerMgr := initZapLog()
        // Make logger avaible everywhere
        zap.ReplaceGlobals(loggerMgr)
        defer loggerMgr.Sync() // flushes buffer, if any
        logger := loggerMgr.Sugar()
        logger.Debug("START")
        conf := initConf()
        /* Your test here
        if false {
            t.Fail()
        }*/
    }
    

提交回复
热议问题