Andrew W. Phillips
Andrew W. Phillips

Reputation: 3601

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

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.

Upvotes: 3

Views: 2307

Answers (1)

syntagma
syntagma

Reputation: 24324

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

Upvotes: 2

Related Questions