英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论