While Go provides a
testing package and a
go test command, the former only offers basic testing capabilities. The package also has some drawbacks, such as missing assertions and increasing repetition with large-scale tests. As a result, several Go testing frameworks have been created to augment it.
Go testing frameworks consist of tools and resources for creating and designing tests. Some of these frameworks incorporate the
testing package and
go test command, while others take a different approach.
The following is an exploration of the
testing package as well as a roundup of some Go testing frameworks and what they offer.
How Testing Frameworks Help
Testing frameworks offer a range of benefits. They include utilities like assertions and matchers that aren’t present in the
testing package. Many are bundled with tools for advanced test coverage and results reporting, mocking, and automation. Some even help with test generation, which reduces the amount of work needed to write tests. Their reporting tools are user-friendly with color-coded output to distinguish different results in the terminal. The test reports they produce often include dumps of error values and messages, and they print snippets of code failures.
More advanced frameworks supply web UIs that use a browser to run tests and display results. Others provide fine-grained test execution control with their bundled tools, which offer extended filtering, skipping, and parallel test running functionality. These frameworks make testing and debugging faster by cutting down on repetitive tasks. Because they provide assertions and other helper functions to abstract away the more complicated aspects of writing tests, they are also simple to use.
Levels of Testing
The overwhelming majority of Go testing frameworks focus on unit or integration testing. Unit testing is the most popular level of testing implemented by Go developers. It involves writing individual-function tests, cross-function tests, and other intra-package tests to make sure that a distinct package and its functions perform according to their requirements. The goal of unit testing is to ensure that the function units work well individually before integrating them with the rest of the system.
Integration testing, on the other hand, involves testing how the various packages and their modules interface with one another in order to identify and address any problems with how they integrate at their boundaries. This is done incrementally until the system as a whole works properly according to its design requirements.
Go Testing Frameworks
The frameworks included in this roundup are evaluated based on criteria that include the features they offer, whether they work with
go test, ease of use, test execution control, how results are reported, how well they’re maintained, their community, and their documentation.
1. The Go Testing Package
testing package provided by Go supports package testing and uses the
go test command to automatically run these tests. It can run benchmarks as well as sub-tests and sub-benchmarks. It supports test skipping to limit scope, and it makes test setup and teardown available when preparing for and cleaning up after testing.
However, the package does not support assertions or other testing helper functions, as the development team believes implementing these features would add too much complexity. To counter the repetitiveness of writing tests without assertions and helper functions, they suggest using table-driven tests.
The package is simple to use. Tests written with it are readable but aren’t as expressive. It reports test coverage using the
go tool cover command. Test results returned by the Go command can be less descriptive, especially with test failures.
This package is well-documented and the Go website offers FAQs, blog posts, and tutorials. Several entries on testing with it are available on the Go wiki. The core Go team actively supports it, and the Go community provides plenty of help for beginners.
Testify is an incredibly popular testing framework. It makes available a range of assertion functions for comparing and verifying values. It also offers mock functionality to easily mock resources. Its
suite package aids in creating test suites for grouping related tests, along with offering setup and teardown functionality for test preparation and cleanup.
The assertion and mock functionalities make testing easier and faster. With test suites, you can collect related tests and create a shared test setup and teardown. Testify’s test output is standard and not as detailed. However, it does offer the option to annotate assertions with messages, making the tests more expressive.
Testify is very user-friendly. It works well with the
testing package and
go test command. It does not offer its own custom test coverage reporting, unlike other packages. However, coverage can be obtained through the testing package. Its pkg documentation page is thorough and offers numerous examples.
There is a large community of users and contributors for Testify, but updates and new features are few and far between. Testify does offer multiple Slack channels where its users can interact and seek help, so finding support is fairly painless.
GoConvey is a BDD-style testing framework that supports the
go test command. It uses an expressive domain-specific language (DSL) that facilitates the creation of self-documenting, highly readable tests.
You can use its
Convey function to set up contexts and scopes for a test and its
So function to make assertions. GoConvey offers test report output in two ways: in the terminal and through a web UI. The terminal test output is detailed, colorized, and readable. Its web UI offers similar output in a more user-friendly way with the addition of several themes and notification options.
GoConvey has a wide range of assertion helpers for validating and verifying values. It offers code coverage reporting and test setup and teardown functionality. You can generate tests on its web UI. It also supports fine-grained control of test execution, in which you can pause and resume tests. It has a rather large community of contributors, although updates to its codebase are infrequent. Its GitHub wiki is well-documented, with more information available on the GoConvey pkg documentation page and website.
Similar to GoConvey, Ginkgo is a BDD-style testing framework that aids in the creation of expressive test specs. It has multiple container nodes to assist in the organization of specs and to help make assertions. It supports test setup and teardown functionality to prepare for and clean up after individual tests and test suites are run. It allows you to control what test to run with its label feature for collecting related tests. With labels, you can select what tests to run on the command line or in code.
Gingko’s test results output is very readable and can be made available in several formats. You can also customize how the test output is collected. Gingko offers a CLI tool for filtering, running, profiling, and generating test suites. It monitors the test code so that if any changes are made, the tests are rerun.
There is a large and active community of contributors and users behind Gingko. Updates are released frequently. Its website offers detailed documentation, in addition to the documentation on its pkg page.
httpexpect is a REST API and HTTP testing framework. It provides a range of assertions for checking request payloads and responses of multiple types. It also offers chainable builders for creating HTTP requests. Its request builder allows you to construct URL paths and add query parameters, headers, cookies, and payloads in several formats. These request builders and transformers are reusable. Its assertions can be used to check response codes and statuses, payloads, headers, and cookies. It also supports websockets so you can inspect parameters and messages from the connection.
httpexpect’s test result output is verbose, failures are adequately reported, and request and response dumps are made available within it or to an external logger. Clients, loggers, printers, and reporting tools can be customized. It has a large community supporting and contributing to it, but updates to its source code can be infrequent. Detailed documentation is available on its pkg page.
Gomega is a testing framework that offers assertions and matchers. It’s mostly paired with other testing frameworks like Gingko that do not have their own built-in assertions. It provides several matchers but also offers support for creating custom matchers. It allows you to run asynchronous assertions. With Gomega, you can test HTTP clients, streaming buffers, external processes, and complex data types, as well as run benchmarks.
Detailed documentation and guides are available on Gomega’s website, in addition to its pkg documentation page. It can present a bit of a steep learning curve, especially when testing HTTP clients or buffers, for instance. It has an active community of supporters and contributors, and updates to it are fairly regular.
Go testing frameworks improve on the functionality of the
testing package while providing additional features to assist you in your software testing. The different frameworks listed above offer various benefits and abilities to users. The one that works best for you may depend on your comfort level and use case.