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条)

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了