Testing the Notify Package

Powerful Command-Line Applications in Go — by Ricardo Gerardi (120 / 127)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Including OS-Specific Files in the Buil d | TOC | Conditionally Building Your Applic ation 👉

Let’s write some tests for this package. To fully test this package, you’ll write unit tests and integration tests.

First, you’ll write unit tests for the package functions and methods, using a test file within the same package notify. For these tests, you’ll mock the command implementation using the same technique you applied in Testing Commands with Mock Resources, allowing you to fully automate the unit tests without generating screen notifications.

Then, you’ll also write integration tests to test the exposed API and ensure the notifications come up onscreen. This test is particularly important because this package doesn’t produce an executable file to try it out. To avoid having notifications displayed every time, you’ll limit the execution of these tests by providing the build tag +build integration. You’ll only execute this test by providing the same tag to the go test tool.

Let’s start by writing the unit tests. Create and edit the file notify_test.go for the unit tests. Add the build constraint +build !integration to execute this file without the integration build tag. Skip a line to ensure Go processes the comments as a build constraint instead of documentation, then add the package definition:

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.