-
Notifications
You must be signed in to change notification settings - Fork 605
Use t.Cleanup (added in go1.14) to call Finish
automatically
#407
Comments
I think this is a great idea. I would just want to play around and make sure it behaves well in things like subtests and TestMains. This change can be made without adding a new constructor. An unexported inferface could be declared: type cleanuper interface {
Cleanup(f func())
} Then in NewController you can just use a cast and call if the method is there: func NewController(t TestReporter) {
...
if c, ok := t.(cleanuper); ok {
c.Cleanup(func() { fmt.Println("I cleaned up something") })
}
...
} Also, this should not need build version guards then which is always an added benefit in my mind. |
Agree, I was considering the same thing. It does have a small behaviour change in the case that the user was never calling That does seem like an arcane use-case, so I'm +1 on reusing the existing API (less changes, and doesn't require changes to existing test code to benefit). |
Do we consider using gomock without calling This seems like a great change; I forget to call |
In go1.14 the testing package added a new cleanup method that is called at the end of test runs. By taping into this gomock can remove the need for users to call ctrl.Finish() if they pass a *testing.T into gomock.NewController(...). Fixes #407
Requested feature
Currently, any test that uses
gomock
often has this snippet at the top:The caller has to remember to
defer ctrl.Finish()
. This could be avoided if the gomock library supported go1.14'st.Cleanup
functionality.Why the feature is needed
It reduces likelihood of the caller forgetting to call
Finish
, and reduces noise in tests.Proposed solution
(Likely in a go1.14+-only file)
Finish
OK to call multiple times.testing.TB
and callsFinish
in a cleanup functionThe text was updated successfully, but these errors were encountered: