Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 在运行任何规范之前运行测试套件(在套件安装之前_Unit Testing_Go_Ginkgo - Fatal编程技术网

Unit testing 在运行任何规范之前运行测试套件(在套件安装之前

Unit testing 在运行任何规范之前运行测试套件(在套件安装之前,unit-testing,go,ginkgo,Unit Testing,Go,Ginkgo,我正在体验用于单元测试Go(lang)restapi的银杏(和Gomega)包 我需要使用全局设置,通过定义 var _ = BeforeSuite(func() {...}) 然后每个规范(具体的\u test.go)都应该在这个全局设置之后运行。不幸的是,我无法做到这一点 我的套件文件名是handlers\u suite\u test.go,我的第一个测试规范名是cartContentsHandler\u test.go。在我看来,按照字母顺序运行测试文件,进行cartContentsHa

我正在体验用于单元测试Go(lang)restapi的银杏(和Gomega)包

我需要使用全局设置,通过定义

var _ = BeforeSuite(func() {...})
然后每个规范(具体的
\u test.go
)都应该在这个全局设置之后运行。不幸的是,我无法做到这一点

我的套件文件名是
handlers\u suite\u test.go
,我的第一个测试规范名是
cartContentsHandler\u test.go
。在我看来,按照字母顺序运行测试文件,进行
cartContentsHandler\u测试。go
handlers\u suite\u测试之前运行。go
。我放了一些
日志()
调用这两个文件,不幸的是它们只是确认了我的发现

这真是令人不快的情况,因为我根本无法让我的测试运行……我需要确保在所有规范之前设置并运行
httptest.Server
和DB池连接

您知道如何使套件测试作为测试规范之前的第一个文件运行吗?(我已经尝试将套件文件命名为
\u suite\u test.go
,但在这种情况下,似乎套件根本没有执行)

我的
处理程序\u套件\u测试。转到

package handlers_test

import (
    "<PROJ>/config"
    "<PROJ>/lib"
    "<PROJ>/router"
    "github.com/gorilla/mux"
    . "github.com/onsi/ginkgo"
    . "github.com/onsi/gomega"

    "log"
    "net/http/httptest"
    "os"
    "testing"
)

var r *mux.Router
var s *httptest.Server
var serverURL string

func TestHandlers(t *testing.T) {
    RegisterFailHandler(Fail)
    RunSpecs(t, "Caracal Handlers Suite")
}

var _ = BeforeSuite(func() {
    r = router.NewRouter()
    s = httptest.NewServer(r)
    Expect(len(s.URL)).To(BeNumerically(">", 0))
    serverURL = s.URL
    log.Print("###" + serverURL + "###\n\n") // ==> THIS PRINTS MUCH LATER AFTER log.Print() in cartContentsHandler_test.go

    cwd, _ := os.Getwd()
    cfg := config.ReadCfg(cwd + "/../config/config.json").DB
    lib.DB = lib.InitDB(cfg)
    err := lib.DB.Ping()
    Expect(err).NotTo(HaveOccurred())
})

var _ = AfterSuite(func() {
    //  lib.DB.Close() // ==> this was running into Panic...
    s.Close()
})
在root中时,我以以下方式运行测试:

ginkgo handlers -cover --v

发生的情况是,
BeforeSuite
注册了一个将在测试套件之前执行的函数,而
It
注册了一个将成为测试套件一部分的测试函数。对
description
Context
的回调将立即执行。因此,您必须将依赖于
BeforeSuite的所有内容都放在测试套件之前进入
It

发生的是
BeforeSuite
注册一个将在测试套件之前执行的函数,
It
注册一个将成为测试套件一部分的测试函数。对
description
Context
的回调将立即执行。因此,您必须将所有内容放在at依赖于
BeforeSuite
进入
It

为保证
BeforeSuite
在本地设置
描述
块之前运行必要的全局设置,应使用:

在您的套房中:

var serverURL string

var _ = BeforeSuite(func() {
    r = router.NewRouter()
    s = httptest.NewServer(r)
    serverURL = s.URL
})
在测试中:

var _ = Describe("Handlers/CartContentsHandler", func() {
  var url url.URL

  BeforeEach(func() {
    url = serverURL + "/cart-contents"
  })

  It("Makes a GET request", func() {
    # ....
  })
})

为保证
BeforeSuite
在本地设置
description
块之前运行必要的全局设置,您应使用:

在您的套房中:

var serverURL string

var _ = BeforeSuite(func() {
    r = router.NewRouter()
    s = httptest.NewServer(r)
    serverURL = s.URL
})
在测试中:

var _ = Describe("Handlers/CartContentsHandler", func() {
  var url url.URL

  BeforeEach(func() {
    url = serverURL + "/cart-contents"
  })

  It("Makes a GET request", func() {
    # ....
  })
})

如果将log.Print放入cartContensHandler_test.go中的
It
函数中,会发生什么?嗯,那么在
It
中添加新的
log.Print()
BeforeSuite
中打印后会输出什么呢?我将移动规范“setup”将代码写入
It
并进行尝试!如果您在cartContensHandler_test.go?Hmm中的
It
函数中进行打印,会发生什么情况?因此,在
It
中添加新的
log.Print()
BeforeSuite
中打印后,它将真正输出!我将移动规范“设置”将代码转换成
It
并将尝试它!你是我的英雄!谢谢!!!所以我终于可以用一个全局设置为一个包运行测试了!!!万岁!!!使用银杏和Gomega,单元测试看起来也像我在PHP或JS world中使用的一样:-)你是我的英雄!谢谢所以最后我可以用一个全局设置为一个包运行测试!!!好极了使用银杏和Gomega,单元测试看起来也和我在PHP或JS world中使用的一样:-)