博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【GoLang】GoLang 单元测试、性能测试使用方法
阅读量:4507 次
发布时间:2019-06-08

本文共 2521 字,大约阅读时间需要 8 分钟。

单元测试代码:

ackage testimport (    // "fmt"    "testing")func Test_FlowControl(t *testing.T) {    var x int64 = 10    if x == 10 {        // fmt.Println("x is  10")        t.Log("x is  10")    } else {        // fmt.Println("x is not 10")        t.Log("x is not 10")    }    t.Log(x)}

性能测试代码:

package testimport (    // "fmt"    "testing")func Benchmark_FlowControl(b *testing.B) {    for i := 0; i < b.N; i++ { //use b.N for looping        b.Log(i)    }}

 

 

1.创建测试文件夹mysql,文件夹下的go文件的package必须与文件夹名一致(不然会识别不到)

2.创建需要测试的文件mysql.go(使用github.com/go-sql-driver/mysql包)

package mysqlimport (    "database/sql"    _ "github.com/go-sql-driver/mysql")func findByPk(pk int) int {    var num int = 0    db, err := sql.Open("mysql", "root:@tcp(127.0.0.1:3306)/plugin_master?charset=utf8")    if err != nil {        panic(err.Error())    }    defer db.Close()    stmtOut, err := db.Prepare("select id from t_admin where id=?")    if err != nil {        panic(err.Error())    }    defer stmtOut.Close()    err = stmtOut.QueryRow(pk).Scan(&num)    if err != nil {        panic(err.Error())    }    return num}
View Code

3.创建单元测试用例文件mysql_test.go(文件名必须是*_test.go的类型,*代表要测试的文件名,函数名必须以Test开头如:TestXxx或Test_xxx)

package mysqlimport (    "testing")func Test_findByPk(t *testing.T) {    num := findByPk(1)    t.Log(num)}
View Code
测试所有的文件 go test,将对当前目录下的所有*_test.go文件进行编译并自动运行测试。
测试某个文件使用”-file”参数。go test –file *.go 。例如:go test -file mysql_test.go,"-file"参数不是必须的,可以省略,如果你输入go test b_test.go也会得到一样的效果。
测试某个方法 go test -run='Test_xxx' "-v" 参数 go test -v ... 表示无论用例是否测试通过都会显示结果,不加"-v"表示只显示未通过的用例结果

4.创建benchmark性能测试用例文件mysql_b_test.go(文件名必须是*_b_test.go的类型,*代表要测试的文件名,函数名必须以Benchmark开头如:BenchmarkXxx或Benchmark_xxx)

package mysqlimport (    "testing")func Benchmark_findByPk(b *testing.B) {    for i := 0; i < b.N; i++ { //use b.N for looping        findByPk(1)    }}
View Code
进行所有go文件的benchmark测试 go test -bench=".*" 或 go test . -bench=".*"
对某个go文件进行benchmark测试 go test mysql_b_test.go -bench=".*"

5.用性能测试生成CPU状态图(暂未测试使用)

使用命令:
go test -bench=".*" -cpuprofile=cpu.prof -c
cpuprofile是表示生成的cpu profile文件
-c是生成可执行的二进制文件,这个是生成状态图必须的,它会在本目录下生成可执行文件mysql.test
然后使用go tool pprof工具
go tool pprof mysql.test cpu.prof
调用web(需要安装graphviz)来生成svg文件,生成后使用浏览器查看svg文件 参考 参考资料: http://studygolang.com/articles/2491 http://www.01happy.com/golang-unit-testing/ http://www.cnblogs.com/yjf512/archive/2013/01/18/2865915.html http://blog.csdn.net/samxx8/article/details/46894587 http://www.phpddt.com/go/go-testing.html

转载于:https://www.cnblogs.com/junneyang/p/6071084.html

你可能感兴趣的文章
filebeat
查看>>
如何在Bitmap中画图?(MFC)
查看>>
Windows 用来定位 DLL 的搜索路径
查看>>
常见的游戏设计技术
查看>>
Backbone 学习笔记五
查看>>
R语言:各种零碎
查看>>
Mysql5.7修改root密码
查看>>
docker入门3:基础操作(2)
查看>>
WC2019退役失败记
查看>>
Centos6.6下安装nginx1.6.3
查看>>
iOS开发之多线程
查看>>
[算法竞赛]第七章_暴力求解法
查看>>
关于全局替换空格,制表符,换行符
查看>>
MorkDown 常用语法总结
查看>>
自定义python web框架
查看>>
sqlserver生成随机数 2011-12-21 15:47 QQ空间
查看>>
jQuery禁止鼠标右键
查看>>
查询linux计算机的出口ip
查看>>
解决Android的ListView控件滚动时背景变黑
查看>>
laravel 多检索条件列表查询
查看>>