首页 > 编程知识 正文

ginkgoforte,门萨入门测试

时间:2023-05-04 23:12:52 阅读:108814 作者:829

简介GinkGo是一个BDD风格的go测试框架,旨在帮助您有效创建具有表现力的综合测试。

$ gogetgithub.com/onsi/ginkgo/ginkgo $ gogetgithub.com/onsi/go mega/. ginkgo入门测试$ goenv|grep-igopathgopathgopathopa src $ mkdir books $ CD books $ ginkgo bootstrap $ ls books _ suite _ test.go $ cat books _ test.gooks onsi/ginkgo testing.t ) registerfailhandler ) fai ing ' books suite ' } $ vim books.gopackagebookstypebookstruct { titlestringauthorstrion book ) categorybylling string { IFB.pages=300 { return ' novel ' } return ' short story ' } $ ginkgofailedtocompilebooks 3360 go 3360 cannotfindmain see ' gohelpmodules ' ginkgo ran1suite in 53.915004 mstestsuitefailed $ gomodiled 0 module books $ gin ks e : books suite===============random seed 33601595924522 will run0of0specs ruite -0passed|0 failed|0 pending|0 skippedpassginkgoran1suite in 1.028037658 stestsuitepassed分析:

可以在与books包相同的目录中指定books_test包。 您可以使用books_test而不是books来避免破坏books软件包中的软件包。 在测试中,必须像导入其他包一样导入books包并从外部进行访问。 它非常适合进入封装,测试内部结构,并进行更多的行为测试。 当然,也可以不使用此功能。 只需将package books_test更改为package books即可。 将. ginkgo和gomega软件包引入测试的顶级命名空间TestBooks是testing测试。 运行Go test或ginkgo命令时,go测试运行将执行此功能。 注册故障处理器(fail ) : Ginkgo测试通过调用故障(descriptionstring )功能来指示失败。 我们使用RegisterFailHandler将这个函数传递给了Gomega。 这是Ginkgo和Gomega之间的唯一连接点。 runspecs(t*testing.t,suiteDescription string )向Ginkgo通知测试套件的启动。 如果任何specs失败,Ginkgo会自动使testing.T失败。 在Specs中添加空测试套件没意思。 虽然可以直接将测试添加到books_suite_test.go中,但建议将测试拆分为单独的文件,特别是对于包含多个文件的软件包。 在book.go模型中添加测试文件吧。

$ ginkgogeneratebookgeneratingginkgotestforbookin : book _ test.go $ catbook _ test.gopackagebooks _ test import (gink

ginkgo和gomega包导入到顶级命名空间中。 导入books包。 这是为了使用特殊的books_test包来隔离测试和代码。 为了方便起见,我们将books包导入到命名空间中,并使用Ginkgo的Describe(textstring,body func ) ) bool函数添加了顶级的describe容器。 var _=…技术允许您通过在未包装在funcinit{}Describe中的Describe功能中包含Specs来对Describe进行顶级评估。 现在,让我们添加一些从JSON加载books的测试。

books_test.go

package books _ test import (.' github.com/onsi/ginkgo '.' github.com/onsi/go mega '/' books ' ) typebokstrokstttstrattttttttttttttttttype book ) CategoryByLength ) string ) ifb.pages=300 ) return ' novel ' } return ' short story ' } var_=descriing func () longbookbookshortbook before each (var ) ) longbook=book ) title3360'lesmiserables author3360 Pages: 1488,} short book=book { title : ' foxin socks ',author:'dr.seuss ',Pages: 24, } ) describe () describe () ) ) ) ) 65 func ()上下文)、withmorethan300pages )、func )、it )、shouldbeanovel ) func () expect (short book.categorybylength ) ) ) ).to ) equal('shortstory ' ) ) ) ) ) ) ) ) ) ) ) )。

Ginkgo广泛使用闭包()闭包不是私有的,闭包的意思不是“内部封闭状态”,而是“外部封闭状态”!可以构建描述性测试套件。 必须使用Describe容器和Context容器以表示形式组织代码行为。 必须使用Describe容器和Context容器以表示形式组织代码行为。 要在BeforeEach和It之间共享状态,请使用闭包变量。 通常在关联最大的Describe或Context容器的顶部定义。 使用Gomega的Expect语法为CategoryByLength ()方法生成期望值。 $ ginkgorunningsuite : books suite===============random seed 33601595926303 will run2of 2s ping-=

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。