The Unit Test Strategy In Vald

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

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
}
func doSomethingWithSlice(ss []string) {
// do something
}
func isAdult(a int) bool {
if a >= 20 {
return true
}
return false
}

How do we implement unit tests in Vald?

Conclusion

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Importance of Version Control

Dynamic Typing in Python

Colony 🚀 Rinkeby

Beefy.Finance Incident Report: The BUNNY rescue

Decentralized Gaia-X Federations enable freedom of choice for cloud adoption

Coding a social network — it so far uses CI4 base and is Chernobyl deterrent, if not inherent.

The Hello Freshers Story!

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.

More from Medium

Progressive Delivery: How To Implement Flagger with Istio

Deploy “N” Preview Pull Requests to “N” clusters

Leveraging Node Groups to Segregate Workloads

Creating Prometheus Custom Exporters with kloia_exporter Pip Package