Description
I recently ran into an issue where we accidentally passed two different errors to require.NoError
:
require.NoError(t, err, TestFunc(), "TestFunc failed")
It was incorrectly testing the previous err
instead of the output of calling TestFunc()
. I've noticed that testify
is inconsistent between whether it checks the arguments before checking if the condition failed, or whether it's only checked if the condition fails.
I've written a couple of simple tests to show the difference:
func TestRequireErr(t *testing.T) {
var actualErr error
wrongErr := errors.New("err1")
require.Error(t, wrongErr, actualErr, "Message")
}
func TestRequireNoErr(t *testing.T) {
var wrongErr error
actualErr := errors.New("err1")
require.NoError(t, wrongErr, actualErr, "Message")
}
In both cases, we're passing two errors -- the second is what we actually want to compare. However, the first error we pass would pass the assertion, while the second is the one we intended, and would cause the assertion to fail.
The output of TestRequireErr
:
=== RUN TestRequireErr
--- FAIL: TestRequireErr (0.00s)
panic: interface conversion: interface is nil, not string [recovered]
panic: interface conversion: interface is nil, not string
The output of TestRequireNoErr
:
=== RUN TestRequireNoErr
--- PASS: TestRequireNoErr (0.00s)
I think both assertions should be consistent -- and ideally, they both ensure that the msg
is actually a string regardless of the condition, since the compiler can't enforce this.
Activity