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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] ## <span style="font-size:15px">**一、testify庫說明**</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Testify是一個用于編寫測試的擴展包,它提供了一系列的斷言函數和輔助函數,可以幫助我們編寫更加簡潔、易讀、易維護的測試代碼。它構建在Golang的原生測試框架之上,提供了更高層次的抽象和易用性 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Github地址:`https://github.com/stretchr/testify` &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;官方文檔:`https://pkg.go.dev/github.com/stretchr/testify` ## <span style="font-size:15px">**二、安裝**</span> ``` go get github.com/stretchr/testify ``` ## <span style="font-size:15px">**三、assert 包:斷言工具包,輔助做測試結果判定**</span> 常用的斷言函數: * assert.Equal(t, expected, actual):驗證兩個值是否相等。 * assert.NotEqual(t, expected, actual):驗證兩個值是否不相等。 * assert.True(t, condition):驗證條件是否為真。 * assert.False(t, condition):驗證條件是否為假。 * assert.Nil(t, value):驗證值是否為nil。 * assert.NotNil(t, value):驗證值是否不為nil。 * assert.Equalf(t TestingT, expected interface{}, actual interface{}, format string, args ...interface{}):用于比較兩個值是否相等,并在比較失敗時提供自定義的錯誤信息 * assert.EqualValues(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}):用于比較兩個值是否相等 ``` package main import ( "github.com/stretchr/testify/assert" "testing" ) type Object struct { Value string } func TestSomething(t *testing.T) { var object = Object{Value: "Something"} assert := assert.New(t) assert.Equal(123, 123, "they should be equal") assert.NotEqual(123, 456, "they should not be equal") assert.Nil(nil) if assert.NotNil(object) { assert.Equal("Something", object.Value) } } ``` ## <span style="font-size:15px">**四、require 包:斷言工具包,輔助做測試結果判定**</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require包提供了與assert包相同的全局函數,但它們并沒有返回布爾結果,而是終止了當前測試。 ``` package main import ( "github.com/stretchr/testify/require" "testing" ) type Object struct { Value string } func TestSomething(t *testing.T) { assertions := require.New(t) assertions.Equal(123, 123, "they should be equal") assertions.NotEqual(123, 456, "they should not be equal") assertions.Nil(nil) } ``` ## <span style="font-size:15px">**五、mock 包**</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;使用Testify的模擬框架,我們可以創建一個模擬對象,并為其指定預期的行為,用這個模擬對象來替代真實的對象。 ### <span style="font-size:15px">**1、mock.Mock結構體函數**</span> ``` type Mock struct { ExpectedCalls []*Call // 表示預期的調用 Calls []Call // 表示實際發生的調用 } ``` | 結構體函數 | 說明 | | --- | --- | | func (m *Mock) **AssertCalled**(t TestingT, methodName string, arguments ...interface{}) bool | 檢查指定的方法是否被調用,并且可以驗證調用時傳入的參數是否符合預期 | | func (m *Mock) **AssertNotCalled**(t TestingT, methodName string, arguments ...interface{}) bool | 斷言未調用該方法 | | func (m *Mock) **AssertExpectations**(t TestingT) bool | 斷言模擬對象的期望行為是否被滿足,并返回斷言結果的布爾值。 | | func (m *Mock) **AssertNumberOfCalls**(t TestingT, methodName string, expectedCalls int) bool | 斷言該方法被調用了expectedCalls次| | func (m *Mock) **Called**(arguments ...interface{}) Arguments | 檢查模擬函數是否被調用,并返回傳遞給模擬函數的參數列表 | | func (m *Mock) **IsMethodCallable**(t TestingT, methodName string, arguments ...interface{}) bool | IsMethodCallable檢查是否可以調用該方法。如果調用方法的次數超過“Repeatability”,則返回false | | func (m *Mock) **MethodCalled**(methodName string, arguments ...interface{}) Arguments | 告訴mock對象已經調用了方法,并獲取要返回的參數數組 | | func (m *Mock) **On**(methodName string, arguments ...interface{}) *Call | 設置mock的方法和參數 | ### <span style="font-size:15px">**2、mock.Call結構體函數**</span> ``` type Call struct { Parent *Mock // 指向 Mock 結構體的指針,表示該調用所屬的模擬對象。 Method string // 被調用或將要被調用的方法的名稱。 Arguments Arguments // 方法的參數 ReturnArguments Arguments // 當調用該方法時應該返回的參數 Repeatability int // 設置期望時返回參數的次數。0 表示總是返回該值 WaitFor <-chan time.Time // 用于阻塞 Return 直到接收到消息或被關閉的通道。如果為 nil,則立即返回。 RunFn func(Arguments) // 用于處理傳遞的引用參數內容的處理程序。在模擬解碼器等方法時很有用。 PanicMsg *string // 用于模擬函數調用時的 panic 的消息。如果設置為非空字符串,則函數調用將會 panic,而不考慮其他設置。 } ``` <table> <thead> <th width="43%">結構體函數</th> <th width="32%">說明</th> <th width="25%">示例</th> </thead> <tbody> <tr> <td>func (c *Call) <strong>After</strong>(d time.Duration) *Call</td> <td>設置阻塞到調用返回的時間</td> <td> ``` Mock.On("MyMethod", arg1, arg2).After(time.Second) ``` </td> </tr> <tr> <td>func (c *Call) <strong>Maybe</strong>() *Call</td> <td>允許方法調用是可選的。在斷言期望值時,不調用可選方法不會導致錯誤</td> <td></td> </tr> <tr> <td>unc (c *Call) <strong>NotBefore</strong> (calls ...*Call) *Call</td> <td>指定某個方法在另一個方法之前被調用</td> <td> ``` Mock.On("Do").Return(nil).Notbefore(Mock.On("Init").Return(nil)) ``` </td> </tr> <tr> <td>func (c *Call) <strong>On</strong>(methodName string, arguments ...interface{}) *Call</td> <td>該方法的作用是設置對特定方法的調用期望,以便在后續的測試中驗證該方法是否按照期望被調用。</td> <td> ``` Mock.On("MyMethod", 1).Return(nil).On("MyOtherMethod", 'a', 'b', 'c').Return(errors.New("Some Error")) ``` </td> </tr> <tr> <td>func (c *Call) <strong>Once</strong>() *Call</td> <td>表示mock只應返回一次值</td> <td> ``` Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Once() ``` </td> </tr> <tr> <td>func (c *Call) <strong>Panic</strong>(msg string) *Call</td> <td>當出現panic時,提示msg</td> <td> ``` Mock.On("DoSomething").Panic("test panic") ``` </td> </tr> <tr> <td>func (c *Call) <strong>Return</strong>(returnArguments ...interface{}) *Call</td> <td>指定期望的返回值</td> <td> ``` Mock.On("DoSomething").Return(errors.New("failed")) ``` </td> </tr> <tr> <td>func (c *Call) <strong>Run</strong>(fn func(args Arguments)) *Call</td> <td>Run設置在返回之前要調用的處理程序</td> <td> ``` Mock.On("Unmarshal", AnythingOfType("*map[string]interface{}")).Return().Run(func(args Arguments) { arg := args.Get(0).(*map[string]interface{}) arg["foo"] = "bar" }) ``` </td> </tr> <tr> <td>func (c *Call) <strong>Times</strong>(i int) *Call</td> <td>表示mock應該只返回指示的次數</td> <td> ``` Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Times(5) ``` </td> </tr> <tr> <td>func (c *Call) <strong>Twice</strong>() *Call</td> <td>表示mock只應返回兩次值</td> <td> ``` Mock.On("MyMethod", arg1, arg2).Return(returnArg1, returnArg2).Twice() ``` </td> </tr> <tr> <td>func (c *Call) <strong>Unset</strong>() *Call</td> <td>清除mock</td> <td> ``` test.On("func", mock.Anything).Unset() ``` </td> </tr> <tr> <td>func (c *Call) <strong>WaitUntil</strong>(w <-chan time.Time) *Call</td> <td>阻塞等待,用于測試異步方法或需要等待一定時間后才能進行斷言</td> <td> ``` Mock.On("MyMethod", arg1, arg2).WaitUntil(time.After(time.Second)) ``` </td> </tr> </tbody> </table> ### <span style="font-size:15px">**3、示例**</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;假設有一個名為 calculator.go 的文件,其中包含一個簡單的計算器接口和實現: ``` // calculator.go package util type Calculator interface { Add(a, b int) int Subtract(a, b int) int } type MyCalculator struct{} func (c *MyCalculator) Add(a, b int) int { return a + b } func (c *MyCalculator) Subtract(a, b int) int { return a - b } ``` &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;創建一個名為 calculator_test.go 的測試文件。在這個示例中,我們創建了一個 MockCalculator 結構體,它嵌入了 `github.com/stretchr/testify/mock` 提供的 Mock 結構體。 ``` // calculator_test.go package main import ( "github.com/stretchr/testify/mock" "testing" ) type MockCalculator struct { mock.Mock } func (m *MockCalculator) Add(a, b int) int { // 重寫了 Calculator 接口的方法,并使用 m.Called 和 m.On 來設置模擬對象的行為和期望 args := m.Called(a, b) return args.Int(0) } func (m *MockCalculator) Subtract(a, b int) int { args := m.Called(a, b) return args.Int(0) } func TestCalculator_Add(t *testing.T) { mockCalculator := new(MockCalculator) mockCalculator.On("Add", 3, 4).Return(6) // 調用 result := mockCalculator.Add(3, 4) if result != 6 { t.Errorf("Expected result to be 7, but got %d", result) } mockCalculator.AssertExpectations(t) } ``` <p style="display:none"> ## <span style="font-size:15px">**六、suite 包**</span> https://blog.csdn.net/qq_30614345/article/details/131314038 </p>
                  <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>

                              哎呀哎呀视频在线观看