Writing Tests for Goci
Learn how to write test cases for goci.
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
:
mkdir -p usercode/processes/goci/testdata/{tool,toolErr}
Switch to the newly created testdata/tool
directory and initialize a new dummy
module for this project:
cd usercode/processes/goci/testdata/toolgo mod init testdata/tool
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:
package addfunc add(a, b int) int {return a + b}
Next, we’ll edit ...