Ginkgo测试框架初探

为什么选择Ginkgo

基于目前go语言流行的测试框架:

静态(TDD ) )。

是goconvey(BDD )

我是GinkGo(BDD )

在需求沟通上,如果对开发需求的理解有偏差,摩托车就容易变成自行车。 BDD的动作驱动测试为了解决这个问题,也就是说先写功能说明。 了解开发、测试、产品等,可以低成本修正错误。

BDD可以为我们产品的理解提供反馈,更符合对互联网团队目前功能点的验收测试,可以结合TDD框架testify进行更细致的测试。

使用过rspec框架的人,对Ginkgo的语法很熟悉,可以降低学习成本,Ginkgo的函数表达能力很强,功能也很丰富

名字

Ginkgo

GoConvey

testify

辅助

Gomega

斯泰尔斯

速度

速度

资产

Equal

IsSame

深度专家

True

Nil

爱普蒂

错误

输入

IsType

字符串容器

字符串匹配

集合

Panics

HasLen

马蒂斯

Satisfy

威奇

框架

Ginkgo是golang的BDD类型测试框架,Ginkgo官方推广的匹配库是gomega,可以代替嵌入go的Testing的语法库,减少代码写入量,语义

快速开始

Step1.安装

$ gogetgithub.com/onsi/gink go/gink go

$ go get github.com/onsi/gomega/.

Step2.生成测试套件套件

$ cd path/to/books

$ ginkgo bootstrap

上述命令将自动生成,如下所示:

//path/to/books/books _ suite _ test.go

package books_test

输入(

. ' github.com/onsi/ginkgo '

. ' github.com/onsi/gomega '

' testing '

functestbooks(t*testing.t ) {

注册故障管理器(fail )。

运行速度(t,' Books Suite ' ) )。

}

请注意,其中package是books_test。 在go中,package的名称必须与文件所在的目录名称一致。 packageName_test是特例,go允许

步骤3 .生成specs

$ ginkgo生成书

上述命令将自动生成,如下所示:

//path/to/books/book_test.go

package books_test

输入(

. '/path/to/books '

. ' github.com/onsi/ginkgo '

. ' github.com/onsi/gomega '

var_=describe('book ',func ) {

() )

step4.添加specs

//path/to/books/books _ suite _ test.go

var_=describe('book ',func ) {

var (

书册

错误错误

json s

tring

)

BeforeEach(func() {

json = `{

"title":"Les Miserables",

"author":"Victor Hugo",

"pages":1488

}`

})

JustBeforeEach(func() {

book, err = NewBookFromJSON(json)

})

Describe("loading from JSON", func() {

Context("when the JSON parses succesfully", func() {

It("should populate the fields correctly", func() {

Expect(book.Title).To(Equal("Les Miserables"))

Expect(book.Author).To(Equal("Victor Hugo"))

Expect(book.Pages).To(Equal(1488))

})

It("should not error", func() {

Expect(err).NotTo(HaveOccurred())

})

})

Context("when the JSON fails to parse", func() {

BeforeEach(func() {

json = `{

"title":"Les Miserables",

"author":"Victor Hugo",

"pages":1488oops

}`

})

It("should return the zero-value for the book", func() {

Expect(book).To(BeZero())

})

It("should error", func() {

Expect(err).To(HaveOccurred())

})

})

})

Describe("Extracting the author's last name", func() {

It("should correctly identify and return the last name", func() {

Expect(book.AuthorLastName()).To(Equal("Hugo"))

})

})

})

method

Ginkgo提供了一系列拥可以嵌套的函数,来更丰富地描述测试,下面举几个比较常用的函数

Describe

描述一种行为或者一个方法

Context

丰富Describe所描述的行为或方法,增加条件语句,尽可能全地覆盖各种condition

it

申明用例, 期望这个用例得到的结果

Expect

这个是gomega提供的方法,用来断言结果

BeforeEach

会在每个小于等于beforeEach嵌套层的it函数之前运行,来设置公用的数据变量

JustBeforeEach

会在所有BeforeEach执行之后运行,在每个小于等于JustBeforeEach嵌套层的it函数之前运行, 可以有效避免重复创建

Mock

Ginkgo没有提供相关的mock方法,Ginkgo作者认为可以通过依赖注入和通过interface来实现, 他认为这比mock和stubs更清楚和富有表现力,话虽如此,但Gomock这个package,Ginkgo作者是比较推荐的,他实现mock相对来说比较简单

import (

"code.google.com/p/gomock/gomock"

. github.com/onsi/ginkgo

. github.com/onsi/gomega

)

var _ = Describe("Consumer", func() {

var (

mockCtrl *gomock.Controller

mockThing *mockthing.MockThing

consumer *Consumer

)

BeforeEach(func() {

mockCtrl = gomock.NewController(GinkgoT())

mockThing = mockthing.NewMockThing(mockCtrl)

consumer = NewConsumer(mockThing)

})

AfterEach(func() {

mockCtrl.Finish()

})

It("should consume things", func() {

mockThing.EXPECT().OmNom()

consumer.Consume()

})

})

补充

此文档仅供快速入门,更多详细用法可以查询官方文档