golang开发技能

本文主要介绍go相关 开发技巧、调试技巧、工具使用、单元测试、基准测试、性能测试相关。

1、Go命令:go test工具详解

这里先大致介绍测试工具"go test",go test 本身可以携带很多参数,熟悉这些参数可以让我们的测试过程更加方面。具体使用参见下一节。

(1)运行整个项目

go test

(2)只运行某个测试文件

注:math_test.go 和 math.go 是一对,缺一不可且前后顺序不可调。

go test math_test.go math.go

(3)加 -v 查看详细结果

go test -v math_test.go math.go

(4)只测试某个函数,-run支持正则。如下例子中 TestAdd,如果还有一个测试函数为TestAdd02那么它也会被运行。

go test -v -run="TestAdd"

(5)生成test的二进制文件:加 -c 参数

go test -v -run="TestAdd" -c

(6)执行这个test测试文件:加 -o 参数

go test -v -o math.test

(7)只测试安装/重新安装 依赖包,而不运行代码:加 -i 参数

go test -i

2、单元测试:如何进行单元测试

计算机中单元测试(Unit Testing)又称模块测试。用于对程序模块进行正确性检验。

程序单元是应用的最小可测试部件,一般来说都是对某一个函数方法进行测试,以尽可能的保证没有问题或者问题可被我们预知。为了达到这个目的,我们可以使用各种手段、逻辑,模拟不同的场景进行测试。

那么我们如何在写Golang代码时,进行单元测试呢? ------ 其实很简单。

2.1、单元测试

准备如下两个Go文件。

math.go

Go 复制代码
package math

func Add(x, y int) int {
	return x + y
}

math_test.go

Go 复制代码
package math

import "testing"

func TestAdd(t *testing.T) {
	t.Log(Add(1, 2))
}

然后使用 go test 工具去执行。输出如下:

Go 复制代码
[root746495-0 /data/go/gostudy/gocode/chapter1_basic]# go test .
ok      chapter1        0.002s

2.2、测试框架遵循规则

观察上述事例知道 Go语言测试框架要遵循以下原则。

(1)单元测试代码的go文件必须以 _test.go 结尾。前面最好是被测试的文件名(不是必须),例如要测试 math.go 那么对应的测试文件命名为 math_test.go 最好。

(2)单元测试的函数名必须以 Test 开头,后面直接跟要测试的函数名。比如要测试Add函数,测试的函数就命名为TestAdd。

(3)单元测试的函数必须接收一个指向 testing.T 类型的指针,并且不能返回任何值。

2.3、表组测试

Add(1,2)是一次单元测试的场景,而Add(2,4)、Add(3,6) 又是另外两种单元测试的场景。

对于类似这种多种输入场景的测试,我们可以同时放在TestAdd里进行测试,即 表组测试

修改 math_test.go 如下:

Go 复制代码
package math

import "testing"

func TestAdd(t *testing.T) {
	sum := Add(1, 2)
	if sum == 3 {
		t.Log("the result is ok")
	} else {
		t.Fatal("the result is wrong!")
	}

	sum = Add(2, 4)
	if sum == 6 {
		t.Log("the result is ok")
	} else {
		t.Fatal("the result is wrong!")
	}
}

执行 go test . -v

Go 复制代码
[root746495-0 /data/go/gostudy/gocode/chapter1_basic]# go test . -v
=== RUN   TestAdd
    math_test.go:8: the result is ok
    math_test.go:15: the result is ok
--- PASS: TestAdd (0.00s)
PASS
ok      chapter1        0.002s

如果输出场景实在太多,也可以使用如下所示的 表格测试法

将 math_test.go 修改如下:

Go 复制代码
package math

import "testing"

type TestTable struct {
	xarg int
	yarg int
}

func TestAdd(t *testing.T) {
	tables := []TestTable{
		{1, 2},
		{2, 4},
		{4, 8},
		{5, 10},
		{6, 12},
	}
	for _, table := range tables {
		result := Add(table.xarg, table.yarg)
		if result == (table.xarg + table.yarg) {
			t.Log("the result is ok")
		} else {
			t.Fatal("the result is wrong")
		}
	}
}

执行 go test . -v 输出如下:

Go 复制代码
[root5259746495-0 /data/go/gostudy/gocode/chapter1_basic]# go test . -v
=== RUN   TestAdd
    math_test.go:21: the result is ok
    math_test.go:21: the result is ok
    math_test.go:21: the result is ok
    math_test.go:21: the result is ok
    math_test.go:21: the result is ok
--- PASS: TestAdd (0.00s)
PASS
ok      chapter1        0.002s

3、调试技巧:使用gdb调试Go程序

如果使用 vscode 或者 golang 可以直接上手,关于ide调试这里不做过多介绍。

接下来主要介绍下GDB命令行调试。

------ 关于gdb调试后续用到再说吧。

4、Go命令:Go命令指南

这些在go语言入门中都已经介绍过了。

5、性能分析:pprof工具的简单使用

pprof 是Go程序性能分析常用的工具,关于pprof有很多的包,分别是:

  • runtime/pprof:Go的内置故,比较基础不常用。
  • pkg/profile:对runtime/pprof进行简化,一行代码即可,等程序执行结束后才可以分析。
  • net/http/pprof:最好用的库,可以暴露http服务实时获取分析。

km上有很多资料、也有过分享;后面需要的时候再研究研究。

相关推荐
lamdaxu4 分钟前
分布式调用(02)
后端
daiyunchao5 分钟前
让Pomelo支持HTTP协议
后端
芒猿君32 分钟前
AQS——同步器框架之源
后端
SaebaRyo40 分钟前
手把手教你在网站中启用https和http2
后端·nginx·https
咩咩觉主1 小时前
C# &Unity 唐老狮 No.7 模拟面试题
开发语言·unity·c#
大丈夫在世当日食一鲲1 小时前
Java中用到的设计模式
java·开发语言·设计模式
A-Kamen1 小时前
Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现与实战指南
java·spring boot·后端
豆豆酱1 小时前
Transformer结构详解
后端
upsilon1 小时前
golang切片slice
后端·go
狂奔小菜鸡1 小时前
Java运行时数据区
java·jvm·后端