[英]How do I test Go's "testing" package functions?
I'm writing a library of testing utility functions and I would like them to be themselves tested.我正在编写一个测试实用程序函数库,我希望对它们进行测试。
An example of one such function is:一个这样的函数的例子是:
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"
}
I would like to write a TestIsShwifty
that feeds in something to make MyFunction
return an error and then make t.Error
.我想编写一个
TestIsShwifty
来输入一些东西以使MyFunction
返回错误,然后使t.Error
。 Then, I want to have the TestIsShwifty
pass with something like:然后,我想让
TestIsShwifty
通过类似的东西:
func TestIsShwifty(t *testing.T) {
if doesError(t, IsShwifty(t)) == false {
t.Error("Oh know! We didn't error!")
}
}
Is this possible in Go?这在 Go 中可能吗?
I figured it out!我想到了!
I just needed to create a separate instance of testing.T
.我只需要创建一个单独的
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.