Adding Another Step to the Pipeline

Powerful Command-Line Applications in Go — by Ricardo Gerardi (64 / 127)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Defining a Pipeline | TOC | Handling Output from External Pr ograms 👉

You’ve structured your code so you can add more steps to the pipeline by instantiating a new step and adding it to the pipeline slice. According to the initial plan, the next step in the pipeline is the execution of tests using the go test command. But before you add that step, add a test file with a single test case to the add package you’re using for testing.

Switch to the subdirectory testdata/tool, where you have the file add.go:

​ ​$ ​​cd​​ ​​testdata/tool​

In this subdirectory, create the test file add_test.go, and add a single test case to test the add function:

processes/goci.v3/testdata/tool/add_test.go

​ ​package​ add

​ ​import​ (
​ ​"testing"​
​ )

​ ​func​ TestAdd(t *testing.T) {
​ a := 2
​ b := 3

​ exp := 5

​ res := add(a, b)

​ ​if​ exp != res {
​ t.Errorf(​"Expected %d, got %d."​, exp, res)
​ }
​ }

Save the file and execute the test to ensure it works:

​ ​$ ​​go​​ ​​test​​ ​​-v​
​ === RUN TestAdd
​ --- PASS: TestAdd (0.00s)
​ PASS
​ ok testdata/tool 0.003s

Copy the test file to the subdirectory testdata/toolErr where you have the test code that fails to build. Even though the…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.