-
Notifications
You must be signed in to change notification settings - Fork 17.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: testing: Error Cases Helper #41494
Comments
In cases where the user of a package is expected to be able to test what kind of error they received, we would normally recommend supporting In cases where the user is just expected to test So it's not immediately obvious to me that this is a feature we would want to add the testing package. Of course you can always write it yourself, as you are doing today. I'm only commenting on whether it should go into the standard library. |
I think I didn't frame this quite right: My proposal was less around Error-substring testing and more around error-case checking in the context of table-tests. I actually rather like the idea of using That being said, err := fmt.Errorf("%w addendum", SomeError)
fmt.Printf("IsErr: Case(nil, !nil): %t\n", errors.Is(nil, SomeError))
fmt.Printf("IsErr: Case(!nil, nil): %t\n", errors.Is(err, nil))
fmt.Printf("IsErr: Case(nil, nil): %t\n", errors.Is(nil, nil))
fmt.Printf("IsErr: Case(!nil, !nil match): %t\n", errors.Is(err, SomeError))
So I'm satisfied with using |
I would like to propose adding a helper method to the standard library (in the
testing
pkg, probably) for testing whether an error contains a substring in the context of unit-testing. It's pattern I use frequently, and tend to copy around between libraries.When testing whether or not an error is expected based on an expected substring, there are four cases to check.
{err, no err} X {expErr, no expErr}
. This is a helper to check all four cases.Generally, I use such a helper in table tests:
Here's the sort of method method I end up writing:
The text was updated successfully, but these errors were encountered: