前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >golang测试框架goconvey的使用

golang测试框架goconvey的使用

作者头像
Johns
修改2022-06-30 10:27:24
3.6K0
修改2022-06-30 10:27:24
举报
文章被收录于专栏:代码工具代码工具

前面我们介绍了golang测试框架里面的testify, 下面让了解一下另一个用的也比较多的断言框架goconvey。

一。简介

goconvey是一个支持golang的单元测试框架,能够自动监控文件修改并启动测试,并可以将测试结果实时输出到web界面,goconvey提供了丰富的断言简化测试用例的编写。

github地址:https://github.com/smartystreets/goconvey

以下是官方列举的goconvey的几个特性:

  • 直接集成go test
  • 可以管理和运行测试用例
  • 提供了丰富的断言函数
  • 支持很多 Web 界面特性(通过http://localhost:8080访问)
  • 设置界面主题
  • 查看完整的测试结果
  • 使用浏览器提醒
  • 自动检测代码变动并编译测试
  • 半自动化书写测试用例:http://localhost:8080/composer.html
  • 查看测试覆盖率:http://localhost:8080/reports/
  • 临时屏蔽某个包的编译测试

相比较于testify,goconvey可以功能较为聚焦,自动化做的非常赞,测试报告也是非常的专业。接下来让我们具体看一下如何使用goconvey框架吧。

二。使用方法

1. 安装

代码语言:txt
复制
go get github.com/smartystreets/goconvey

2. Quick start

GoConvey "Given-when-then"(如果-当-推断)的模式, 非常符合我们人的正常理解逻辑。

代码语言:txt
复制
package package_name
import (
    "testing"
    . "github.com/smartystreets/goconvey/convey"
)

func TestSpec(t *testing.T) {

	// Only pass t into top-level Convey calls
	Convey("Given some integer with a starting value", t, func() {
		x := 1

		Convey("When the integer is incremented", func() {
			x++

			Convey("The value should be greater by one", func() {
				So(x, ShouldEqual, 2)
			})
		})
	})
}

3. 启动

代码语言:txt
复制
$ $GOPATH/bin/goconvey

通过http://localhost:8080可以查看测试结果,如果测试代码有做修改,面板会自动更新。

image.png
image.png

更多请参考官方iwiki:https://github.com/smartystreets/goconvey/wiki/Documentation

本文系外文翻译,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系外文翻译前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一。简介
  • 二。使用方法
    • 1. 安装
      • 2. Quick start
        • 3. 启动
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档