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

后端 未结 6 1232
既然无缘
既然无缘 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 12:48

    t.Log and t.Logf do print out in your test but can often be missed as it prints on the same line as your test. What I do is Log them in a way that makes them stand out, ie

    t.Run("FindIntercomUserAndReturnID should find an intercom user", func(t *testing.T) {
    
        id, err := ic.FindIntercomUserAndReturnID("test3@test.com")
        assert.Nil(t, err)
        assert.NotNil(t, id)
    
        t.Logf("\n\nid: %v\n\n", *id)
    })
    

    which prints it to the terminal as,

    === RUN   TestIntercom
    === RUN   TestIntercom/FindIntercomUserAndReturnID_should_find_an_intercom_user
        TestIntercom/FindIntercomUserAndReturnID_should_find_an_intercom_user: intercom_test.go:34:
    
            id: 5ea8caed05a4862c0d712008
    
    --- PASS: TestIntercom (1.45s)
        --- PASS: TestIntercom/FindIntercomUserAndReturnID_should_find_an_intercom_user (1.45s)
    PASS
    ok      github.com/RuNpiXelruN/third-party-delete-service   1.470s
    

提交回复
热议问题