这里填写标题
1. Go Unit Testing at Compass
1.1. The Basics of Go Unit Testing
func Sub(x int, y int) int {
return x - y
}
test code:
import "testing"
func TestResult_2(t *testing.T) {
x, y := 6, 3
actual := Sub_1(x, y)
expected := x - y
if actual != expected {
t.Fail()
}
}
1.2. Handling Many Test Cases with Test Tables
func Sub(x int, y int) int {
if x > y {
return x - y
}
if y > x {
return y - x
}
return 0
}
test code:
import (
"github.com/stretchr/testify/assert"
"testing"
)
var subTests = []struct {
x int
y int
expected int
message string
}{
{
6, 2, 4, "x greater than y"},
{
2, 6, 4, "y greater than x"},
{
2, 2, 0, "x and y equal"},
{
-4, -2, 2, "negative values, y greater than x"},
}
func TestSub(t *testing.T) {
for _, test := range subTests {
assert.