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

goconvey学习笔记:测试工具

范弘亮
2023-12-01

goconvey安装

使用go get进行安装:go get github.com/smartystreets/goconvey

单元测试代码

package goconvery

import (
	"errors"
)

func Add(a, b int) int {
	return a + b
}

func Sub(a, b int) int {
	return a - b
}

func Multiply(a, b int) int {
	return a * b
}

func Division(a, b int) (int, error) {
	if b == 0 {
		return 0, errors.New("被除数不能为0")
	}
	return a / b, nil
}
package goconvery

import (
	"testing"

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

func TestAdd(t *testing.T) {
	Convey("两个数相加", t, func() {
		//测试1+2应该=3
		So(Add(1, 2), ShouldEqual, 3)
	})
}

func TestSub(t *testing.T) {
	Convey("两个数相减", t, func() {
		//测试1-2应该=-1
		So(Sub(1, 2), ShouldEqual, -1)
	})
}

func TestMultiply(t *testing.T) {
	Convey("两个数相乘", t, func() {
		//测试1/2应该=2
		So(Multiply(1, 2), ShouldEqual, 2)
	})
}

func TestDivision(t *testing.T) {

	Convey("两个数相除", t, func() {
		//在
		Convey("被除数为0", func() {
			_, err := Division(1, 0)
			So(err, ShouldNotBeNil)
		})

		Convey("被除数不为0", func() {
			result, err := Division(10, 5)
			So(result, ShouldEqual, 2)
			So(err, ShouldBeNil)
		})
	})
}

运行测试

例:G:\go_study\src\goBase\goconvery>go test -v

在本地可以查看到测试结果

测试用例web页面

(选装)安装测试覆盖工具包:go get code.google.com/p/go.tools/cmd/cover

使用goconvery提供的自动化编译测试

例:`G:\go_study\src\goBase\goconvery>goconvey

2019/11/25 14:48:24 goconvey.go:61: Initial configuration: [host: 127.0.0.1] [port: 8080] [poll: 250ms] [cover: true]
2019/11/25 14:48:25 tester.go:19: Now configured to test 10 packages concurrently.
2019/11/25 14:48:25 goconvey.go:178: Serving HTTP at: http://127.0.0.1:8080
2019/11/25 14:48:25 goconvey.go:105: Launching browser on 127.0.0.1:8080
2019/11/25 14:48:25 integration.go:122: File system state modified, publishing current folders... 0 3149327442
2019/11/25 14:48:25 goconvey.go:118: Received request from watcher to execute tests...
2019/11/25 14:48:25 goconvey.go:111: exec: "start": executable file not found in %PATH%
2019/11/25 14:48:25 goconvey.go:113:
2019/11/25 14:48:25 executor.go:69: Executor status: 'executing'
2019/11/25 14:48:25 coordinator.go:46: Executing concurrent tests: goBase/goconvery
2019/11/25 14:48:27 parser.go:24: [passed]: goBase/goconvery
2019/11/25 14:48:27 executor.go:69: Executor status: 'idle'
2019/11/25 14:49:02 integration.go:122: File system state modified, publishing current folders... 3149327442 3149327830
2019/11/25 14:49:02 goconvey.go:118: Received request from watcher to execute tests...
2019/11/25 14:49:02 executor.go:69: Executor status: 'executing'
2019/11/25 14:49:02 coordinator.go:46: Executing concurrent tests: goBase/goconvery
2019/11/25 14:49:04 parser.go:24: [passed]: goBase/goconvery
2019/11/25 14:49:04 executor.go:69: Executor status: 'idle'
2019/11/25 14:49:08 integration.go:122: File system state modified, publishing current folders... 3149327830 3149327838
2019/11/25 14:49:08 goconvey.go:118: Received request from watcher to execute tests...

点击地址http://127.0.0.1:8080打开网页,在网页中查看测试运行结果

 类似资料: