<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 功能強大 支持多語言、二開方便! 廣告
                ## 使用第三方測試工具 Go測試有許多有用的工具。 這些工具可以讓你更容易地了解每個功能級別的代碼覆蓋率,還可以使用斷言工具來減少測試代碼行。 本文將介紹 github.com/axw/gocov 和github.com/smartystreets/goconvey 軟件包,以展示其中的一些功能。此外 github.com/smartystreets/goconvey 包支持斷言和運行時測試。 ### 實踐 1.獲取第三方庫: ``` go get github.com/axw/gocov go get github.com/smartystreets/goconvey/ ``` 2. 建立 funcs.go: ``` package tools import ( "fmt" ) func example() error { fmt.Println("in example") return nil } var example2 = func() int { fmt.Println("in example2") return 10 } ``` 3. 建立 structs.go: ``` package tools import ( "errors" "fmt" ) type c struct { Branch bool } func (c *c) example3() error { fmt.Println("in example3") if c.Branch { fmt.Println("branching code!") return errors.New("bad branch") } return nil } ``` 4. 建立 funcs_test.go: ``` package tools import ( "testing" . "github.com/smartystreets/goconvey/convey" ) func Test_example(t *testing.T) { tests := []struct { name string }{ {"base-case"}, } for _, tt := range tests { Convey(tt.name, t, func() { res := example() So(res, ShouldBeNil) }) } } func Test_example2(t *testing.T) { tests := []struct { name string }{ {"base-case"}, } for _, tt := range tests { Convey(tt.name, t, func() { res := example2() So(res, ShouldBeGreaterThanOrEqualTo, 1) }) } } ``` 5. 建立 structs_test.go: ``` package tools import ( "testing" . "github.com/smartystreets/goconvey/convey" ) func Test_c_example3(t *testing.T) { type fields struct { Branch bool } tests := []struct { name string fields fields wantErr bool }{ {"no branch", fields{false}, false}, {"branch", fields{true}, true}, } for _, tt := range tests { Convey(tt.name, t, func() { c := &c{ Branch: tt.fields.Branch, } So((c.example3() != nil), ShouldEqual, tt.wantErr) }) } } ``` 6. 運行: ``` $ gocov test | gocov report ok github.com/agtorre/go-cookbook/chapter8/tools 0.006s coverage: 100.0% of statements github.com/agtorre/go-cookbook/chapter8/tools/struct.go c.example3 100.00% (5/5) github.com/agtorre/go-cookbook/chapter8/tools/funcs.go example 100.00% (2/2) github.com/agtorre/go-cookbook/chapter8/tools/funcs.go @12:16 100.00% (2/2) github.com/agtorre/go-cookbook/chapter8/tools ---------- 100.00% (9/9) Total Coverage: 100.00% (9/9) ``` 7. 執行goconvey命令,會在瀏覽器中顯示: ![](https://box.kancloud.cn/21b9c114362d1c98846b13801bd9e9e7_712x367.jpg) ### 說明 本節演示了如何使用goconvry。與前面的小節相比,Convey關鍵字基本替代了t.Run,并且可以生成在goconvey生成的UI中顯示的標簽,但與t,Run的表現有所不同。如果你有嵌套的這樣測試塊: ``` Convey("Outer loop", t, func(){ a := 1 Convey("Inner loop", t, func() { a = 2 }) Convey ("Inner loop2", t, func(){ fmt.Println(a) }) }) ``` 使用goconvey命令,將打印1。如果我們使用t.Run,將打印2。換句話說,t.Run按順序運行測試,永遠不會重復。此行為對于將變量設置在外部代碼塊中非常有用。如果混合使用,就必須記住這種區別。 在使用convey斷言時,在UI中有成功的復選標記和其他統計信息。使用它還可以減少if檢查單行的大小,甚至可以創建自定義斷言。 如果保留goconvey Web界面并打開通知,則在保存代碼時,將自動運行測試,并且將收到有關任何增加或減少覆蓋范圍以及構建失敗時的通知。 以上功能都可以單獨或一起使用。 在努力提高測試覆蓋率時,gocov工具非常有用。它可以快速識別尚未測試的函數,并幫助了解覆蓋率報告。此外,gocov可用于生成使用 github.com/matm/gocov-html 包隨Go代碼一起提供的備用HTML報告。 * * * * 學識淺薄,錯誤在所難免。歡迎在群中就本書提出修改意見,以饗后來者,長風拜謝。 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>

                              哎呀哎呀视频在线观看