V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
guonaihong
V2EX  ›  Go 编程语言

go test 常见套路(1)

  •  
  •   guonaihong ·
    guonaihong · 2019-09-16 08:56:59 +08:00 · 3331 次点击
    这是一个创建于 1676 天前的主题,其中的信息可能已经有所发展或是发生改变。

    本文主要聊下 go 测试的常见套路,测试是开发过程中比较重要的一环节,特别是在 github 上撸代码,这是既要当开发,又要当测试。下面介绍常见套路让测试变的轻松点(最下面有本人 github 地址,感兴趣可一看)。

    go test 函数

    测试函数以 Test 开头,go test 就可以执行, 函数形参使用 testing.T 的指针

    func TestJoin(t *testing.T) {
    	urls := []string{
    		"http://127.0.0.1:43471/v1",
    	}
    
    	want := []string{
    		"http://127.0.0.1:43471/v1",
    	}
    
    	if joinPaths("", urls[0]) != want[0] {
    		t.Errorf("got %s want %s\n", joinPaths("", urls[0]), want[0])
    	}
    }
    

    go 性能测试

    导入 testing 包,指定 B 对象,就可以编写性能测试代码,性能测试代码以 Benchmark 开头这是和标准库的约定, 在常见的 go test 选项后面跟一个-bench 选项当然-benchmem 可以显示内存占用,下面的代码主要考量 bytes.Buffer 和 strings.Builder 的性能区别

    func Benchmark_BytesWriteMem(b *testing.B) {
    	var buf bytes.Buffer
    
    	for i := 0; i < b.N; i++ {
    		buf.WriteString("hello")
    		buf.String()
    	}
    }
    
    func Benchmark_builderMem(b *testing.B) {
    	var s strings.Builder
    
    	for i := 0; i < b.N; i++ {
    		s.WriteString("hello")
    		s.String()
    	}
    }
    
    • 输出
    env GOPATH=`pwd` go test -bench "Benchmark_builderMem" -benchmem  -v .
    goos: linux
    goarch: amd64
    pkg: github.com/guonaihong/test/string
    Benchmark_builderMem-4   	200000000	         9.54 ns/op	      30 B/op	       0 allocs/op
    PASS
    ok  	github.com/guonaihong/test/string	2.831s
    
    
    env GOPATH=`pwd` go test -bench "Benchmark_BytesWriteMem" -benchmem  -v .
    goos: linux
    goarch: amd64
    pkg: github.com/guonaihong/test/string
    Benchmark_BytesWriteMem-4   	  500000	    104201 ns/op	 1254075 B/op	       1 allocs/op
    PASS
    ok  	github.com/guonaihong/test/string	52.153s
    

    只测试某个函数

    go test 默认会把包下面的所有测试函数都跑一遍,如果从满屏幕测试函数里面找一个错误的日志很费力。 这时候需要只测试某个函数,可以使用-test.run 函数名的方式,该选项是支持正则表达式的。

    go test -test.run=^TestHello ./...
    

    查看测试代码覆盖度

    如果 go 里面可以细化到哪些代码被测试过,就可以看出精心编写的测试代码是否 bug,答案是必须要有。

    go test -coverprofile=cover.prof ./...
    go tool cover -html=cover.prof
    

    github 地址

    https://github.com/guonaihong/gout

    4 条回复    2019-09-16 12:33:22 +08:00
    hantsy
        1
    hantsy  
       2019-09-16 09:04:25 +08:00
    Go 生态没 Junit,BDD 类似的语法吗?
    guonaihong
        2
    guonaihong  
    OP
       2019-09-16 09:09:23 +08:00
    @hantsy golang 也有 BDD 测试框架,只是主流的做法还是基于官方原生写法。
    lazyfighter
        3
    lazyfighter  
       2019-09-16 11:09:13 +08:00
    有个三方的 verify
    guonaihong
        4
    guonaihong  
    OP
       2019-09-16 12:33:22 +08:00
    @lazyfighter 可有 github 地址?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2970 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 00:18 · PVG 08:18 · LAX 17:18 · JFK 20:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.