Skip to content

proposal: testing: testing.T.IsNil #70719

@glossd

Description

@glossd

Proposal Details

Go Wiki explains that assertions are bad because they either

  1. stop the tests early
  2. 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)
    }
    ...
}
  1. If the error happens there's no point in going further
  2. 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()
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions