The Unit Test Strategy In Vald

  • Software functional quality
  • Software structural quality

What is the unit test?

func add(i, j int) int {
return i + j
}
func TestAdd(t *testing.T) {
got := add(1,2)
if got != 3 {
t.Errorf("add(1,2) = %d; want 3", got)
}

}

What is Vald?

Why do we need unit tests in Vald?

Unit test strategy

Know what you are testing

Test behaviors and results, not implementation

Test one thing at a time

func TestGetUserData(t *testing.T) {
t.Run("success to get user data", func(t *testing.T) {
// test implementation
})
t.Run("failed to connect to database (unauthorized)", func(t *testing.T) {
// test implementation
})
t.Run("failed to connect to database (host not found)", func(t *testing.T) {
// test implementation
})
t.Run("failed to get non-exists user data", func(t *testing.T) {
// test implementation
})
// other test cases
}

Make tests readable and understandable

  • Test readability
  • Test understandability
func TestInsertUserData(t *testing.T) {
t.Run("test insert user data", func(t *testing.T) { // define a understandable test case name
// define production-like data as test data
u := User {
Name: "Peter Parker",
Age: 17,
}
result := InsertUserData(u)
// check result
})
}

Make tests deterministic

func genRand() int {
return rand.Intn(100)
}

Make tests independent and self-sufficient

func GetEnvs(en string) []string {
e := os.Getenv(en)
if e == "" {
return nil
}
return strings.Split(e, ",")
}
func TestGetEnvs(t *testing.T) {
if err := os.Setenv("env_name", "value"); err != nil {
t.Error(err)
}
// implement the test case
}

Repeat yourself when necessary

Measure code coverage but focus on test coverage

Code coverage & Test coverage

Code coverage

func calcSum(val ...int32) (sum int32) {
if len(val) == 0 {
return
} else {
for _, v := range val {
sum += v
}
return sum
}
}
func TestCalcSum(t *testing.T) {
sum := calcSum(1, 2)
if sum != 3 {
t.Errorf("calcSum return incorrect result")
}
}
$ go test . -cover
ok github.com/vdaas/vald 0.201s coverage: 80.0% of statements
func calcSum(val ...int32) (sum int32) {
if len(val) == 0 { // this statement is executed to check the argument length
return // this is not covered by the test
} else { // not executable code
for _, v := range val { // this part is covered until the return statement below
sum += v
} // not executable code
return sum
}
}

Test coverage

func doSomething(i int) {
// do something
}
  • math.MaxInt32
  • math.MinInt32
func doSomethingWithSlice(ss []string) {
// do something
}
  • nil
  • []string{“”}
  • []string{}
  • []string{“string”}
  • []string{“string1”, “string2”}
func isAdult(a int) bool {
if a >= 20 {
return true
}
return false
}
  • 19
  • 20
  • 21

How do we implement unit tests in Vald?

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
vald.vdaas.org

vald.vdaas.org

A highly scalable distributed fast approximate nearest neighbor dense vector search engine.