Writing Tests for Goci
Explore how to write effective tests for the goci command-line tool by creating sample Go programs for success and failure cases. Learn to implement table-driven tests, capture output, and verify errors using Go's testing packages and custom error types.
We'll cover the following...
Tests for goci
The goci tool executes tasks on a Go project. To write tests for it, we need to create a few small Go programs. Let’s start with two test cases, one for a successful build and one for a failed build, so we can test the error handling.
We’ll create a directory testdata under our project directory to hold the files required for the testing. When building goci, any files under this directory are ignored. In addition, we’ll create two subdirectories under testdata to hold the code for each case: tool and toolErr:
Switch to the newly created testdata/tool directory and initialize a new dummy
module for this project:
Next we create a basic Go library to serve as a test subject. We add a file add.go under testdata/tool with the following content:
Next, we’ll edit the file add.go ...