Testing the Notify Package

Learn how to write test cases to test the notify package.

Overview

Let’s write some tests to fully test this package. To do that, we’ll write unit tests and integration tests.

First, we’ll write unit tests for the package functions and methods, using a test file within the same package notify. For these tests, we’ll mock the command implementation using the same technique, allowing us to fully automate the unit tests without generating screen notifications.

Then, we’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, we’ll limit the execution of these tests by providing the build tag +build integration. We’ll only execute this test by providing the same tag to the go test tool.

Updating the notify_test.go file

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

Get hands-on with 1400+ tech skills courses.