Debugging With Tests
Explore debugging techniques in Go for command-line programs by using unit and end-to-end tests. Understand common bugs like handling empty inputs and relative paths, and learn best practices for detecting and fixing these issues to improve program reliability.
We'll cover the following...
Discovering bugs
Okay. I admit it. I cheated. When I originally ran the e2e tests some of them failed, and I discovered two separate bugs. Before we get into the bugs themselves, it’s interesting to note that the e2e tests exposed the bugs, but they didn’t really help us discover the bugs. I ran an e2e test, I didn’t get the result that I expected, but it didn’t provide me enough information to understand what went wrong.
To actually find the bugs themselves, I ran multi-git itself in the debugger with the same environment variables and command-line arguments as the failing e2e test case. Then, I was able to locate the actual bug.
Alright, let’s look at the bugs.
Learning from bugs
Bugs are par for the course. Even the best developers have bugs. To paraphrase an old saying, “If you don’t have bugs you’re not really trying”. Now, if your tests catch your bugs, they are not bugs at all in my opinion. It is part of the development process. In this case, my unit tests didn’t catch those particular bugs. I committed them to git and they are part of revision 0.3 of multi-git, so I consider them real bugs. In revision 0.4, I added the end-to-end tests, and this is where I caught ...