All developers have seen them, even in well-structured Golang programs: comments suggesting you keep away from lines of code since they seem to be working in a magic way. These warnings make us timid, fearing we might break something. But applications need to change, to improve and innovate.
That’s why unit tests are a vital part of software development. They help developers know whether the small parts of their software perform their intended function correctly. With the right amount of unit test coverage in place, developers feel more confident to change their implementations, and even refactor it from scratch, knowing they can easily check if the new version is still working as intended.
As software grows in complexity, so does the importance of unit testing and a solid set of tools for it in the same language. And since test code with good coverage can be substantial, it needs to be as readable and maintainable as product code, to encourage developers to use it and gain its benefits.
For our Go community projects such as GoCenter, we make extensive use of the popular Testify module, which provides a set of Golang packages for performing essential unit test functions.
This article shows how you can use Testify’s main features to write unit tests in Go that are easy to read and maintain. It does that by showing how unit tests would look like when using pure Go, introducing Testify’s packages that can help with the task being performed and then showing the resulting code after Testify adoption. We’ll show some best practices for how to perform assertions and write mocks for dependencies.
Testify: A Top Gopher
Testify is a developer-friendly set of packages with over 11,000 stars on GitHub, and has great community support. Testify extends the lightweight
testing framework of Go to perform assertions and mock dependencies.
These features, as well the everyday reliance of our Go community team on it, are a big part of why the Testify module is honored as a “Top Gopher” in GoCenter. When you view GoCenter’s rich metadata about the Testify module, you can see why:
- The module’s ReadMe directs you to comprehensive documentation. We can learn more details about the module’s code through the GoDoc tab, which shows automatically generated documentation of functions and more.
- GoCenter’s Used By and Metrics tabs show that this module is popular and broadly trusted, with many downloads, forks, contributors, and usage by other Go modules.
- GoCenter’s Security tab also reveals that the current version of this module and its dependencies have no known NVD vulnerabilities, as verified by a JFrog Xray deep scan.
A Simple GoLang Unit
To start writing the unit tests first we need a component to test. For this exercise we will use the following service definition:
For this service definition we have an implementation and we are interested in testing it. The implementation has some business logic to determine if a product is reservable or not. The implementation also depends on a Data Access Object component to provide information about the products. The implementation needs to pass the following simplified test cases:
- The service implementation needs to respect the service definition
- Products added to the catalog more than 1 year ago are reservable
- Other products are not reservable
- Products not in the catalog should cause a product not found error
The service implementation looks like this:
Now that we have a simple service, we can use Testify to create unit tests that assure it operates as intended.
The most basic tasks performed by unit tests are assertions. Assertions are usually used to verify if the actions performed by the test using determined input produce the expected output. They can also be used to check if the components follow the desired design rules.
Using pure Go to run the assertions needed to check if the first test case is being honored and also if our service implementation is being initialized properly, we get the following code:
To help with the assertions, Testify provides package
github.com/stretchr/testify/assert. This package has several methods that can help to compare values against expected results. If we replace our comparisons by those methods we get the following:
Besides helping with the assertions, Testify packages also provide better messaging when one of those operations fail. For example, if we forgot to set the productDAO field in the service implementation constructor, we would get the following test failure:
=== RUN TestNewProductServiceImpl TestNewProductServiceImpl: product_service_impl_test.go:22: Error Trace: product_service_impl_test.go:22 Error: Expected value not to be nil. Test: TestNewProductServiceImpl Messages: Product Service dependency not initialized TestNewProductServiceImpl: product_service_impl_test.go:23: Product Service dependency not initialized --- FAIL: TestNewProductServiceImpl (0.00s)
So far, even though we have better messaging and more convenient methods to run the assertions, we were not able to reduce the size of our test. We still have a repeating pattern of if-not-assertion-break that can make it harder to read our test code. To help with that, Testify provides package
github.com/stretchr/testify/require. This package has the same assertion methods provided by the assert package but it will break the test immediately when an assertion fails. Introducing that package, we get the following shorter and easier to read test code:
When testing a component, we ideally want to isolate it completely to avoid having failures elsewhere to compromise our tests. This is especially harder when the component we want to test has dependencies on other components from different layers in our software. In the scenario we are using here, our service implementation depends on a component from the Data Access Object (DAO) layer to access information about the products.
To promote the desired isolation, it is common for developers to write fake simplified implementations of those dependencies to be used during the tests. Those fake implementations are called mocks.
We can create a mock implementation of the ProductDAO to be injected into the service implementation for the test execution. The ProductDAO interface that our mock needs to implement looks like this:
To enable the test execution, it is necessary that the mock provides a behavior compatible with all test cases we want to validate, otherwise we cannot achieve the desired test coverage. Using pure Go, our test case with the mock would look like the following:
The main issue with the approach above is that now our test case logic is distributed. Part of it is implemented in the test case itself, where we send events to the component being tested and run assertions with the results, while the other part is implemented in the mock, which needs to provide behavior compatible to what the test case is testing. It is easy to see how our test case could break now not because of an issue in the test itself, but because the mock is not returning the required data.
Another issue that can make it even more frustrating is that we are also sharing the mock between multiple test cases. It is possible for changes applied to the mock to satisfy one test case needs to break other ones. In our scenario we have only 3 test cases we care about, but you can imagine how messy it could become if we had more complex test cases. Splitting the mock into multiple ones would not necessarily help it as well, and could potentially make it worse with the complexity spread even more. Also, if our mocked interface changes, we would need to update several mocks to keep them compatible.
What we need is to keep the test case logic centralized and independent. To help with that, Testify provides package
github.com/stretchr/testify/mock. This package provides tools to create mocks that allow the behavior to be injected in runtime, which allow the test case itself to do it keeping the mocked logic close to the test logic.
Using Testify mock package to create our DAO mock and moving the mock behavior initialization to the test cases, and also adding Testify require package to run our assertions, our test code looks like this:
In the implementation above, notice how the mock behavior and the test logic are both centralized inside the test case. Also, notice how the registered mock behavior is exclusive to the test case where it is placed, since it belongs to a single mock instance that is not shared between multiple tests. The tests even register different behaviors for product id 1 with no issues at all. ProductDaoTestifyMock can be safely reused between multiple test cases since it has no behavior.
I hope you’ve found useful information in this article and I hope it can help you enjoy writing better unit tests in your projects. To add Testify to you project using Go modules and start playing with it, just run the following commands:
$ export GOPROXY=https://gocenter.io $ go get github.com/stretchr/testify
Check out Testify on GoCenter or search to discover even more great Go modules.