运行银杏测试套件(在运行任何规范之前的BeforeSuite设置 [英] Running Ginkgo test suite (BeforeSuite setup before any spec is ran
问题描述
我需要使用全局设置,通过定义应该可以实现的全局设置
var _ = BeforeSuite(func(){...})
然后每个规范(具体< file> _test.go
)应该在这个全局设置之后运行。不幸的是,我无法做到这一点...
我的套件文件名是 handlers_suite_test.go
,我的第一个测试规范名称是 cartContentsHandler_test.go
。在我看来,银杏运行在字母顺序排列测试文件制作 cartContentsHandler_test.go
来运行之前 handlers_suite_test.go
。我已经将一些 log()
调用放入这两个文件中,不幸的是他们只是证实了我的发现......
真的很不愉快,因为我不能让我的测试运行...我需要确保一个 httptest.Server
和数据库池连接在全部设置之前运行
你知道怎么做才能让suite_test在测试规范之前作为第一个文件运行?试图将套件文件命名为 _suite_test.go
,但在这种情况下,它看起来像套件甚至没有执行)。
我的 handlers_suite_test.go
:
package handlers_test
导入(
< PROJ> / config
< PROJ> / lib
< PROJ> / router
github .com / gorilla / mux
。github.com/onsi/ginkgo
。github.com/onsi/gomega
log
net / http / httptest
os
tes ting
)
var r * mux.Router
var s * httptest.Server
var serverURL string
func TestHandlers(t * testing.T){
RegisterFailHandler(失败)
RunSpecs(T, 狞处理程序套件)
}
VAR _ = BeforeSuite(FUNC(){
R = router.NewRouter()
S = httptest.NewServer(R)
期待(LEN(s.URL))向(BeNumerically( > 中,0))
serverURL = s.URL
log.Print(###+ serverURL +### \\\
\\\
)// ==>这将打印更晚后log.Print()在cartContentsHandler_test.go
CWD,_:= os.Getwd()
CFG:= config.ReadCfg(CWD +/../ config.json)。DB
lib.DB = lib.InitDB(cfg)
err:= lib.DB.Ping()
期望(err).NotTo(HaveOccurred( ))
$)
var _ = AfterSuite(func(){
// lib.DB.Close()// ==>这会进入Panic。 ..
s.Close()
})
My cartContensHandler_test.go
:
包handlers_test
import(
$ p< p>
。github.com/onsi/ginkgo
。github.com/onsi/gomega
log
(处理程序/ CartContentsHandler,func(){
描述(检索所有可用的)net / http
strings
)
var _ (无查询字符串参数,func(){
var rdr * strings.Reader
var req * http.Request
var res * http.Response
var err error
var url = serverURL +/ cart-contents
log.Print(url)
它(做一个GET请求,func(){
rdr = strings.NewReader()
req,err = http.NewRequest(GET,url,rdr)
Expect(err).NotTo(HaveOccurred())
})
它(检索响应,func(){
res,err = http。 DefaultClient.Do(req)
Expect(err).NotTo(HaveOccurred())
})
It(Returns HTTP 200 OK,func(){
Expect(res.StatusCode).To(BeNumerically(==,http.StatusOK))// ==>现在,这个返回值为404,因为请求的URL是没有服务器部分的
))
})
})
})
< p>在< PROJ> root中,我以这种方式运行测试:
ginkgo handlers -cover --v
解决方案会发生什么是
BeforeSuite
注册了一个将在测试套件之前执行的函数,而It
注册了一个测试函数将成为测试套件的一部分。立即执行对Describe
和Context
的回调。因此,您必须将依赖于BeforeSuite
的所有内容都放入It
中。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 iscartContentsHandler_test.go
. It seems to me Ginkgo runs the test files in alphabetical order making thecartContentsHandler_test.go
to run beforehandlers_suite_test.go
. I have put somelog()
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
解决方案What happens is that
BeforeSuite
registers a function that will be executed before the test suite, andIt
registers a test function that will be part of the test suite. The callbacks toDescribe
andContext
are executed immediately. So you must put everything that depends onBeforeSuite
intoIt
.这篇关于运行银杏测试套件(在运行任何规范之前的BeforeSuite设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!