Home > database >  go assertion utility functions behave like non-blocking operation
go assertion utility functions behave like non-blocking operation

Time:07-04

enter image description here I expected that each assertion is a blocking operation and test would stop at a point as soon as it detects a failure. But it didn't happened.

How go tool can smell upcoming failures and inform me ? AFAIK, assertion's methods are all blocking, it shouldn't have been happened. Am I missing something ?

❯ 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

CodePudding user response:

Assuming you're talking about the assert functions in stretchr/testify (https://pkg.go.dev/github.com/stretchr/testify/assert), note that assert.True simply verifies whether something is true and if not, calls t.Errorf. As the testing documentation says, this:

marks the function as having failed but continues execution.

This differs from t.FailNow, which:

marks the function as having failed and stops its execution by calling runtime.Goexit (which then runs all deferred calls in the current goroutine). Execution will continue at the next test or benchmark. FailNow must be called from the goroutine running the test or benchmark function, not from other goroutines created during the test. Calling FailNow does not stop those other goroutines.

(In the testify package, there's a require series of functions that do the same thing as assert but use t.FailNow when they fail. This lets you invoke require.True to stop further tests.)

As an aside, the word blocking is the wrong one to use here. Each test run by the testing package is in a goroutine and finishing (or aborting) the entire test is done by finishing (or exiting early from) the goroutine, but the functions you mention are all synchronous ("blocking") function calls.

CodePudding user response:

In order to have a set of utility functions which stop the current test at the first failing site, use the other companion package : https://pkg.go.dev/github.com/stretchr/testify/require

require.True(t, reflect.DeepEqual("A", "A"))
require.True(t, reflect.DeepEqual("B", "B"))
...

as a side note, the stretchr/testify module also provides the .Equal(...) and .EqualValues(...) functions :

assert.Equal(t, "A", "A")
require.Equal(t, "B", "B")

expected := "A"
got := executeMyFunc()
assert.Equal(t, expected, got)
require.Equal(t, expected, got)

# or
assert.EqualValues(t, expected, got)
require.EqualValues(t, expected, got)
  • Related