單元測試,是指對軟體中的最小可測試單元進行檢查和驗證
#單元就是人為規定的最小的被測功能模組
#一般來說,要根據實際情況去判定其具體含義,如C 語言中單元指一個函數,Go 裡面也單元也是一個函數
單元測試是在軟體開發過程中要進行的最低級別的測試活動,軟體的獨立單元將在與程式的其他部分相隔離的情況下進行測試。
單元測試,咱們平常也叫它單測,平時開發的時候,也需要寫一些demo 來測試我們的專案中的函數或某個小功能【建議:golang教學】
go test 單元測試
GO 語言裡面的單元測試,是使用標準函式庫testing
有以下簡單規則:
- 匯入test 標準函式庫
- 單測檔名,後面跟上
_test
- 單測檔中的函數名為Test開頭,且參數必須是
t *testing.T
簡單範例:
寫一個簡單的例子,加入後綴和前綴
.├── cal.go ├── cal_test.go ├── lll └── sub.go
cal.go
package mainfunc Addprefix(str string) string { return "hello_"+str}func Addsuffix(str string) string { return str+"_good"}
cal_test.go
package mainimport "testing"func TestAddprefix(t *testing.T) { Addprefix("xiaomotong")}func TestAddsuffix(t *testing.T) { Addsuffix("xiaomotong")}
#sub. go
package mainfunc MyAdd(a int, b int) int { if a+b > 10{ return 10 } return a+b}func MySub(one int, two int) int{ if one - two <p><strong>sub_test.go</strong></p><pre class="brush:php;toolbar:false">package mainimport "testing"import "fmt"func TestMyAdd(t *testing.T) { num := MyAdd(4 ,9) fmt.Println(num) num = MyAdd(4 ,2) fmt.Println(num)}
執行單元測試
go test -v
- -v
-v 是參數會顯示每個用例的測試結果,顯示執行的單測函數,是否通過以及單測的時候
運行結果如下
=== RUN TestAddprefix --- PASS: TestAddprefix (0.00s)=== RUN TestAddsuffix --- PASS: TestAddsuffix (0.00s)=== RUN TestMyAdd 10 6 --- PASS: TestMyAdd (0.00s)PASS ok my_new_first/golang_study/later_learning/gotest 0.002s
在套件目錄下執行go test ,會執行套件裡面所有的單元測試檔
只執行指定的單測函數
咱們可以這樣用:
go test -run TestMyAdd -v
#結果如下:
=== RUN TestMyAdd 10 6 --- PASS: TestMyAdd (0.00s)PASS ok my_new_first/golang_study/later_learning/gotest 0.002s
##子測試
就是在我們寫的單測函數中,呼叫testing 套件裡的Run 方法,跑子測試咱們改造一下上述的sub_test.go
package mainimport "testing"import "fmt"func TestMyAdd(t *testing.T) { num := MyAdd(4 ,9) fmt.Println(num) num = MyAdd(4 ,2) fmt.Println(num)}func TestMySub(t *testing.T) { t.Run("one", func(t *testing.T) { if MySub(2, 3) != 1 { t.Fatal("cal error") } }) t.Run("two", func(t *testing.T) { if MySub(3, 1) != 2 { t.Fatal(" error ") } })}單獨呼叫子測試函數,執行
go test -run TestMySub/one -v
=== RUN TestMySub=== RUN TestMySub/one --- PASS: TestMySub (0.00s) --- PASS: TestMySub/one (0.00s)PASS ok my_new_first/golang_study/later_learning/gotest 0.003s
產生報告,計算覆蓋率
- 產生覆蓋率報告檔
go test -v -covermode=count -coverprofile=cover.out
在瀏覽器中開啟html 文件,可以查看到如下報告
圖中綠色的部分是已覆蓋,紅色的部分是未覆蓋,咱們的例子已經全部覆蓋具體的函數功能
go test 後面的指令,咱們也可以看幫助文件
很多公司都開始搞效能了,單測,自動化測試,CI/CD 都是要趕緊搞起來的,最好是做成一鍵發布一鍵回滾的。羨慕這些基礎設定都非常完善的地方,哈哈哈~
以上是一文搞懂GO中的單元測試(unit testing)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

goroutinesarefunctionsormethodsthatruncurranceingo,啟用效率和燈威量。 1)shememanagedbodo'sruntimemultimusingmultiplexing,允許千sstorunonfewerosthreads.2)goroutinessimproverentimensImproutinesImproutinesImproveranceThroutinesImproveranceThrountinesimproveranceThroundinesImproveranceThroughEasySytaskParallowalizationAndeff

purposeoftheInitfunctionoIsistoInitializeVariables,setUpConfigurations,orperformneccesSetarySetupBeforEtheMainFunctionExeCutes.useInitby.UseInitby:1)placingitinyourcodetorunautoamenationally oneraty oneraty oneraty on inity in ofideShortAndAndAndAndForemain,2)keepitiTshortAntAndFocusedonSimImimpletasks,3)

Gointerfacesaremethodsignaturesetsthattypesmustimplement,enablingpolymorphismwithoutinheritanceforcleaner,modularcode.Theyareimplicitlysatisfied,usefulforflexibleAPIsanddecoupling,butrequirecarefulusetoavoidruntimeerrorsandmaintaintypesafety.

在Go中使用recover()函數可以從panic中恢復。具體方法是:1)在defer函數中使用recover()捕獲panic,避免程序崩潰;2)記錄詳細的錯誤信息以便調試;3)根據具體情況決定是否恢復程序執行;4)謹慎使用,以免影響性能。

本文討論了使用GO的“字符串”軟件包進行字符串操作,詳細介紹了共同的功能和最佳實踐,以提高效率並有效地處理Unicode。

本文詳細介紹了GO的“時間”包用於處理日期,時間和時區,包括獲得當前時間,創建特定時間,解析字符串以及測量經過的時間。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

禪工作室 13.0.1
強大的PHP整合開發環境