This chapter covers
- Exploring challenges to testability
- Improving testability by isolating dependencies
- Using dependency injection techniques for testability
In chapter 4, we developed a user-friendly command-line program called the HIT tool, which provides a command-line interface (CLI) for the HIT client. We learned how to parse command-line arguments and flags using the standard library’s os
and flag
packages.
This chapter focuses on testing the HIT tool using various dependency injection techniques. First, we’ll explore the challenges we might face while testing CLI tools and see how to make our tool testable. Next, we’ll add CLI tests to verify the tool’s functionality and unit tests for its flag parser to cover edge cases. The combination of these tests will help ensure that our tool works correctly from the user’s perspective while handling edge cases effectively.
By the end of this chapter, you’ll have a solid understanding of testing CLI tools. Although this chapter explains testing a command-line tool, you can apply the same techniques to any project in which you want to isolate dependencies to have more testable code.
NOTE This chapter requires basic knowledge of interfaces. Check out appendix D for an introduction to interfaces in Go.