Testing the Initial CLI Implementation
Explore how to write integration tests for a Go-based command-line to-do application, focusing on testing the user interface rather than business logic. Understand building, executing, and cleaning up test binaries using Go's testing tools and packages. Gain skills to create reliable CLI testing workflows with subtests and external command execution.
We can use different approaches to test our CLI tool. Since we already executed unit tests when developing the to-do API, we don’t need to repeat them here. Our CLI implementation is a wrapper around the API. Let’s leverage
some of Go’s features and write
Benefits of Go
One of the main benefits of Go is that it provides tools for automating the execution of tests out of the box; no additional frameworks or libraries are required.
Since we write tests using Go itself, we can use any resources and features
available with the language to write our test cases. In this case, we’ll use the
os/exec package, which lets us execute external commands.
For this test suite, we need to accomplish two main goals:
- Use the
go buildtool to compile the program into a binary file. - Execute the binary file with different arguments and assert its correct behavior.
Recommended way to execute
The recommended way to execute extra setup before our tests is to use the TestMain() function. This function ...