golang test測試例項

2021-09-22 13:11:48 字數 4180 閱讀 3814

本文的目的是對mymysql進行單元測試和效能測試

準備工作:

1

2

3

4

5

6

7

8

9

10

11

12

drop tableifexists 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''comment'密碼,md5存',

primary key(`adminid`)

)

comment='後台使用者資訊表'

很好理解,根據adminid獲取使用者名稱和密碼

1

2

3

4

5

6

7

8

9

10

package mymysql

import(

"testing"

)

func test_getadmin(t *testing.t)

}

這裡做單元測試的,測試getadmin函式

寫到這裡你就可以在命令列中執行go test了

這裡有個 -v引數,如果不加這個引數的話,只會顯示錯誤的測試用例,否則就顯示所有的測試用例(成功 + 錯誤)

mymysql_b_test.go的**:

1

2

3

4

5

6

7

8

9

package mymysql

import (

"testing"

)

func benchmark_getadmin(b *testing.b)

}

然後執行 go test -v -bench=".*"

這裡的-bench是可以指定執行的用例

返回結果表示這個測試用例在1s中內執行了2000次,每次呼叫大約用了891898ns

使用命令:

go test -bench=".*" -cpuprofile=cpu.prof -c

cpuprofile是表示生成的cpu profile檔案

-c是生成可執行的二進位制檔案,這個是生成狀態圖必須的,它會在本目錄下生成可執行檔案mymysql.test

然後使用go tool pprof工具

go tool pprof mymysql.test cpu.prof

呼叫web(需要安裝graphviz)

顯示svg檔案已經生成了

golang test測試例項

golang test測試例項 本文的目的是對mymysql進行單元測試和效能測試 準備工作 drop table if exists admin create table admin adminid int 10 unsigned not null auto increment,username ...

golang test測試例項

本文的目的是對mymysql進行單元測試和效能測試 準備工作 drop table if exists admin create table admin adminid int 10 unsigned not null auto increment,username varchar 20 not n...

Monkey測試例項

monkey是android中的乙個命令列工具,可以執行在模擬器裡或實際裝置中。它向系統傳送偽隨機的使用者事件流 如按鍵輸入 觸控螢幕輸入 手勢輸入等 實現對正在開發的應用程式進行壓力測試。monkey測試是一種為了測試軟體的穩定性 健壯性的快速有效的方法。1,環境檢查 先檢查電腦adb有沒有,在執...