当前位置: 首页 > 工具软件 > GoConvey > 使用案例 >

GoConvey

皇甫繁
2023-12-01

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界面。

 类似资料:

相关阅读

相关文章

相关问答