cweill / gotests

Automatically generate Go test boilerplate from your source code.
Apache License 2.0
4.93k stars 346 forks source link

Verbose error comparison #92

Closed jdheyburn closed 2 years ago

jdheyburn commented 5 years ago

I've enhanced locally generated tests that expect an error to make a DeepEqual to the returned test, such as below:

if tt.wantErr != nil && !reflect.DeepEqual(err, tt.wantErr) {

Where tt.wantErr = errors.New("expected error").

I've found the template file that needs to be enhanced for this at below: https://github.com/cweill/gotests/blob/master/internal/render/templates/function.tmpl

If you think this is a suitable idea then I'm happy to implement the change.

I find this method of testing for the exact error allows the tester to know exactly what the error is, as opposed to any error altogether.

cweill commented 5 years ago

@jdheyburn: IIRC the best practice for writing go tests is not to compare the error messages exactly, because it tends to be brittle. The best thing for you to do is create a custom template and pass it via the --template_dir flag. That way you don't need a custom fork of gotests.

jdheyburn commented 5 years ago

Thanks for the tip. For reference I created an overriding function.tmpl file gist that can be found here: https://gist.github.com/jdheyburn/978e7b84dc9c197bcdd41afece2edab5

Swathi-L commented 2 years ago

Hi @cweill, Since --template_dir enhancement is done, can this issue be closed?

PS: Am a newbie looking to contribute in this repo, found some issues to be open although its implementation is already completed, so commenting on those.

On the same note, can we have a bot that checks on older open issues in this repo and forces the owner/ issue opener to recheck and take appropriate actions like close or modify labels of the issue.