dsdtumf776629385 2016-03-24 14:42
浏览 68
已采纳

运行Ginkgo测试套件(在运行任何规格之前安装BeSuiteSuite)

I am experiencing with Ginkgo (and Gomega) packages for unit testing Go(lang) Rest API.

I need to use global setup which should be achievable by defining

var _ = BeforeSuite(func() {...})

Then each spec (concrete <file>_test.go) should run after this global setup. Unfortunately I cannot make this happen...

My suite file name is handlers_suite_test.go and my first test spec name is cartContentsHandler_test.go. It seems to me Ginkgo runs the test files in alphabetical order making the cartContentsHandler_test.go to run before handlers_suite_test.go. I have put some log() call into both files and unfortunately they just confirm my findings...

This is really unhappy situation as I cannot make my tests running at all... I need to make sure that an httptest.Server and DB pool connection are set up and running before all specs.

Do you know what to do in order to make the suite_test to run as the very first file before test specs? (I already tried to name the suite file as _suite_test.go but in this case it looks like suite is not even executed at all).

My handlers_suite_test.go:

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 + "###

") // ==> 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()
})

My cartContensHandler_test.go:

package handlers_test

import (
    . "github.com/onsi/ginkgo"
    . "github.com/onsi/gomega"

    "log"
    "net/http"
    "strings"
)

var _ = Describe("Handlers/CartContentsHandler", func() {
    Describe("Retrieves all available cart content types", func() {
        Context("No query string parameters", func() {
            var rdr *strings.Reader
            var req *http.Request
            var res *http.Response
            var err error
            var url = serverURL + "/cart-contents"

            log.Print(url)

            It("Makes a GET request", func() {
                rdr = strings.NewReader("")
                req, err = http.NewRequest("GET", url, rdr)
                Expect(err).NotTo(HaveOccurred())
            })

            It("retrieves a response", func() {
                res, err = http.DefaultClient.Do(req)
                Expect(err).NotTo(HaveOccurred())
            })

            It("Returns HTTP 200 OK", func() {
                Expect(res.StatusCode).To(BeNumerically("==", http.StatusOK)) // ==> NOW THIS RETURNS 404 as request is to URL without server part
            })
        })
    })
})

When in <PROJ> root, I run the tests this way:

ginkgo handlers -cover --v
  • 写回答

2条回答 默认 最新

  • doutuan9357 2016-03-24 15:40
    关注

    What happens is that BeforeSuite registers a function that will be executed before the test suite, and It registers a test function that will be part of the test suite. The callbacks to Describe and Context are executed immediately. So you must put everything that depends on BeforeSuite into It.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题