gofunc - 簡單好用的go test工具

gofunc

簡單好用的go test工具

使用場景

用於快速測試某個函數或表達式,比如有個函數用於刪除slice裏的一個元素:

func DeleteSliceInt(data []int, i int) []int {
    return append(data[:i], data[i+1:]...)
}

正常的單元測試需要新建 xxx_test.go 文件,編寫 Testxxx 函數,這樣操作有時候過於繁瑣。

爲簡化步驟,開發了工具命令 gofunc ,在命令行模式下調用 DeleteSliceInt 實現單元測試,並輸出返回值:

$ gofunc -r 'DeleteSliceInt([]int{0,1,2,3,4,5}, 3)'
>> [0 1 2 4 5]
PASS
ok      0.089s

使用方法

  • git clone到本地,執行go build,得到exe
  • 複製 gofunc 程序到系統bin目錄
  • 切換到測試函數所在目錄,執行gofunc命令
- 若測試函數
- 無參數,無返回值: gofunc yourFuncName
- 有參數,無返回值: gofunc 'yourFuncName(arg1, arg2)'
- 無參數,有返回值: gofunc -r yourFuncName
- 有參數,有返回值: gofunc -r 'yourFuncName(arg1, arg2)'

表達式測試

$ gofunc -r 'time.Now().Format("2006-01-02 15:04:05")'
>> 2020-04-15 14:01:48
$ gofunc -r 'int(math.Floor(float64(59)/60 + 0.5))'
>> 1
$ gofunc -r "strconv.FormatFloat(3.1415926, 'f', 2, 64)"
>> 3.14
$ gofunc -r 'strings.ReplaceAll("who make test easier", "who", "gofunc")'
>> gofunc make test easier

目前僅支持fmt、time、math、strconv、strings包
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章