一、hello
- hello.go
package mainconst (spanish = "Spanish"french = "French"englishHelloPrefix = "Hello, "spanishHelloPrefix = "Hola, "frenchHelloPrefix = "Bonjour, "
)func main() {
}
func Hello(name, language string) string {if name == "" {name = "World"}return greetingPrefix(language) + name
}
func greetingPrefix(language string) (prefix string) {switch language {case french:prefix = frenchHelloPrefixcase spanish:prefix = spanishHelloPrefixdefault:prefix = englishHelloPrefix}return
}
- hello_test.go
package mainimport "testing"func TestHello(t *testing.T) {assertCorrectMessage := func(t *testing.T, got, want string) {//当测试失败时,报错不会显示真正的错误行数,而是会显示至 调用该辅助函数的位置t.Helper() //t.Helper() 需要告诉测试套件这个方法是辅助函数(helper)if got != want {t.Errorf("got '%q' want '%q'", got, want)}}//为西班牙语用户编写测试t.Run("in Spanish", func(t *testing.T) {got := Hello("Elodie", "Spanish")want := "Hola, Elodie"assertCorrectMessage(t, got, want)})t.Run("saying hello to people", func(t *testing.T) { // t.Run 用于在测试函数内部运行子测试got := Hello("Chris", "")want := "Hello, Chris"assertCorrectMessage(t, got, want)})t.Run("empty string defaults to 'world'", func(t *testing.T) {got := Hello("", "")want := "Hello, World"assertCorrectMessage(t, got, want)})
}
二、整数
package integersimport ("fmt""testing"
)func TestAdder(t *testing.T) {sum := Add(2, 2)expected := 4if sum != expected {t.Errorf("expected '%d' but got '%d'", expected, sum)}
}/*go test -v (显示详细信息)因为有这行注释 //Output:6 才会在terminal中输出
=== RUN ExampleAdd
--- PASS: ExampleAdd (0.00s)
并且 添加注释 //Output:值 若给定output后的值 与 结果sum值不匹配 会报错
*/
func ExampleAdd() {sum := Add(1, 5)fmt.Println(sum)//Output:6
}
func Add(x, y int) int {return x + y
}
三、迭代
package iterationconst repeatCount = 5// 将字符串重复5次
func Repeat(character string) string {var repeatStr stringfor i := 0; i < repeatCount; i++ {repeatStr += character}return repeatStr
}
repeat_test.go
// repeat_test.go
package iterationimport ("fmt""testing"
)// 测试用例
func TestRepeat(t *testing.T) {repeated := Repeat("a")expected := "aaaaa"if repeated != expected {t.Errorf("expected '%s' got '%s'", expected, repeated)}
}// ExampleRepeat is a test function for Repeat.
func ExampleRepeat() {sum := Repeat("B")fmt.Println(sum)//Output: BBBBB
}// terminal: go test -bench="."
// 基准测试
func BenchmarkRepeat(b *testing.B) {for i := 0; i < b.N; i++ {Repeat("a")}
}
四、数组与切片
package go_testsimport ("reflect""testing"
)// go test -cover 命令用于运行测试并生成代码覆盖率报告func TestSumAllTails(t *testing.T) {checksSums := func(t *testing.T, got, want []int) {if !reflect.DeepEqual(got, want) {t.Errorf("got %v want %v ", got, want)}}t.Run("make the sums of some slices", func(t *testing.T) {got := SumAllTails([]int{1, 2}, []int{0, 9})want := []int{2, 9}checksSums(t, got, want)})t.Run("safely sum empty slices", func(t *testing.T) {got := SumAllTails([]int{1}, []int{0, 9})want := []int{0, 9}checksSums(t, got, want)})
}// 它会把每个切片的尾部元素相加(尾部的意思就是除去第一个元素以外的其他元素)。
func SumAllTails(numbersToSum ...[]int) []int {var sums []intfor _, v := range numbersToSum {if len(v) == 0 { //[]int{}sums = append(sums, 0)} else {tail := v[1:] //除了第一个不要sums = append(sums, Sum(tail))}}return sums
}
func TestSumAll(t *testing.T) {got := SumAll([]int{1, 2}, []int{0, 9})want := []int{3, 9}//测试或比较两个结构体、切片、映射等复杂数据结构的相等性。if !reflect.DeepEqual(got, want) { //got != wantt.Errorf("got %v want %v ", got, want)}
}// 这回我们需要一个 SumAll 函数,它接受多个切片,并返回由每个切片元素的总和组成的新切片。
func SumAll(numbersToSum ...[]int) []int {var sums []intfor _, v := range numbersToSum {sums = append(sums, Sum(v))}//如果你有一个容量为 2 的切片,但使用 mySlice[10]=1 进行赋值,会报运行时错误。//sums = make([]int, lengthOfNumbers)//for i, v := range numbersToSum {// sums[i] = Sum(v)//}return sums
}// 我们创建一个 Sum 函数,它使用 for 来循环获取数组中的元素并返回所有元素的总和。
func TestSum(t *testing.T) {// 普通sum数组//t.Run("collection of 5 numbers", func(t *testing.T) {// numbers := []int{1, 2, 3, 4, 5}// got := Sum(numbers)// want := 15// if want != got {// t.Errorf("got %d want %d given %v", got, want, numbers)// }//})//动态sum数组t.Run("collection of any size", func(t *testing.T) {numbers := []int{1, 2, 3}got := Sum(numbers)want := 6if want != got {t.Errorf("got %d want %d given %v", got, want, numbers)}})
}func Sum(numbers []int) int {sum := 0for i, _ := range numbers {sum += numbers[i]}return sum
}