-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Closed as not planned
Labels
Milestone
Description
Proposal Details
Go Wiki explains that assertions are bad because they either
- stop the tests early
- omit useful information.
Often, in tests, we need to check the errors on nil. If it's not, it indicates a function failure.
This is a classic way of handling errors in tests.
func TestDo(t *testing.T) {
out, err := Do()
if err != nil {
t.Fatalf("Do error: %s", err)
}
...
}- If the error happens there's no point in going further
- Errors carry a description of what went wrong.
I propose to add the IsNil method to the common testing type.
It would result in code like this:
func TestDo(t *testing.T) {
out, err := Do()
t.IsNil(err)
...
}I would also like to point out that the nil or zero value doesn't carry any information and perhaps IsZero method should be added as well.
However, to prevent any possible abuse of assertions, we could limit the parameter type to the error interface.
E.g.
func (c *common) IsErrorNil(err error) {
if !reflect.ValueOf(err).IsNil() {
c.checkFuzzFn("IsErrorNil")
c.log(fmt.Sprintf("error: %s", err))
c.FailNow()
}
}Reactions are currently unavailable