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 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题