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

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


去测试 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)

}


函数式编程
浏览 80回答 1
1回答

牧羊人nacy

您可以编写自己的函数来执行此操作:func panicOnTimeout(d time.Duration) {&nbsp; &nbsp; <-time.After(d)&nbsp; &nbsp; panic("Test timed out")}func TestMain(m *testing.M) {&nbsp; &nbsp; go panicOnTimeout(10 * time.Hour) // custom timeout&nbsp; &nbsp; code := m.Run()&nbsp; &nbsp; os.Exit(code)}这应该模拟做什么go test -timeout。一定要通过-timeout 0以防止触发默认测试超时。
打开App,查看更多内容
随时随地看视频慕课网APP