gotestyourself / gotestyourself/gotest.tools
Better approach for error/non-error in table tests
- Dominant language
- Go
- Stars
- 576
- Forks
- 54
- Avg merge
- 6d 3h
- Merged PRs (30d)
- 2
Description
I notice I keep running into this, so let me open an issue;
When writing table-tests, I usually combine "happy" and "unhappy" tests, which means that for some tests, I'd be checking for an error, and for the happy tests, check that no error occurs.
For example;
```go
func TestErrorOrNot(t *testing.T) {
doSomething := func(withError bool) error {
if withError {
return errors.New("something went wrong")
}
return nil
}
for _, test := range []struct {
withErr bool
errString string
}{{withErr: true, errString: "something went wrong"}, {}} {
err := doSomething(test.withErr)
if test.errString != "" {
assert.Error(t, err, test.errString)
}
assert.NilError(t, err)
}
}
```
I'd like to get rid of the `if test.errString != "" {` in these tests;
Initially thought; let's use `assert.Error(t, err, test.errString)`, which fails, because a `nil` error is not the same as an empty error-string. Using `assert.ErrorContains(t, err, test.errString)` has the same issue, because `""` must be included in the error (which isn't the case if there's no error at all)
Wondering if there's a better approach, or if a new utility should be added that accepts "empty error string" as "no error is produced".
@vdemeester @dnephin
Contributor guide
Assessment
This issue has not been assessed yet.