golang test测试实例

前端之家收集整理的这篇文章主要介绍了golang test测试实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文的目的是对myMysqL进行单元测试和性能测试

准备工作:

1 go get github.com/ziutek/myMysqL/thrsafe

2 在MysqL建表和初始化数据(db是test)

1
2
3
4
5
6
7
8
9
10
11
12
@H_403_40@
drop table if exists admin;
CREATE TABLE `admin` (
`adminid` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL DEFAULT '' COMMENT '后台用户名' ,
`password` char(32) NOT NULL DEFAULT '密码,md5存' PHP plain" style="white-space:pre-wrap; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.8em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
PRIMARY KEY(`adminid`)
)
COMMENT= '后台用户信息表'
COLLATE= 'utf8_general_ci'
ENGINE=InnoDB;
insert into admin set adminid=1,username= 'admin' PHP plain" style="white-space:pre-wrap; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.8em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,password= '21232f297a57a5a743894a0e4a801fc3' ;

3 gopath下建立myMysqL

4 myMysqL.go的代码

12
13
14
15
16
17
18
19
20
21
22
23
24
25
package myMysqL
import(
"log"
"github.com/ziutek/myMysqL/MysqL"
_ "github.com/ziutek/myMysqL/native"
)
func getAdmin(adminid int) (string,string){
db := MysqL.New( "tcp" PHP plain" style="white-space:pre-wrap; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.8em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas, "" "127.0.0.1:3306" "root" "password" "test" )
err := db.Connect()
err != nil {
panic(err)
}
rows,res,err := db.Query( "select * from admin where adminid=%d" PHP plain" style="white-space:pre-wrap; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.8em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,adminid)
err != nil {
panic(err)
}
len(rows) < 1 {
log.Panic( "rows error" )
}
row := rows[0]
first := res.Map( "username" )
second := res.Map( )
username,password := row.Str(first),row.Str(second)
return PHP plain" style="white-space:pre-wrap; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.8em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,password
}

很好理解,根据adminid获取用户名和密码

5 myMysqL_test.go的代码

10
"testing"
)
func Test_getAdmin(t *testing.T) {
PHP spaces" style="white-space:pre-wrap; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.8em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,_ := getAdmin(1)
(username != "admin" ) {
t.Error( "getAdmin get data error" }
}<br>这里做单元测试的,测试getAdmin函数

写到这里你就可以在命令行中运行go test了

这里有个 -v参数,如果不加这个参数的话,只会显示错误的测试用例,否则就显示所有的测试用例(成功 + 错误

6 下面做性能测试

myMysqL_b_test.go的代码

9
import (
func Benchmark_getAdmin(b *testing.B){
for i := 0; i < b.N; i++ { //use b.N for looping
getAdmin(1)
}

猜你在找的Go相关文章