Testing the Initial CLI Implementation

Learn how to create test cases for the CLI implementation.

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 integration testsIn integration testing, modules are tested together to verify that they are working as expected. instead. This way we’re testing the user interface of the tool instead of the business logic again.

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 build tool to compile the program into a binary file.
  • Execute the binary file with different arguments and assert its correct behavior.
...

Get hands-on with 1400+ tech skills courses.