Ashwin
Ashwin

Reputation: 1013

Is there a way to set test timeout value dynamically

Currently I am using the following command for running my test with timeout value given during test call.

go test myModule -run TestSanity -v --race -timeout 10h

Is there a way in Golang testing module to set it during program execution. Something like,

func TestMain(m *testing.M) {
    // customTimeout = "10h"
    // m.Timeout(customTimeout)   <--- Something like this
    code := m.Run()
    os.Exit(code)
}

Upvotes: 6

Views: 4651

Answers (1)

svsd
svsd

Reputation: 1869

You could write your own function to do that:

func panicOnTimeout(d time.Duration) {
    <-time.After(d)
    panic("Test timed out")
}

func TestMain(m *testing.M) {
    go panicOnTimeout(10 * time.Hour) // custom timeout

    code := m.Run()
    os.Exit(code)
}

This should simulate what go test -timeout does. Be sure to pass -timeout 0 to prevent the default test timeout from triggering though.

Upvotes: 6

Related Questions