The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
guonaihong

go test 常见套路(1)

  •  
  •   guonaihong ·
    guonaihong · Sep 16, 2019 · 3909 views
    This topic created in 2432 days ago, the information mentioned may be changed or developed.

    本文主要聊下 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 replies    2019-09-16 12:33:22 +08:00
    hantsy
        1
    hantsy  
       Sep 16, 2019
    Go 生态没 Junit,BDD 类似的语法吗?
    guonaihong
        2
    guonaihong  
    OP
       Sep 16, 2019
    @hantsy golang 也有 BDD 测试框架,只是主流的做法还是基于官方原生写法。
    lazyfighter
        3
    lazyfighter  
       Sep 16, 2019
    有个三方的 verify
    guonaihong
        4
    guonaihong  
    OP
       Sep 16, 2019
    @lazyfighter 可有 github 地址?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   913 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 21:11 · PVG 05:11 · LAX 14:11 · JFK 17:11
    ♥ Do have faith in what you're doing.