Here is an example of a method we want to test in the main package. The syntax deliberately avoids the use of assertions and leaves the responsibility for checking values and behaviour to the developer. Unit testing in Go is just as opinionated as any other aspect of the language like formatting or naming. Master unit testing in - isolating dependencies, using fakes and checking code coverage with built-in tools. The standard tool-chain also includes benchmarking and statement-based code coverage similar to NCover (.NET) or Istanbul (Node.js). Go has a built-in testing command called go test and a package testing which combine to give a minimal but complete testing experience.
You can carry on and read this post for free, or get the updated and extended version re-written for 2021 with many more examples in my new ebook - Everyday Golang.Ĭheck it out now, or read this version for free 1. It was even referenced in the Kubernetes documentation for the kubeadm tool. This blog post was originally written in 2017, and has received hundreds of thousands of views since then. Read the extended version in my new eBook I'll cover some other recommendations at the end of the post. I consider the following book as essential reference and reading for Golang, you can purchase it on Amazon: Go Programming Language, Addison-Wesley. This post is a continuation of that theme, which covers unit testing. In the previous post titled "Grab JSON from an API" we explored how to interact with a HTTP client and parse JSON.