Skip to content

proposal: testing: ability to tell if test is running in parallel #70017

Open
@jim-minter

Description

@jim-minter

Proposal Details

Unit tests can indicate their ability to be run in parallel by making the opt-in call t.Parallel().

However sometimes a test might opt in but then mistakenly change some global state which could race with something else. In this case, the parallel test should actually be a serial one. In a large codebase it can be tricky to protect against this happening.

For example,

func TestShouldNotBeParallel(t *testing.T) {
    t.Parallel()
    // ...
    changeGlobalState(t) // a mistake, given above t.Parallel()
    // ...
}

func changeGlobalState(t *testing.T) {
    // proposal: could we check `t` here to determine that we're running in parallel when we shouldn't be, and error out?
    if t.IsRunningInParallel() {
        t.Fatal("changeGlobalState called from test with t.Parallel()")
    }

    // ...
}

The proposal here is a function like t.IsRunningInParallel() that could return true in the case that the current test is running in a parallel context. I don't think there's a way to check this currently?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions