douzhuo8312 2015-02-12 11:28
浏览 133
已采纳

在Go中,如何在运行时获取测试环境?

I want to check if codes are running for the go test,so that I could make some configurations.

Is there any function to do that? Like:

runtime.IsBeingTested()

  • 写回答

1条回答 默认 最新

  • dougao1542 2015-02-12 11:49
    关注

    Just specify that you run a test in test's init. For example, in pkg.go:

    package pkg
    
    var isTesting = false
    
    // ...
    

    And in pkg_test.go:

    package pkg
    
    func init() {
        isTesting = true
    }
    
    // ...
    

    This technique can be used not just with bools but with any data or function. If you have some variable in your package (in your case, a configuration variable), you can just override it in init.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?