Proposal Details
When tests require a certain flag value to work, flag.Set(...) is often chosen as the go-to way.
An example:
import "testing"
func TestSomeFeature(t *testing.T) {
err := flag.Set("enable_some_feature", "true")
if err != nil {
t.Fatal(err)
}
// Trigger the feature and make assertions.
}
There are some downsides to this approach, most importantly these:
- Parallel tests may encounter race conditions and panic when calling the
flag.Set function.
- The flag value is set throughout the test process, i.e. affects other tests are well, while it shouldn't.
I propose having a func (t* testing.T) SetFlag(name, value string) function that makes the flag set goroutine-safe and only affect a single test scope, i.e. the flag is reset to default value when t is done.