Search⌘ K

Creating the To-Do Test File

Explore how to write comprehensive tests for a to-do API in Go by creating a separate test file. Learn to use temporary files and testing functions to validate adding, completing, deleting, saving, and loading tasks, ensuring your command-line tool works reliably.

The code for the to-do API is complete. Let’s write some tests to ensure it’s working properly.

Creating the todo_test.go file

Let’s start by adding the package definition to a new file named todo_test.go:

Go (1.6.2)
package todo_test
Adding the package

In general, all files in the same directory must belong to the same Go package. An exception to this rule is when writing tests. We can define a different package for our tests to access only the exported types, variables, and functions from the package we’re testing. This is a common practice when testing libraries because it ensures the tests ...