<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## 使用表驅動測試 本節將演示如何編寫表驅動測試,收集測試覆蓋率并對其進行改進。還將使用 github.com/cweill/gotests 包來生成測試。 如果你一直在下載其他章節的測試代碼,這些代碼應該看起來非常熟悉。使用本節與前幾節的測試組合,應該能夠實現100%的測試覆蓋率。 ### 實踐 1. 獲取第三方庫: ``` go get github.com/cweill/gotests/ ``` 2. 建立 coverage.go: ``` package main import "errors" // Coverage 是一個具有一些分支條件的簡單函數 func Coverage(condition bool) error { if condition { return errors.New("condition was set") } return nil } ``` 3. 建立 coverage_test.go: 運行 ``` gotests -all -w ``` 這會生成: ``` package main import "testing" func TestCoverage(t *testing.T) { type args struct { condition bool } tests := []struct { name string args args wantErr bool }{ //TODO } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { if err := Coverage(tt.args.condition); (err != nil) != tt.wantErr { t.Errorf("Coverage() error = %v, wantErr %v", err, tt.wantErr) } }) } } ``` 4. 填充TODO部分: ``` {"no condition", args{true}, true}, ``` 5. 運行測試: ``` go test -cover PASS coverage: 66.7% of statements ok github.com/agtorre/go-cookbook/chapter8/coverage 0.007s ``` ``` go test -coverprofile=cover.out go tool cover -html=cover.out -o coverage.html ``` 打開coverage.html可以看到覆蓋率報告。 ### 說明 go test -cover命令附帶一個基本的Go安裝。它可用于收集Go應用程序的測試覆蓋率報告。此外,它還能夠輸出覆蓋率指標和HTML覆蓋率報告。此工具通常由其他工具包裝,將在下一節中介紹。 https//github.com/golang/go/wiki/TableDrivenTests 涵蓋了這些表驅動的測試樣例,可以在不編寫大量額外代碼的情況下完成可以處理許多情況的干凈測試。 首先自動生成測試代碼,然后根據需要填寫測試用例以幫助創建更多的覆蓋范圍。在調用非變量函數或方法時,或測試輸入和輸出的許多變化,可能很難達到100%的測試覆蓋率,在這樣的情況下,模糊測試會變得很有用。 * * * * 學識淺薄,錯誤在所難免。歡迎在群中就本書提出修改意見,以饗后來者,長風拜謝。 Golang中國(211938256) beego實戰(258969317) Go實踐(386056972)
                  <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>

                              哎呀哎呀视频在线观看