golang之单元测试通过viper读取配置报错的问题
①在项目的任意目录下创建 xxx_test.go文件,我这里是 gorm_test.go文件
内容如下:
package product
import (
"common"
"model"
"testing"
)
func TestGormOperation(t *testing.T) {
//create
product :=model.Product{}
product.Name = "超级大的滑雪场冬季必备"
product.Kind = "旅游"
common.Db.Create(product)
}
一运行报错了,原来是common下通过viper读取配置文件,路劲错了,主要是因为在 test下,viper读取的配置文件的相对路径不是项目的根目录,而是当前目录,所系需要根据实际情况通过 ../../ 访问配置文件
我这里:
//configPathW = "./config/"
configPathW = "../../config/"
