Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of testing in go
package sum
// Ints returns sum of list integers.
func Ints(vs ...int) int {
return ints(vs)
}
func ints(vs []int) int {
if len(vs) == 0 {
return 0
}
return Ints(vs[1:]...) + vs[0]
}
////////////////////////////
package sum_test
import "testing"
import "github.com/jakelacey2012/testing"
func TestInt(t *testing.T) {
tt := []struct {
name string
numbers []int
sum int
}{
{"One to five", []int{1, 2, 3, 4, 5}, 15},
{"No Numbers", nil, 0},
{"one and minus one", []int{1, -1}, 0},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
s := sum.Ints(tc.numbers...)
if s != tc.sum {
t.Fatalf("Sum of %v should be %v; got %v", tc.name, tc.sum, s)
}
})
}
}
//////////////////////////////////
package sum_test
import (
"fmt"
"github.com/jakelacey2012/testing"
)
func ExampleInts() {
s := sum.Ints(1, 2, 3, 4, 5)
fmt.Println("Sum of one to five is", s)
// Output:
// Sum of one to five is 15
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.