You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I propose to add the following methods to testing.T (and others):
func (*testing.T) WithValue(key, valany) *testing.Tfunc (*testing.T) Value(keyany) any
Its use would be similar to context.Context, but the idea is that it allows one to pass additional configuration parameters to testing helpers which might be called somewhere deep. Because generally testing.T is passed to all helpers (so that they can call t.Helper() it is reasonable to use t also to pass some additional optional values.
Example: github.com/stretchr/testify package prints diffs when assertions fail. The issue is that there are different ways to print those diffs. For example, should one respect String methods on structs printed (might hide important information) or not (might make structs unreadable because it uses default state representation). To configure this github.com/stretchr/testify could have global settings, but that is fragile. Better would be that caller could call something like t = t.WithValue(testify.UseStringify, true) and then pass this t to its assert.Equal. Why is this better than just having assert.Equal accepting a parameter because assert.Equal might be called deep inside some other testing helper and caller wants to control that. So I propose that such setting could be passed through t itself.
Ugly workaround is to misuse environment variables and use t.Setenv but that disables parallelism and is simply ugly.
The text was updated successfully, but these errors were encountered:
Proposal Details
I propose to add the following methods to
testing.T
(and others):Its use would be similar to
context.Context
, but the idea is that it allows one to pass additional configuration parameters to testing helpers which might be called somewhere deep. Because generallytesting.T
is passed to all helpers (so that they can callt.Helper()
it is reasonable to uset
also to pass some additional optional values.Example:
github.com/stretchr/testify
package prints diffs when assertions fail. The issue is that there are different ways to print those diffs. For example, should one respectString
methods on structs printed (might hide important information) or not (might make structs unreadable because it uses default state representation). To configure thisgithub.com/stretchr/testify
could have global settings, but that is fragile. Better would be that caller could call something liket = t.WithValue(testify.UseStringify, true)
and then pass thist
to itsassert.Equal
. Why is this better than just havingassert.Equal
accepting a parameter becauseassert.Equal
might be called deep inside some other testing helper and caller wants to control that. So I propose that such setting could be passed throught
itself.Ugly workaround is to misuse environment variables and use
t.Setenv
but that disables parallelism and is simply ugly.The text was updated successfully, but these errors were encountered: