Debugging With Tests

In this lesson, you will see how the tests can help us find bugs, what we can learn from them, and how to fix them.

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 the bugs.

Type go test in the terminal below which contains the buggy code with the tests that catch it. You will see that one test case fails.

Get hands-on with 1400+ tech skills courses.