[关闭]
@SkeyJIA 2017-02-20T14:58:02.000000Z 字数 593 阅读 967

go test

go


go test 的相关使用介绍

1. 条件build

a.go

  1. // +build !test
  2. ...todu code

b.go

  1. //+build test
  2. ...todu code

2. go test 最大操作范围:包 包

  1. //在packageName目录下,包与包用空格间隔
  2. go test -tags "test" funnel/tasks net/http

3.go test 最小操作范围:func

  1. go test -run=TestFunctionName
  2. //测试当前包下面的TestFunctionName函数
  • 测试文件需要与被测试文件保持同级目录 demo.go demo_test.go
  • 文件名命名规则:FuncName_test.go
  • 测试函数名命名:Test+FuncName(t *testing.T)
  • 一个test.go文件内,可以包含多个Test+FuncName
  • 编写包级别的公共函数,以 packageName_test.go 命名文件

4. go test 常用命令列表

  • -v 无论用例是否测试通过都会显示结果,不加"-v"表示只显示未通过的用例结果
  • -run 测试某个函数 go test -run=TestFunctionName
  • -cover 输出测试代码覆盖率
  • t.Log,t.Logf 打印正常信息(结果与预期相符)
  • t.Error,t.Errorf 打印错误信息(结果与预期不符)
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注