有没有办法动态设置测试超时值

Ash*_*win 4 unit-testing go

目前,我正在使用以下命令运行我的测试,并在测试调用期间给出超时值。

去测试 myModule -run TestSanity -v --race -timeout 10h

Golang 测试模块中有没有办法在程序执行期间设置它。就像是,

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

svs*_*vsd 5

你可以编写自己的函数来做到这一点:

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)
}
Run Code Online (Sandbox Code Playgroud)

这应该模拟什么go test -timeout。一定要通过-timeout 0以防止触发默认测试超时。