<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                o語言自帶了 testing 測試包,可以進行自動化的單元測試,輸出結果驗證,并且可以測試性能。 ## 為什么需要測試 完善的測試體系,能夠提高開發的效率,當項目足夠復雜的時候,想要保證盡可能的減少 bug,有兩種有效的方式分別是代碼審核和測試,Go語言中提供了 testing 包來實現單元測試功能。 ## 測試規則 要開始一個單元測試,需要準備一個 go 源碼文件,在命名文件時文件名必須以`_test.go`結尾,單元測試源碼文件可以由多個測試用例(可以理解為函數)組成,每個測試用例的名稱需要以 Test 為前綴,例如: ~~~ func TestXxx( t \*testing.T ){ //...... } ~~~ 編寫測試用例有以下幾點需要注意: * 測試用例文件不會參與正常源碼的編譯,不會被包含到可執行文件中; * 測試用例的文件名必須以`_test.go`結尾; * 需要使用 import 導入 testing 包; * 測試函數的名稱要以`Test`或`Benchmark`開頭,后面可以跟任意字母組成的字符串,但第一個字母必須大寫,例如 TestAbc(),一個測試用例文件中可以包含多個測試函數; * 單元測試則以`(t *testing.T)`作為參數,性能測試以`(t *testing.B)`做為參數; * 測試用例文件使用`go test`命令來執行,源碼中不需要 main() 函數作為入口,所有以`_test.go`結尾的源碼文件內以`Test`開頭的函數都會自動執行。 Go語言的 testing 包提供了三種測試方式,分別是單元(功能)測試、性能(壓力)測試和覆蓋率測試。 ## 單元(功能)測試 在同一文件夾下創建兩個Go語言文件,分別命名為 demo.go 和 demt\_test.go,如下圖所示: ![](http://c.biancheng.net/uploads/allimg/191015/4-19101516392LR.gif) 具體代碼如下所示: #### demo.go: ~~~ package demo // 根據長寬獲取面積 func GetArea(weight int, height int) int { return weight * height } ~~~ #### demo\_test.go: ~~~ package demo import "testing" func TestGetArea(t *testing.T) { area := GetArea(40, 50) if area != 2000 { t.Error("測試失敗") } } ~~~ ~~~ 執行測試命令,運行結果如下所示: PS D:\\code> go test -v === RUN TestGetArea --- PASS: TestGetArea (0.00s) PASS ok _/D_/code 0.435s ~~~ ## 性能(壓力)測試 將 demo\_test.go 的代碼改造成如下所示的樣子: ~~~ package demo import "testing" func BenchmarkGetArea(t *testing.B) { for i := 0; i < t.N; i++ { GetArea(40, 50) } } ~~~ 執行測試命令,運行結果如下所示: ~~~ PS D:\\code> go test -bench="." goos: windows goarch: amd64 BenchmarkGetArea-4 2000000000 0.35 ns/op PASS ok _/D_/code 1.166s ~~~ 上面信息顯示了程序執行?2000000000 次,共耗時 0.35 納秒。 ## 覆蓋率測試 覆蓋率測試能知道測試程序總共覆蓋了多少業務代碼(也就是 demo\_test.go 中測試了多少 demo.go 中的代碼),可以的話最好是覆蓋100%。 將 demo\_test.go 代碼改造成如下所示的樣子: ~~~ package demo import "testing" func TestGetArea(t *testing.T) { area := GetArea(40, 50) if area != 2000 { t.Error("測試失敗") } } func BenchmarkGetArea(t *testing.B) { for i := 0; i < t.N; i++ { GetArea(40, 50) } } ~~~ 執行測試命令,運行結果如下所示: ~~~ PS D:\\code> go test -cover PASS coverage: 100.0% of statements ok _/D_/code 0.437s ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看