GoConvey

1. GoConvey简介
GoConvey是一款针对Golang的测试框架,可以管理和运行测试用例,同时提供了丰富的断言函数,并支持很多Web界面特性。
2. 安装
go get github.com/smartystreets/goconvey
运行完后:
G O P A T H / s r c 目 录 下 新 增 了 g i t h u b . c o m 子 目 录 , 该 子 目 录 里 包 含 了 G o C o n v e y 框 架 的 库 代 码 。 在 GOPATH/src目录下新增了github.com子目录,该子目录里包含了GoConvey框架的库代码。 在 GOPATH/srcgithub.comGoConveyGOPATH/bin目录下新增了GoConvey框架的可执行程序goconvey.
3. 基本使用方法

package controllers

import (
	"testing"

	"github.com/smartystreets/goconvey/convey"
)

func TestActivityController_UpdateUsing(t *testing.T) {

	convey.Convey("3 shouldEqual 3", t, func() {
		convey.So(3, convey.ShouldEqual, 3)
	})

	convey.Convey("shouldBeTrue where a == b", t, func() {
		convey.So(IsEqual(3, 2), convey.ShouldBeTrue)
	})
}
func IsEqual(a, b int) bool {
	if a == b {
		return true
	}
	return false
}

要点

  • 每个测试用例必须使用Convey函数包裹起来,它的第一个参数为string类型的测试描述,第二个参数为测试函数的入参(类型为 *testing.T),第三个参数为不接受任何参数也不返回任何值得函数(习惯使用闭包)。
  • Convey函数的第三个参数闭包的实现中通过So函数完成断言判断,它的第一个参数为实际值,第二个参数为断言函数变量,第三个参数或者没有(当第二个参数为类ShouldBeTrue形式的函数变量)或者有(当第二个函数为类ShouldEqual形式的函数变量)。

4. Convey语句的嵌套
Convey语句可与无限嵌套,以体现测试用例之间的关系,需要注意的是,只有最外层的Convey需要传入*testing.T类型的变量t.

package controllers

import (
	"testing"

	."github.com/smartystreets/goconvey/convey"
)

func TestActivityController_UpdateUsing(t *testing.T) {

	Convey("Test Convey", t, func() {
		Convey("3 shouldEqual 3", func() {
			So(3, ShouldEqual, 3)
		})

		Convey("shouldBeTrue where a == b", func() {
			So(IsEqual(3, 2), ShouldBeTrue)
		})
	})
}
func IsEqual(a, b int) bool {
	if a == b {
		return true
	}
	return false
}

5. Web界面
在$GOPATH/bin下 执行 ./goconvey.exe, 在浏览器中输入 http://localhost:8080/, 打开Web界面。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值