Running Ginkgo test suite (BeforeSuite setup before any spec is ran

落花浮王杯 提交于 2019-12-13 07:13:35

问题


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 + "###\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()
})

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

回答1:


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.




回答2:


To guarantee that the BeforeSuite will run the necessary global setup before the local setup for a Describe block, you should use BeforeEach:

In your suite:

var serverURL string

var _ = BeforeSuite(func() {
    r = router.NewRouter()
    s = httptest.NewServer(r)
    serverURL = s.URL
})

and in the test:

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

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

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


来源:https://stackoverflow.com/questions/36202934/running-ginkgo-test-suite-beforesuite-setup-before-any-spec-is-ran

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!