golang test测试实例

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

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

准备工作:

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

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

1 @H_403_15@
2 @H_403_15@
3 @H_403_15@
4 @H_403_15@
5 @H_403_15@
6 @H_403_15@
7 @H_403_15@
8 @H_403_15@
9 @H_403_15@
10 @H_403_15@
11 @H_403_15@
12 @H_403_15@
drop table if exists admin; @H_403_15@
CREATE TABLE `admin` ( @H_403_15@
`adminid` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, @H_403_15@
`username` varchar(20) NOT NULL DEFAULT '' COMMENT '后台用户名' , @H_403_15@
`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, @H_403_15@
PRIMARY KEY(`adminid`) @H_403_15@
) @H_403_15@
COMMENT= '后台用户信息表' @H_403_15@
COLLATE= 'utf8_general_ci' @H_403_15@
ENGINE=InnoDB; @H_403_15@
@H_403_15@
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' ; @H_403_15@ @H_403_15@
@H_403_15@ @H_403_15@

3 gopath下建立myMysqL

4 myMysqL.go的代码

12 @H_403_15@
13 @H_403_15@
14 @H_403_15@
15 @H_403_15@
16 @H_403_15@
17 @H_403_15@
18 @H_403_15@ @H_502_141@ 19 @H_403_15@
20 @H_403_15@
21 @H_403_15@
22 @H_403_15@
23 @H_403_15@
24 @H_403_15@
25 @H_403_15@
package myMysqL @H_403_15@
import( @H_403_15@
"log" @H_403_15@
"github.com/ziutek/myMysqL/MysqL" @H_403_15@
_ "github.com/ziutek/myMysqL/native" @H_403_15@
) @H_403_15@
func getAdmin(adminid int) (string,string){ @H_403_15@
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" ) @H_403_15@
err := db.Connect() @H_403_15@
err != nil { @H_403_15@
panic(err) @H_403_15@
} @H_403_15@
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) @H_403_15@
err != nil { @H_403_15@
panic(err) @H_403_15@
} @H_403_15@
len(rows) < 1 { @H_403_15@
log.Panic( "rows error" ) @H_403_15@
} @H_403_15@
row := rows[0] @H_403_15@
first := res.Map( "username" ) @H_403_15@
second := res.Map( ) @H_403_15@
username,password := row.Str(first),row.Str(second) @H_403_15@
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 @H_403_15@
} @H_403_15@ @H_403_15@
@H_403_15@ @H_403_15@

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

5 myMysqL_test.go的代码

10 @H_403_15@
"testing" @H_403_15@
) @H_403_15@
func Test_getAdmin(t *testing.T) { @H_403_15@
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) @H_403_15@
(username != "admin" ) { @H_403_15@
t.Error( "getAdmin get data error" } @H_403_15@
}<br>这里做单元测试的,测试getAdmin函数 @H_403_15@ @H_403_15@
@H_403_15@ @H_403_15@

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

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

6 下面做性能测试

myMysqL_b_test.go的代码

9 @H_403_15@
import ( @H_403_15@
func Benchmark_getAdmin(b *testing.B){ @H_403_15@

猜你在找的Go相关文章