Search code examples
unit-testinggotesting

How to easily find out which tests fail


I test my code with go test ./... -v -short.

Unfortunately, -v only prints out each test as it happens, but does not leave a summary of the results at the bottom like in Java. This means that if any test failed somewhere at the top, I have to scroll up and look for the word FAIL or search for it in a text editor.

The -failfast flag isn't helping either because some of my tests still get printed after the first test failure for some reason.

I don't really care if tests get run after the initial test failure. I just want to be able to easily tell if any test failed, preferably in just one place (e.g. a summary of how many tests passed or failed, or by seeing a flag if all tests passed or not).

Is there a way to easily tell if there was a test failure because I don't want to accidentally continue coding if I still have test failures.

I'm on Windows 10 64-bit.

UPDATE: Many thanks to @icza for the findstr tip. I later realized that I also wanted to see the error descriptions along with the test failures, but did not want to run go test twice. This is what I came up with for CMD (does not work on Powershell):

go test ./... -v -short > test-results.txt & findstr "FAIL _test" test-results.txt

Now findstr should report test failures as well as error descriptions. And if you want to see the full test results, simply open test-results.txt.


Solution

  • Failing tests are indicated with FAIL in the output. So all you have to do is filter the output for that word.

    On Unix systems:

    go test ./... |grep FAIL
    

    On Windows:

    go test ./... |findstr FAIL
    

    Note that this is purely text processing, it doesn't know anything about go tests and their results. This means you might get "false positives" if a test outputs the word FAIL even if it succeeds. But in practice, this pretty much does the job you want.

    A more sophisticated and more accurate way to achieve this would be to pass -json flag to go test, so it generates JSON output, which you can process with a program (e.g. written in Go itself). Failing tests are indicated with a JSON object having an "Action":"fail" field, e.g.

    {"Time":"2019-03-01T12:06:21.108544405+01:00","Action":"fail",
     "Package":"some/package","Test":"TestSomething","Elapsed":0.01}
    

    And even if you don't want to write a program for this, filtering the JSON output leaves less chance for false positive (filtering for "Action":"fail"):

    Unix:

    go test ./... -json |grep '"Action":"fail"'
    

    Windows:

    go test ./... -json |findstr /C:"\"Action\":\"fail\""