Unit testing Go code with mocks and dependency injection
Embedded Examples
Who reads manual pages?! Most users dread working through abstract descriptions of what a system does and prefer working examples of applications that can be copied directly and quickly adapted to individual needs. These example sections are usually hidden in the depths of the instructions, prompting many users to first scroll all the way to the bottom. Unfortunately, it's quite common that these application examples were neglected during development and don't work (anymore), because the developer changed the code and forgot to add the changes to the matching section on the manual page. This is remedied by Go's automatic manual page creator with examples that can be added to be continually tested in the unit test suite.
Figure 2 shows a web version of the manual page, featuring automatically generated and continually tested examples, which were embedded in the original Go code by the developer. The following command:
godoc -http=:6060
launches a webserver on port 6060. If you point a browser at http://localhost:6060/pkg
, you can maneuver to the man page for the package. When a user clicks on the down arrow next to the word Example, this opens a section showing both the sample code and the results expected on the standard output.
The highlight is that this example was generated automatically from the test code of the package in Listing 7 by the godoc
command above. And, on top of that, using go test
, the sample code is actually run in the test suite, and its outcome is compared with what is documented – so the documentation always remains up to date!
Listing 7
example_test.go
01 package myhello 02 03 func ExampleSayHello() { 04 SayHello() 05 // Output: hello 06 }
The way this whole thing works is that Go can identify the ExampleSayHello()
function in Listing 7 as an application example useful for the documentation. How? It simply sees the Example
prefix of the function in the test file (suffix _test
). The web version of the godoc
command then puts it in place with the surrounding documentation, partly extracted from programmer comments and automatic code introspection. By convention, Go interprets the comment in line 5 of Listing 7 ("// Output: hello"
) as the example function's expected output, and go test
executes ExampleSayHello()
and checks whether hello
actually appears on the standard output.
Along with the documentation automatically generated from code and comments in the library file in Listing 8, this results in self-checking documentation. It's a clear benefit for programmers who don't like to read instructions but just go directly to cut and paste. But software maintainers also benefit, as these checks make sure that embarrassing errors, such as the first application example in the documentation failing to run, no longer occur.
Listing 8
example.go
01 package myhello 02 03 import ( 04 "fmt" 05 ) 06 07 func SayHello() { 08 fmt.Println("hello") 09 }
Infos
- Listings for this article: ftp://ftp.linux-magazine.com/pub/listings/linux-magazine.com/226/
- Uber's dig: https://github.com/uber-go/dig
- Google's wire: https://github.com/google/wire
« Previous 1 2 3
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.
-
Gnome OS Transitioning Toward a General-Purpose Distro
If you're looking for the perfectly vanilla take on the Gnome desktop, Gnome OS might be for you.