簡體   English   中英

如何測試 Go 的“測試”包功能?

[英]How do I test Go's "testing" package functions?

我正在編寫一個測試實用程序函數庫,我希望對它們進行測試。

一個這樣的函數的例子是:

func IsShwifty(t *testing.T, foo string) bool {
    result, err := bar.MyFunction(string)
    if err != nil {
      t.Error("Failed to get shwifty: " + foo, err)
    }
    return result == "shwifty"
}

我想編寫一個TestIsShwifty來輸入一些東西以使MyFunction返回錯誤,然后使t.Error 然后,我想讓TestIsShwifty通過類似的東西:

func TestIsShwifty(t *testing.T) {
  if doesError(t, IsShwifty(t)) == false {
    t.Error("Oh know! We didn't error!")
  }
}

這在 Go 中可能嗎?

我想到了!

我只需要創建一個單獨的testing.T實例。

func TestIsShwifty(t *testing.T) {
  newT := testing.T{}
  IsShwifty(newT)
  if newT.Failed() == false {
    t.Error("Test should have failed")
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM