Go 測試單個方法


1.目錄

gotest.go

package mytest

import (
    "errors"
)

func Division(a, b float64) (float64, error) {
    if b == 0 {
        return 0, errors.New("除數不能為0")
    }
    return a / b, nil
}

gotest_test.go

package mytest

import (
    "testing"
)

func Test_Division_1(t *testing.T) {
    if i, e := Division(6, 2); i != 3 || e != nil { //try a unit test on function
        t.Error("除法函數測試沒通過") // 如果不是如預期的那么就報錯
    } else {
        t.Log("第一個測試通過了") //記錄一些你期望記錄的信息
    }
}


func Test_Division_2(t *testing.T) {
    if _, e := Division(6, 0); e == nil { //try a unit test on function
        t.Error("Division did not work as expected.") // 如果不是如預期的那么就報錯
    } else {
        t.Log("one test passed.", e) //記錄一些你期望記錄的信息
    }
}

1. 在目錄下執行 go test  是測試目錄所有以XXX_test.go 結尾的文件。

2.測試單個方法  下面2種寫法。

  go test -test.v  -test.run="Test_Division_1" -test.count 5
      go test -v  -run="Test_Division_1" -count 5
3.查看幫助 go test -help  

 

 

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM