具有外部包依赖性的 Golang 测试未通过 Gitlab Ci,但通过本地

我有测试:


package misc


var (

    Trace   *log.Logger

    Info    *log.Logger

    Warning *log.Logger

    Error   *log.Logger

)


func TestFTPGetFilesFromServer(t *testing.T) {

    misc.InitLogger(4) 

}


with InitLogger is located in another package


func InitSimpleLogger(level int) {

    if level == 1 {

        Trace = log.New(ioutil.Discard, "[TRACE]\t", log.Ldate|log.Ltime|log.Lshortfile)

        Info = log.New(ioutil.Discard, "[INFO]\t", log.Ldate|log.Ltime|log.Lshortfile)

        Warning = log.New(ioutil.Discard, "[WARNING]\t", log.Ldate|log.Ltime|log.Lshortfile)

        Error = log.New(os.Stdout, "[ERROR]\t", log.Ldate|log.Ltime|log.Lshortfile)

    } else if level == 2 {

        Trace = log.New(ioutil.Discard, "[TRACE]\t", log.Ldate|log.Ltime|log.Lshortfile)

        Info = log.New(ioutil.Discard, "[INFO]\t", log.Ldate|log.Ltime|log.Lshortfile)

        Warning = log.New(os.Stdout, "[WARNING]\t", log.Ldate|log.Ltime|log.Lshortfile)

        Error = log.New(os.Stdout, "[ERROR]\t", log.Ldate|log.Ltime|log.Lshortfile)

    } else if level == 3 {

        Trace = log.New(os.Stdout, "[TRACE]\t", log.Ldate|log.Ltime|log.Lshortfile)

        Info = log.New(ioutil.Discard, "[INFO]\t", log.Ldate|log.Ltime|log.Lshortfile)

        Warning = log.New(os.Stdout, "[WARNING]\t", log.Ldate|log.Ltime|log.Lshortfile)

        Error = log.New(os.Stdout, "[ERROR]\t", log.Ldate|log.Ltime|log.Lshortfile)

    } else {

        Trace = log.New(os.Stdout, "[TRACE]\t", log.Ldate|log.Ltime|log.Lshortfile)

        Info = log.New(os.Stdout, "[INFO]\t", log.Ldate|log.Ltime|log.Lshortfile)

        Warning = log.New(os.Stdout, "[WARNING]\t", log.Ldate|log.Ltime|log.Lshortfile)

        Error = log.New(os.Stdout, "[ERROR]\t", log.Ldate|log.Ltime|log.Lshortfile)

    }

}



千巷猫影
浏览 108回答 1
1回答

芜湖不芜

测试不确保misc.InitSimpleLogger(4)在依赖此函数调用的副作用的测试之前执行。将调用移至函数init()以确保它在测试之前执行。改变func TestFTPGetFilesFromServer(t *testing.T) {     misc.InitLogger(4)  }到func init() {     misc.InitLogger(4)  }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go