github.com/nibnait/go-learn@v0.0.0-20220227013611-dfa47ea6d2da/src/test/chapter/ch5/36_unit_test/functions_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestSquare(t *testing.T) { 11 inputs := [...]int{1, 2, 3} 12 expected := [...]int{1, 4, 9} 13 14 for i := 0; i < len(inputs); i++ { 15 ret := square(inputs[i]) 16 if ret != expected[i] { 17 t.Errorf("input is %d, the expected is %d, the actual %d", 18 inputs[i], expected[i], ret) 19 } 20 } 21 } 22 23 func TestErrorInCode(t *testing.T) { 24 fmt.Println("Start") 25 t.Error("Error") 26 fmt.Println("End") 27 } 28 29 func TestFailInCode(t *testing.T) { 30 fmt.Println("Start") 31 t.Fatal("Error") 32 fmt.Println("End") 33 } 34 35 func TestSquareWithAssert(t *testing.T) { 36 inputs := [...]int{1, 2, 3} 37 expected := [...]int{1, 4, 9} 38 for i := 0; i < len(inputs); i++ { 39 ret := square(inputs[i]) 40 assert.Equal(t, expected[i], ret) 41 } 42 }