新聞中心
大家好,我是 polarisxu。

Go1.17 預(yù)計(jì)在 8 月份發(fā)布。目前 tip.golang.org 可以瀏覽 Go1.17 的相關(guān)內(nèi)容,https://tip.golang.org/doc/go1.17 也有了 Go1.17 相關(guān)改動(dòng)的部分文檔。這段時(shí)間,我會(huì)陸續(xù)給大家分享 Go1.17 中相關(guān)的新特性,提前學(xué)習(xí)。。。好吧,提前卷了~
今天先聊聊在測(cè)試中增加的隨機(jī)化 flag:shuffle。
01 安裝 tip 版本
由于 Go1.17 還未發(fā)布,因此為了體驗(yàn)它的新特性,我們需要安裝 tip 版本。這是一個(gè)正在開(kāi)發(fā)的版本,也就是倉(cāng)庫(kù)的 master 分支代碼。因此,我們需要通過(guò)源碼編譯安裝。
這里我使用 goup 這個(gè)管理工具進(jìn)行安裝:
- $ goup install tip
安裝成功后,查看版本信息(你看到的大概率和我的不一樣):
- $ go version
- go version devel go1.17-1607c28172 Sun May 30 02:37:38 2021 +0000 darwin/amd64
02 新的 shuffle flag
安裝完 tip 版本后,執(zhí)行如下命令:
- $ go help testflag
然后找到下面這個(gè) flag:
- -shuffle off,on,N
- Randomize the execution order of tests and benchmarks.
- It is off by default. If -shuffle is set to on, then it will seed
- the randomizer using the system clock. If -shuffle is set to an
- integer N, then N will be used as the seed value. In both cases,
- the seed will be reported for reproducibility.
這是 Go1.17 新增的,提交的代碼見(jiàn):https://golang.org/cl/310033。
從名稱(chēng)可以看出,這是控制測(cè)試執(zhí)行順序是否隨機(jī)的 flag。它有三個(gè)值:off、on 和 N,其中默認(rèn)是 off,即不啟用隨機(jī),這相當(dāng)于 Go1.17 版本之前的測(cè)試行為。而 on 表示啟用 shuffle,那 N 是什么意思?它也表示啟用隨機(jī)。on 和 N 的區(qū)別解釋下:
因?yàn)槭请S機(jī),就涉及到隨機(jī)種子(seed)問(wèn)題。當(dāng)值是 on 時(shí),隨機(jī)數(shù)種子使用系統(tǒng)時(shí)鐘;如果值是 N,則直接用這個(gè) N 當(dāng)做隨機(jī)數(shù)種子。注意 N 是整數(shù)。
當(dāng)測(cè)試失敗時(shí),如果啟用了 shuffle,這個(gè)種子會(huì)打印出來(lái),方便你重現(xiàn)之前測(cè)試場(chǎng)景。
03 例子體驗(yàn)下
創(chuàng)建一個(gè)包 calc,增加「加減乘除」四個(gè)函數(shù):
- func Add(x, y int) int {
- return x + y
- }
- func Minus(x, y int) int {
- return x - y
- }
- func Mul(x, y int) int {
- return x * y
- }
- func Div(x, y int) int {
- return x / y
- }
并為這四個(gè)函數(shù)寫(xiě)好單元測(cè)試(代碼太長(zhǎng),這里只列出 Add 的,寫(xiě)法不重要,按你喜歡的方式寫(xiě)單元測(cè)試即可):
- func TestAdd(t *testing.T) {
- type args struct {
- x int
- y int
- }
- tests := []struct {
- args args
- want int
- }{
- {
- args{1, 2},
- 3,
- },
- {
- args{-1, 3},
- 3, // 特意構(gòu)造一個(gè) failure 的 case
- },
- }
- for _, tt := range tests {
- if got := Add(tt.args.x, tt.args.y); got != tt.want {
- t.Errorf("Add() = %v, want %v", got, tt.want)
- }
- }
- }
然后運(yùn)行單元測(cè)試(不加 shuffle flag):
- $ go test -v ./...
- === RUN TestAdd
- calc_test.go:27: Add() = 2, want 3
- --- FAIL: TestAdd (0.00s)
- === RUN TestMinus
- --- PASS: TestMinus (0.00s)
- === RUN TestMul
- --- PASS: TestMul (0.00s)
- === RUN TestDiv
- --- PASS: TestDiv (0.00s)
- FAIL
- FAIL test/shuffle 0.441s
- FAIL
多次運(yùn)行,發(fā)現(xiàn)執(zhí)行順序都是你文件中寫(xiě)好的單元測(cè)試順序,我這里是 Add、Minus、Mul、Div。
加上 shuffle flag 后運(yùn)行:
- $ go test -v -shuffle=on ./...
- -test.shuffle 1622383890431866000
- === RUN TestMul
- --- PASS: TestMul (0.00s)
- === RUN TestDiv
- --- PASS: TestDiv (0.00s)
- === RUN TestAdd
- calc_test.go:27: Add() = 2, want 3
- --- FAIL: TestAdd (0.00s)
- === RUN TestMinus
- --- PASS: TestMinus (0.00s)
- FAIL
- FAIL test/shuffle 0.177s
- FAIL
輸出有兩處變化:
- 多了 -test.shuffle 1622383890431866000,即上面說(shuō)到的種子。如果不是 on 而是 N,則這里的值就是 N 的值;
- 順序不確定。你多次運(yùn)行,發(fā)現(xiàn)每次順序可能不一樣;
順便提一句,對(duì)于 benchmark,shuffle 這個(gè) flag 也是適用的。
04 有什么用
有人可能會(huì)問(wèn),這個(gè)玩意有啥用?
確實(shí),大部分時(shí)候這個(gè)特性沒(méi)啥用。但如果你不希望測(cè)試之間有依賴關(guān)系,而擔(dān)心實(shí)際上依賴了,可以加上這個(gè) flag,以便發(fā)現(xiàn)潛在的問(wèn)題。
其實(shí),這個(gè) flag 早在 2015 年 bradfitz 就提 issue 建議加上,原計(jì)劃在 Go1.6 加上的,但沒(méi)有人寫(xiě)提案,因此擱置了。6 年過(guò)去了,才加上該功能,可見(jiàn)需求不強(qiáng)烈。日常工作中,你大概率也不會(huì)用到,但知曉有這么個(gè)東西還是有用處的,萬(wàn)一需要時(shí),可以用上。
本文轉(zhuǎn)載自微信公眾號(hào)「polarisxu」,可以通過(guò)以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系polarisxu公眾號(hào)。
當(dāng)前名稱(chēng):Go1.17這個(gè)新特性竟然是6年前提出來(lái)的
文章URL:http://m.fisionsoft.com.cn/article/dheeisd.html


咨詢
建站咨詢
