...

/

Writing Tests for Goci

Writing Tests for Goci

Learn how to write test cases for goci.

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:

Press + to interact
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:

Press + to interact
cd usercode/processes/goci/testdata/tool
go 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 add
func add(a, b int) int {
return a + b
}

Next, we’ll e​dit ...