你正在使用的Go版本是什么( go version
)?
$ go version
go version go1.13.6 linux/amd64
这个问题在最新版本中是否重现?
是的
你正在使用什么操作系统和处理器架构( go env
)?
go env
输出
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/vimalkum/.cache/go-build"
GOENV="/home/vimalkum/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/vimalkum/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build339334134=/tmp/go-build -gno-record-gcc-switches"
你做了什么?
https://play.golang.org/p/525R1YDp8sB
编写一个调用 t.Log
或 t.Logf
的测试用例。当测试失败时,这些日志的输出会生成。如果我正在使用CI进行测试,如果能够将测试日志时间戳与其他由CI捕获的日志相关联,那么我的工作会容易得多。
你期望看到什么?
t.Log
语句应该产生时间戳。
你看到了什么?
没有时间戳
2020/01/20 21:15:05 GetValue called
--- FAIL: TestGetValue (0.00s)
main_test.go:9: this is log statement
main_test.go:11: this is error log statement
main_test.go:12: expected 100
FAIL
exit status 1
FAIL github.com/vimalk78/test-testing 0.001s
6条答案
按热度按时间7y4bm7vi1#
我认为这不是大多数人想要的。如果你需要,你可以自己在你的
t.Log
通话中添加时间戳。2nbm6dog2#
就其价值而言,我认为这也是一个有用的功能,原因与@vimalk78提供的原因相同。如果我们有一个
t.SetPrefix
函数(类似于log
包实现的方式),那将很酷。pb3skfrl3#
同意,一直使用时间戳,并宁愿不要单独的日志包/在每个日志语句中包含一个TS片段。
vojdkbi04#
I personally think this would be nice by default; most of the output from
go test
already includes relative timestamps, like how long a test ran for, or how long the entire package took to test. Here's an example of how relative timestamps on logs could look like:cmd/go's TestScript does something similar; each section of a test script has a comment, and the output includes how long that section took to run (cc @bcmills):
When I write long tests in Go, such as integration tests, I often want similar timestamps or durations to be able to tell what's taking most of the time. It's also useful with
go test -v -run OneTest
, so that the logs being streamed out can give a better idea of how the time is being spent.Like @ianlancetaylor says, I can always add timestamps or elapsed durations to log calls myself, though at this point I've done that in a handful of different places, and I don't see a reason why we shouldn't do it by default :) I reckon I would generally find this useful for any test which takes more than a tens of milliseconds and prints a few logs.
Worth noting that, with
go test -json
, log lines get printed as "output" actions with a timestamp, but that's not very friendly for humans.kgqe7b3p5#
为了记录,我目前的解决方法是:
带有
go test -v
的日志显示如下:wfauudbj6#
就其价值而言,我在CL 405714中的
cmd/go
脚本测试中添加了绝对时间戳。然而,我只在运行开始时添加了一个时间戳:开始时的时间戳与测试的运行时间相结合,给出了一个有界的窗口,其中测试的事件发生,这是我真正关心的目的(诊断由非一致外部服务中的错误引起的测试失败)。