使用go test(golang)找到测试失败的文件名。

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

Finding file name of test that failed using go test (golang)

问题

当Go单元测试失败时,"go test"会显示如下消息:

--- FAIL: TestFillDeepStruct (0.00s)

但是,包含失败测试的文件的名称是什么(在这种情况下是Deep_test.go)?我找不到"go test"的选项来执行此操作。我有很多测试文件,使用grep命令查找测试函数名称很麻烦。

英文:

When a Go unit test fails "go test" will display a message like:

--- FAIL: TestFillDeepStruct (0.00s)

But what is the name of the file (in this case Deep_test.go) containing the failing test. I can find no option to "go test" to do this. I have many test files and it is tedious to grep for the test function name.

答案1

得分: 2

使用-v(详细)标志:

» go test -v example                                                                                                                                         === RUN   TestReturn
--- FAIL: TestReturn (0.00s)
    ex_test.go:10: /home/user/go/src/example
    ex_test.go:11: 期望值为42,实际值为43
FAIL
exit status 1
FAIL    example    0.002s

如果有一些测试不会显示错误行,您可以使用这个简单的bash函数:

function find_failed_tests(){
    FAIL_FILE="/tmp/failed.tmp"
    go test example | grep "^--- FAIL" | awk '{print $3}' > $FAIL_FILE
    grep -rf $FAIL_FILE  ~/go/src/$1
    rm -rf $FAIL_FILE
}

# 用法:
find_failed_tests example
英文:

Use -v (verbose) flag:

» go test -v example                                                                                                                                         === RUN   TestReturn
--- FAIL: TestReturn (0.00s)
	ex_test.go:10: /home/user/go/src/example
	ex_test.go:11: Expected 42, got 43
FAIL
exit status 1
FAIL	example	0.002s

In case there are some tests that will not show error line, you may use a this simple bash function:

function find_failed_tests(){
    FAIL_FILE="/tmp/failed.tmp"
    go test example | grep "^--- FAIL" | awk '{print $3}' > $FAIL_FILE
    grep -rf $FAIL_FILE  ~/go/src/$1
    rm -rf $FAIL_FILE
}

# Usage:
find_failed_tests example

huangapple
  • 本文由 发表于 2017年6月2日 13:00:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/44321118.html
匿名

发表评论

匿名网友

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

确定