package main import ( "fmt" "reflect" "strings" "testing" ) func TestArray(t *testing.T) { fmt.Println("123") } func TestSum(t *testing.T) { x := [...]int{1, 2, 3} sum := 0 for _, v := range x { sum += v } fmt.Println(sum) } //Newsplit 切割字符串 //example: //abc,b=>[ac] func Newsplit(str, sep string) (des []string) { index := strings.Index(str, sep) for index > -1 { sectionBefor := str[:index] des = append(des, sectionBefor) str = str[index+1:] index = strings.Index(str, sep) } //最后1 des = append(des, str) return } //测试用例1:以字符分割 func TestSplit(t *testing.T) { got := Newsplit("123N456", "N") want := []string{"123", "456"} //DeepEqual比较底层数组 if !reflect.DeepEqual(got, want) { //如果got和want不一致说明你写得代码有问题 t.Errorf("The values of %v is not %v\n", got, want) } } //测试用例2:以标点符号分割 func TestPunctuationSplit(t *testing.T) { got := Newsplit("a:b:c", ":") want := []string{"a", "b", "c"} if !reflect.DeepEqual(got, want) { t.FailNow() //出错就stop别往下测了! } }