In Go,how to get test environment at run time?

前端 未结 1 1949
独厮守ぢ
独厮守ぢ 2021-01-28 16:21

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.IsBei

相关标签:
1条回答
  • 2021-01-28 17:00

    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.

    0 讨论(0)
提交回复
热议问题