Skip to content

proposal: testing: allow settings flags for the duration of a test #75256

@nobletooth

Description

@nobletooth

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:

  1. Parallel tests may encounter race conditions and panic when calling the flag.Set function.
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions