设为首页 加入收藏

TOP

golang单元测试一(简单函数测试)(八)
2023-07-23 13:29:47 】 浏览:127
Tags:golang 单元测 简单函
e/uri/uri.go:54.32,56.3 1 1 go-demo/testexample/uri/uri.go:60.16,62.3 1 0 go-demo/testexample/uri/uri.go:67.14,69.3 1 1 go-demo/testexample/uri/uri.go:69.8,72.3 1 1 go-demo/testexample/uri/uri.go:77.16,79.3 1 1 go-demo/testexample/uri/uri.go:81.16,83.17 2 1 go-demo/testexample/uri/uri.go:87.3,87.29 1 1 go-demo/testexample/uri/uri.go:83.17,86.4 1 0 go-demo/testexample/uri/uri.go:88.8,90.3 1 1 go-demo/testexample/uri/uri.go:92.19,94.44 2 1 go-demo/testexample/uri/uri.go:94.44,96.4 1 1 go-demo/testexample/uri/uri.go:99.18,100.37 1 1 go-demo/testexample/uri/uri.go:100.37,101.56 1 1 go-demo/testexample/uri/uri.go:101.56,105.24 1 0 go-demo/testexample/uri/uri.go:105.24,107.6 1 0 go-demo/testexample/uri/uri.go:108.10,108.30 1 1 go-demo/testexample/uri/uri.go:108.30,110.5 1 1 go-demo/testexample/uri/uri.go:111.9,113.4 1 0 go-demo/testexample/uri/uri.go:119.32,121.16 2 1 go-demo/testexample/uri/uri.go:125.2,127.80 2 1 go-demo/testexample/uri/uri.go:135.2,137.86 2 1 go-demo/testexample/uri/uri.go:148.2,148.35 1 1 go-demo/testexample/uri/uri.go:157.2,157.27 1 1 go-demo/testexample/uri/uri.go:121.16,123.3 1 0 go-demo/testexample/uri/uri.go:127.80,130.42 2 1 go-demo/testexample/uri/uri.go:130.42,132.4 1 1 go-demo/testexample/uri/uri.go:137.86,139.3 1 1 go-demo/testexample/uri/uri.go:139.8,146.3 1 1 go-demo/testexample/uri/uri.go:148.35,153.3 2 1 go-demo/testexample/uri/uri.go:153.8,155.3 1 1

看的我是一脸懵逼,不过别着急我们可以把这个文件转成我们想要的html格式:

go tool cover -html=cover_out -o cover_out.html

看下目录中是不是多了个html

tree
.
├── cover_out
├── cover_out.html
├── uri.go
└── uri_test.go

我们浏览器打开这个文件,会看到下面这个界面,不要太爽:
https://cdn.learnku.com/uploads/images/202209/20/25530/ji5UdFNJbi.png!large

红色的就是代表我们测试没有覆盖到的。只需要丰富下测试代码,就可以做到100%覆盖了。这部分就由你自己完成吧。

6、基准测试

通过 Benchmarking,您可以衡量代码的性能并查看您对代码所做的更改的影响,从而优化您的源代码。

文件名必须以 Benchmark 前缀作为单元测试文件名约定。(BenchmarkSomething(*testing.B))

package uri

import (
	"testing"
)

// Test matrix defined on http://www.rabbitmq.com/uri-spec.html
type testURI struct {
	url      string
	username string
	password string
	host     string
	port     int
	vhost    string
	canon    string
}

var uriTest = testURI{
	url:      "amqp://user:pass@host:10000/vhost",
	username: "user",
	password: "pass",
	host:     "host",
	port:     10000,
	vhost:    "vhost",
	canon:    "amqp://user:pass@host:10000/vhost",
}

func BenchmarkUri(b *testing.B) {
	for i := 0; i < b.N; i++ {
		_, _ = ParseURI(uriTest.url)
	}

}
  • 基准函数必须带参数测试。B
  • Benchmark 必须运行 N 次testing.B提供(N 是整数类型,从 Go 调整)
  • 当提供 -bench 标志时,由“go test”命令执行。
  • -bench标志以正则表达式的形式接受其参数。

执行命令如下:

go test -bench=.                             
goos: darwin
goarch: amd64
pkg: go-demo/testexample/uri
cpu: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
BenchmarkUri-8           2041880               579.2 ns/op
PASS
ok      go-demo/testexample/uri 2.075s

基准测试结果意味着基准测试通过。循环运行 2041880 次,每个循环速度为 579.2 纳秒。

首页 上一页 5 6 7 8 下一页 尾页 8/8/8
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇golang中的init初始化函数 下一篇golang的内存管理

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目