为什么在单元测试中使用`–cover`参数时,`runtime.caller(0)`返回的路径不同?

huangapple go评论83阅读模式
英文:

Why runtime.caller(0) is giving different path with '--cover' argument in unit testing

问题

当尝试执行一个没有任何参数的单元测试函数时,它会给出以下输出路径:

C:/Program Files (x86)/Go1.8\bin\go.exe" test -v gclassec/skyline/loggers -run ^TestCreateLogFilesPath$

文件路径:=== C:/GerritNew/goclassec/src/gclassec/skyline/loggers/logs.go

但是当我尝试使用任何参数,比如 --cover,文件路径的值会改变如下:

"C:/Program Files (x86)/Go1.8\bin\go.exe" test -v --cover gclassec/skyline/loggers -run ^TestCreateLogFilesPath$

文件路径:=== gclassec/skyline/loggers/_test/_obj_test/logs.go

我正在使用 runtime.caller(0) 来获取文件路径。

英文:

When trying to execute a unit test function without any argument it give following output path

C:/Program Files (x86)/Go1.8\bin\go.exe" test -v gclassec/skyline/loggers -run ^TestCreateLogFilesPath$

filePath:=== C:/GerritNew/goclassec/src/gclassec/skyline/loggers/logs.go

But when I try to run it with any argument such as --cover the file path value changes as below:

"C:/Program Files (x86)/Go1.8\bin\go.exe" test -v --cover gclassec/skyline/loggers -run ^TestCreateLogFilesPath$

filePath:=== gclassec/skyline/loggers/_test/_obj_test/logs.go

I am using runtime.caller(0) to get file path.

答案1

得分: 0

通过使用os.Getwd()和runtime.caller,并使用一个变量来区分测试和正常运行,它可以工作。

英文:

It worked by using os.Getwd() and runtime.caller with a variable to diffrentiate between testing and normal running.

huangapple
  • 本文由 发表于 2017年7月3日 13:42:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/44878486.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定