dongqiuqiu4736 2016-09-06 21:11
浏览 48
已采纳

Golang测试脚本可以发出警告而不是错误吗?

I have a set of tests that may not pass due to external 3rd party issues. I don't want the test to fail when this condition occurs but would like to be made aware.

Issuing a t.Errorf() is not idea because it will stop all subsequent tests. Is there some kind of "Warning" I can trigger that the test script would post and then continue with the remainder of the tests?

  • 写回答

1条回答 默认 最新

  • doufang2023 2016-09-06 23:28
    关注

    The go test tool is like the compiler. To the compiler something either compiles or doesn't, there are no warnings. I think the closest thing you're going to get is to use t.Skip. It will stop execution of the current test but does not mark it as failed. You will not see anything in the output of go test however so you have to use go test -v.

    Here's an example package that uses t.Skipf if the addExternal function fails.

    package app
    
    import "testing"
    
    func add(a, b int) int {
        return a + b
    }
    
    func addExternal(a, b int) int {
        return 4
    }
    
    func divide(a, b int) int {
        return a / b
    }
    
    func TestThing(t *testing.T) {
        got := add(1, 2)
        want := 3
        if got != want {
            t.Errorf("add(1, 2) = %d, want %d", got, want)
        }
    }
    
    func TestExternalThing(t *testing.T) {
        got := addExternal(3, 4)
        want := 7
        if got != want {
            t.Skipf("addExternal(3, 4) = %d, want %d", got, want)
        }
    }
    
    func TestAnotherThing(t *testing.T) {
        got := divide(6, 3)
        want := 2
        if got != want {
            t.Errorf("divide(6, 3) = %d, want %d", got, want)
        }
    }
    

    And here's the output from running that. Note the return status is 0 and the package is considered to have passed

    $ go test -v
    === RUN   TestThing
    --- PASS: TestThing (0.00s)
    === RUN   TestExternalThing
    --- SKIP: TestExternalThing (0.00s)
            app_test.go:29: addExternal(3, 4) = 4, want 7
    === RUN   TestAnotherThing
    --- PASS: TestAnotherThing (0.00s)
    PASS
    ok      github.com/jcbwlkr/app  0.006s
    $ echo $?
    0
    

    Note though that if I change the t.Skipf to t.Errorf or t.Fatalf I get this output

    $ go test -v
    === RUN   TestThing
    --- PASS: TestThing (0.00s)
    === RUN   TestExternalThing
    --- FAIL: TestExternalThing (0.00s)
            app_test.go:29: addExternal(3, 4) = 4, want 7
    === RUN   TestAnotherThing
    --- PASS: TestAnotherThing (0.00s)
    FAIL
    exit status 1
    FAIL    github.com/jcbwlkr/app  0.005s
    $ echo $?
    1
    

    The other tests in the package are still ran. If I was testing multiple packages such as with go test -v ./... I believe they would also still be ran.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序