duannengling4705 2019-03-01 10:21
浏览 51
已采纳

如何轻松找出哪些测试失败

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.

  • 写回答

1条回答 默认 最新

  • dpfw3607 2019-03-01 10:49
    关注

    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\""
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 如何通过代码传输视频到亚马逊平台
  • ¥15 php查询mysql数据库并显示至下拉列表中
  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信
  • ¥30 Python调用dll文件输出Nan重置dll状态
  • ¥15 浮动div的高度控制问题。
  • ¥66 换电脑后应用程序报错
  • ¥50 array数据同步问题