Golang学习笔记(四)Ginkgo测试框架

本文是Ginkgo测试框架的学习笔记,涵盖了安装、框架解析、Specs构建以及Spec Runner等内容。讲解了如何使用Ginkgo进行Bootstrapping、Adding Specs、Before/BeforeEach等操作,并介绍了如何标记失败的测试用例和记录输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ginkgo 测试框架学习笔记

链接导航 :
Ginkgo主页
Github源码

一、安装Ginkgo

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

然后会有一个ginkgo.exe安装在GOPATH路径的bin里
在这里插入图片描述

二、框架拆解

Ginkgo 提供了一些example

1、Bootstrapping a Suite

$ cd path/to/books
$ ginkgo bootstrap
package books_test

import (
    . "github.com/onsi/ginkgo"
    . "github.com/onsi/gomega"
    "testing"
)

func TestBooks(t *testing.T) {
   
    RegisterFailHandler(Fail)
    RunSpecs(t, "Books Suite")
}
  1. 新建两个package books_test 进行测试,一个文件用于写测试用例,一个文件放启动测试套件的函数.
  2. 导入Ginkgo和Gomega包时在包名前加一个[.],这样可以直接使用包中的方法函数,不用再【包名.函数名】
  3. 执行文件的时候,可以用go test方法也可以用ginkgo
  4. RegisterFailHandler(Fail): ginkgo通过调用Fail(description string)函数来发出fail信号。我们用RegisterFailHandler() 将Fail函数传递给Gomega。RegisterFailHandler()是连接ginkgo和gomega的唯一途径。
  5. RunSpecs(t *testing.T, suiteDescription string):用于启动测试套件,如果任何一个specs失败,该套件则自动返回失败。

执行结果

$ ginkgo #or go test

=== RUN TestBootstrap

Running Suite: Books Suite
==========================
Random Seed: 1378936983

Will run 0 of 0 specs


Ran 0 of 0 Specs in 0.000 seconds
SUCCESS! -- 0 Passed | 0 Failed | 0 Pending | 0 Skipped

--- PASS: TestBootstrap (0.00 seconds)
PASS
ok      books   0.019s

2、Adding Specs to a Suite

$ ginkgo generate book
package books_test

import (
    "/path/to/books"
    . "github.com/onsi/ginkgo"
    . "github.com/onsi/gomega"
)

var _ = Describe("Book", func() {
   
	 var (
        longBook  Book
        shortBook Book
    )

    BeforeEach(func() {
   
        longBook = Book{
   
            Title:  "Les Miserables",
            Author: "Victor Hugo",
            Pages:  1488,
        }

        shortBook = Book{
   
            Title:  "Fox In Socks",
            Author: "Dr. Seuss",
            Pages:  24,
        }
    })

    Describe("Categorizing book length", func() {
   
        Context("With more than 300 pages", func() {
   
            It("should be a novel", func() {
   
                Expect(longBook.CategoryByLength()).To(Equal("NOVEL"))
            })
        })

        Context("With fewer than 300 pages", func() {
   
            It("should be a short story", func() {
   
                Expect(shortBook.CategoryByLength()).To(Equal("SHORT STORY"))
            })
        })
    })
})
  1. 我们添加了一个顶层的描述容器,用Ginkgo的【Describe(text string, body func() ) bool 】函数。使用【var _ = …】允许我们直接描述填充顶层的Describe() 不需要用【func init(){}】初始化包装它。
  2. 【Describe() {}】中的函数包含了我们的使用规范。
  3. 为了共享BeforeEach和It之间的状态,Ginkgo使用闭包函数变量来构建顶层的Describe()和Context()
  4. Ginkgo中使用【Descirbe()】和【Context()】来描述代码的测试行为,将一个或多个测试用例It归类。
  5. Ginkgo中使用【BeforceEach()】来为specs设置状态,执行每个测试用例It前执行该段代码,使用【It()】来指定单个spec,是测试用例的基本单位,即It中包含的代码就算一个测试用例。
  6. 使用Gomega中的【Expect()】函数来设置期望。

执行结果

$ ginkgo # or go test
=== RUN TestBootstrap

Running Suite: Books Suite
==========================
Random Seed: 1378938274

Will run 2 of 2 specs

••
Ran 2 of 2 Specs in 0.000 seconds
SUCCESS! -- 2 Passed | 0 Failed | 0 Pending | 0 Skipped

--- PASS: TestBootstrap (0.00 seconds)
PASS
ok      books   0.025s

3、Marking Specs as Failed

虽然通常使用匹配器库(如Gomega)在规范中进行断言,但Ginkgo提供了一个简单的、全局的失败函数,它允许您将一个spec标记为失败。

Fail("Failure reason")

Fail() 标志该测试用例运行结果为失败,并打印里面的信息。
Fail和Gomega(Gomega也调用了Fail)将会把当前的Space和panic记录为失败。同时Ginkgo会停止当前进度中的spec,包括后续所有与该时间相关的断言和代码。
通常Ginkgo会自动挽救这种panic,并进入下一段测试。
如果测试启动的一个线程调用了Fail,或调用了Gomega的失败断言,那么Ginkgo没有办法挽救错误引起的panic。这会导致后续的测试内容中断。为了避免这种情况,需要使用【GinkgoRecover】来挽救panic。
Example:

It("panics in a goroutine", func(done Done) 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值