Go test 详解

栏目: 编程工具 · 发布时间: 7年前

内容简介:注:部分内容参考网络上的文章,如有侵权,请告知。测试用例有四种形式:1、本地目录模式,在没有包参数(例如

注:部分内容参考网络上的文章,如有侵权,请告知。

Go test 的测试用例形式

测试用例有四种形式:

  • TestXxxx(t *testing.T) // 基本测试用例
  • BenchmarkXxxx(b *testing.B) // 压力测试的测试用例
  • Example_Xxx() // 测试控制台输出的例子
  • TestMain(m *testing.M) // 测试Main函数

Go test 有两种运行模式:

1、本地目录模式,在没有包参数(例如 go testgo test -v )调用时发生。在此模式下, go test 编译当前目录中找到的包和测试,然后运行测试二进制文件。在这种模式下,caching 是禁用的。在包测试完成后,go test 打印一个概要行,显示测试状态、包名和运行时间。

2、包列表模式,在使用显示包参数调用 go test 时发生(例如 go test mathgo test ./... 甚至是 go test . )。在此模式下,go 测试编译并测试在命令上列出的每个包。如果一个包测试通过, go test 只打印最终的 ok 总结行。如果一个包测试失败, go test 将输出完整的测试输出。如果使用 -bench-v 标志,则 go test 会输出完整的输出,甚至是通过包测试,以显示所请求的基准测试结果或详细日志记录。

go test 的变量有哪些?

  • test.short : 一个快速测试的标记,在测试用例中可以使用 testing.Short() 来绕开一些测试
  • test.outputdir : 输出目录
  • test.coverprofile : 测试覆盖率参数,指定输出文件
  • test.run : 指定正则来运行某个/某些测试用例
  • test.memprofile : 内存分析参数,指定输出文件
  • test.memprofilerate : 内存分析参数,内存分析的抽样率
  • test.cpuprofile : cpu分析输出参数,为空则不做cpu分析
  • test.blockprofile : 阻塞事件的分析参数,指定输出文件
  • test.blockprofilerate : 阻塞事件的分析参数,指定抽样频率
  • test.timeout : 超时时间
  • test.cpu : 指定cpu数量
  • test.parallel : 指定运行测试用例的并行数

Go test flag

  • -args
  • -c
  • -exec xprog
  • -i
  • -o file
  • -bench regexp
  • -benchtime t
  • -count n
  • -cover
  • -covermode set,count,atomic
  • -coverpkg pkg1,pkg2,pkg3
  • -cpu 1,2,4
  • -list regexp
  • -parallel n
  • -run regexp
  • -short
  • -timeout d
  • -v
  • -benchmem
  • -blockprofile block.out
  • -blockprofilerate n
  • -coverprofile cover.out
  • -cpuprofile cpu.out
  • -memprofile mem.out
  • -memprofilerate n
  • -mutexprofile mutex.out
  • -mutexprofilefraction n
  • -outputdir directory
  • -trace trace.out

更多中文文档:

什么是单元测试?

单元测试,是一种测试我们的代码逻辑有没有问题,有没有按照我们所期望的来运行,通过它来保证我们的代码质量。

顾名思义,单元测试,大多都是对某一个函数方法进行的测试,以让我们的测试粒度最细。

如何编写单元测试?

$ go test

可以测试 func TestXxx(*testing.T)

子测试

如何跳过一些测试

https://stackoverflow.com/questions/24030059/skip-some-tests-with-go-test

什么是基准测试?

基准测试,是一种测试代码执行性能的方式,你可以将多种代码实现进行比对。

基准测试主要是通过测试 CPU ( go test -bench . -test.cpuprofile ) 和内存 ( go test -bench . --benchmem ) 的分配,来展示被测试代码的性能,进而找到性能更优的解决方案。

如何编写基准测试?

func BenchmarkXXX(b *testing.B) {
	
}

什么是 HTTP 测试?

顾名思义,就是测试 web http 协议的测试。

如何编写 HTTP 测试?

Go 标准库为我们提供了一个 httptest 的库,通过它就能够轻松的完成 HTTP 的测试。

什么是测试覆盖率?

由单元测试的代码,触发运行到的被测试代码的代码行数占所有代码行数的比例,被称为测试覆盖率,代码覆盖率不一定完全精准,但是可以作为参考,可以帮我们测量和我们预计的覆盖率之间的差距。

go test 工具就为我们提供了一个度量测试覆盖率的能力。

$ go test -v -coverprofile=c.out

再通过 go tool 来将 c.out 转换成 html 格式的测试覆盖率报告。

$ go tool cover -html=c.out -o=tag.html

其他更多,可以参考: 测试

# 生成指定 package 的测试覆盖率(fib.out 后面不带参数的,默认是命令所在目录)
$ go test -v -covermode=count -coverprofile fib.out
# 查看汇总的 fib 测试覆盖率
$ go tool cover -func=fib.out
# 生成 html
$ go tool cover -html=fib.out -o fib.html

VSCode Go Test Coverage 插件

Go Coverage Viewer is the simplest way to view your code coverage results right inside the editor

测试比较工具

  • benchcmp
  • benchviz

第三方测试库

本文的一些代码实例地址: https://github.com/yangwenmai/examples/test-example

参考资料

  1. Go 单元测试,基准测试,http 测试
  2. Go 测试,go test 工具的具体指令 flag
  3. 测试、性能测试以及代码示例的编写
  4. visualizing-go-benchmarks-with-benchviz
  5. advanced-testing-with-go / YouTube 视频
  6. Testing Techniques / YouTube 视频

茶歇驿站

一个可以让你停下来看一看,在茶歇之余给你帮助的小站,这里的内容主要是后端技术,个人管理,团队管理,以及其他个人杂想。

Go test 详解


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Numerical Linear Algebra

Numerical Linear Algebra

Lloyd N. Trefethen、David Bau III / SIAM: Society for Industrial and Applied Mathematics / 1997-06-01 / USD 61.00

Numerical Linear Algebra is a concise, insightful, and elegant introduction to the field of numerical linear algebra.一起来看看 《Numerical Linear Algebra》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具