代码之家  ›  专栏  ›  技术社区  ›  nevzatseferoglu

go断言实用程序函数的行为类似于非阻塞操作

  •  1
  • nevzatseferoglu  · 技术社区  · 2 年前

    enter image description here 我希望每个断言都是一个阻塞操作,测试一旦检测到故障就会停止。但它没有发生。

    go工具如何嗅到即将发生的故障并通知我?哎呀,断言的方法都被阻塞了,这不应该发生。我错过什么了吗?

    ❯ go test -run Test_Assert . -v
    === RUN   Test_Assert
        config_test.go:136:
                Error Trace:    config_test.go:136
                Error:          Should be true
                Test:           Test_Assert
        config_test.go:138:
                Error Trace:    config_test.go:138
                Error:          Should be true
                Test:           Test_Assert
    --- FAIL: Test_Assert (0.00s)
    FAIL
    FAIL    github.com/hazelcast/hazelcast-go-client    0.178s
    FAIL
    
    1 回复  |  直到 2 年前
        1
  •  1
  •   torek    2 年前

    假设你说的是 assert stretchr/authentic中的函数( https://pkg.go.dev/github.com/stretchr/testify/assert ),请注意 assert.True 只需验证某事是否属实,如果不是,则调用 t.Errorf . 像 the testing documentation says ,这是:

    将函数标记为已失败,但继续执行。

    这与 t.FailNow ,其中:

    将函数标记为已失败,并通过调用运行时停止其执行。Goexit(然后运行当前goroutine中的所有延迟调用)。执行将在下一个测试或基准测试中继续。FailNow必须从运行测试或基准测试函数的goroutine调用,而不是从测试期间创建的其他goroutine调用。呼叫FailNow并不能阻止其他的社交活动。

    (在证明文件包中,有一个 require 一系列功能与 明确肯定 但是使用 t、 失败现在 当他们失败时。这允许您调用 require.True 停止进一步测试。)

    顺便说一句,这个词 舞台调度 这里用错了。每个测试由 测试 包位于goroutine中,完成(或中止)整个测试是通过完成(或提前退出)goroutine来完成的,但您提到的功能都是同步的(“阻塞”) 函数调用 .