Head First Go

编程入门 行业动态 更新时间:2024-10-11 09:29:59

<a href=https://www.elefans.com/category/jswz/34/1761207.html style=Head First Go"/>

Head First Go

文章目录

  • 前言
  • 1. 一个应该有自动化测试的函数
  • 2. 编写测试
  • 3. 使用 go test
  • 4. 现在使用test来测试我们的拼接函数
  • 5. 使用Errorf获取更详细的测试失败信息
  • 6. 抽离出函数
  • 7. 修复字符串拼接函数
  • 8. 运行特定的测试集


前言

自动化测试能够测试一个程序是否能正常运行,避免程序上线之后出现的各种问题


提示:以下是本篇文章正文内容,下面案例可供参考

1. 一个应该有自动化测试的函数

首先我们来写一个函数,这个函数的作用是把几个字符串连接成一个大的字符串

//join.go
package proseimport "strings"func JoinWithCommas(phrases []string) string {result := strings.Join(phrases[:len(phrases)-1], ", ")	//对于第一个集合里面的参数使用第二个参数进行连接result += ", and " //在最后一个单词之前插入andresult += phrases[len(phrases)-1]return result
}
package mainimport ("fmt""mycode/prose"
)func main() {phrases := []string{"my parents", "a rodeo clown"}fmt.Println("A photo of", prose.JoinWithCommas(phrases))//A photo of my parents, and a rodeo clownphrases = []string{"my parents", "a rodeo clown", "a prize bull"}fmt.Println("A photo of", prose.JoinWithCommas(phrases))//A photo of my parents, a rodeo clown, and a prize bull
}

其实对于三个的情况是没问题的,但是对于两个参数,就有问题了,我举个例子:比如传入的参数是a,b,c,那么组成的string就是 this is a, b, and c,而传入a,b的时候组成的参数是this is a and b,但是上面的例子很明显不符合两个参数的需求,因为多了一个逗号,但是去掉逗号又不满足三个参数的需求,所以我们可以通过自动化测试来测试出这些问题



2. 编写测试

Go包含一个testing包,可以用来进行自动化测试,还有一个go test命令。

package mainimport "testing"func TestTwoElements(t *testing.T){t.Error()
}func TestThreeElements(t *testing.T){t.Error("no test here either")
}
  • go 的Test函数以Test开头
  • 不需要把测试和正在测试的代码放在一个包中,但是如果需要导出,就要放到同一个包
  • 测试需要使用testing包中的类型,需要导入
  • 测试函数应该接收单个参数:一个执行testing.T的指针
  • 可以通过testing.T值调用方法例如(Error)来报告测试失败



3. 使用 go test

go test + 包名可以测试go root下面某个包的go测试文件

package mainimport "testing"func TestTwoElements(t *testing.T){t.Error()
}func TestThreeElements(t *testing.T){t.Error("no test here either")
}

删除掉Error,就可以看到测试成功了



4. 现在使用test来测试我们的拼接函数

package mainimport ("mycode/prose""testing"
)func TestTwoElements(t *testing.T) {list := []string{"apple", "orange"}s := prose.JoinWithCommas(list)if s != "apple and orange" {t.Error("didn't match expected value - 1")}
}func TestThreeElements(t *testing.T) {list := []string{"apple", "orange", "pear"}s := prose.JoinWithCommas(list)if s != "apple, orange, and pear" {t.Error("didn't match expected value - 2")}
}


可以看到经过测试,第一个方法就发生了错误,这样我们就可以看到传入两个参数是不符合我们的要求的。



5. 使用Errorf获取更详细的测试失败信息

Errorf 函数接收一个带格式化动词的字符串,就像fmt.Printf 和 fmt.Sprintf 函数一样。

package mainimport ("mycode/prose""testing"
)func TestTwoElements(t *testing.T) {list := []string{"apple", "orange"}want := "apple and orange"got := prose.JoinWithCommas(list)if got != want {t.Errorf("TestTwoElements(%#v) = \"%s\", want \"%s\"", list, got, want)}
}func TestThreeElements(t *testing.T) {list := []string{"apple", "orange", "pear"}want := "apple, orange, and pear"got := prose.JoinWithCommas(list)if got != want {t.Errorf("TestTwoElements(%#v) = \"%s\", want \"%s\"", list, got, want)}
}

我们使用这个来替换原来的t.Error("didn't match expected value - 2"),这样就可以看到详细的详细了



6. 抽离出函数

其实对于test里面那些方法,我们希望将具体Error内容抽离出来形成一个函数,这样调用起来就方便一点

package mainimport ("mycode/prose""testing""fmt"
)func TestTwoElements(t *testing.T) {list := []string{"apple", "orange"}want := "apple and orange"got := prose.JoinWithCommas(list)if got != want {t.Error(errString(list, got, want))}
}func TestThreeElements(t *testing.T) {list := []string{"apple", "orange", "pear"}want := "apple, orange, and pear"got := prose.JoinWithCommas(list)if got != want {t.Error(errString(list, got, want))}
}func errString(list[] string, got string, want string) string{return fmt.Sprintf("JoinWithCommas(%#v) = \"%s\", want \"%s\"", list, got, want)
}



7. 修复字符串拼接函数

而对于这个错误,要修复也很简单,我们只需要判断当len(list) == 2的时候单独处理就行了,同样的你会发现当长度只有1的时候也会出问题,这时候我们也是只需要单独处理len(list) == 1的情况

package proseimport "strings"func JoinWithCommas(phrases []string) string {if len(phrases) == 1{return phrases[0]}else if len(phrases) == 2{return phrases[0] + " and " + phrases[1]}else{result := strings.Join(phrases[:len(phrases)-1], ", ")	//对于第一个集合里面的参数使用第二个参数进行连接result += ", and " //在最后一个单词之前插入andresult += phrases[len(phrases)-1]return result}}



8. 运行特定的测试集

有时候我们不需要测试整个集合,而是只想运行几个特定的测试,go test 也提供了对应的指令来帮我们测试。首先我们另外创建一个测试文件:记住文件名以test结尾,否则go不认为这是一个测试文件

package testimport ("fmt""testing"
)func TestFirstLarger(t *testing.T) {want := 2got := Larger(2, 1)if got != want {t.Error(myError(want, got))}
}func TestSecondLarger(t *testing.T) {want := 8got := Larger(4, 8)if got != want {t.Error(myError(want, got))}
}func myError(want int, got int) string {return fmt.Sprintf("预测结果: %v, 实际结果: %v", want, got)
}func Larger(a int, b int) int {if a > b {return a} else {return b}
}

首先,-v 表示输出详细的测试信息

然后,-v -run 名称 表示运行方法中包括指定名称的方法,所以运行某个特定的方法就 go run 文件夹 -v -run 方法名





如有错误,欢迎指出!!!

更多推荐

Head First Go

本文发布于:2024-02-27 15:31:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1707137.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Head

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!