Testing the Notify Package
Explore writing unit and integration tests for the Notify package in Go. Understand how to mock commands and verify notification sending across platforms. Gain skills to ensure your notification tool behaves reliably without unwanted side effects during testing.
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 ...