Programming

Notes on talk - Advanced testing in golang by Mitchell Hashimoto

This talk has really great takeaways which are worth considering while writing your tests

Suraj Deshmukh

1 minute read

Test Fixtures “go test” sets pwd as package directory Test Helpers should never return an error they should access to the *testing.T object call t.Helper() in the beginning (works only for go1.9+) for things reqiuiring clean up return closures Configurability Unconfigurable behavior is often a point of difficulty for tests. e.g. ports, timeouts, paths. Over-parameterize structs to allow tests to fine-tune their behavior It’s ok to make these configs unexported so only tests can set them.

Methods that satisfy interfaces in golang

What receiver type methods satisfy which interface, can be understood here.

Suraj Deshmukh

3 minute read

Pointer receiver For a struct User with a method Work with pointer receiver. type User struct { Name string Period int } func (u *User) Work() { fmt.Println(u.Name, "has worked for", u.Period, "hrs.") } func main() { uval := User{"UserVal", 5} uval.Work() pval := &User{"UserPtr", 6} pval.Work() } See on go playground. output: UserVal has worked for 5 hrs. UserPtr has worked for 6 hrs. If we call this method on value type object uval it works, and obviously it works with pointer type object pval.