From the Intern's Desk: Google Test at Jungle Disk
In case you are not familiar, Google Test is an open-source testing framework for C++ that provides a fast, flexible and clean method in which to test your software. The framework also has cross-platform support for Mac, Linux and Windows, making it well suited for production software.
My summer internship at Jungle Disk focuses primarily on adding maintainable testing code to specific parts of the Jungle Disk client application. This blog post will focus on some of the things that I have learned while exploring the Google Test framework and crafting solutions to the specific problems I have encountered along the way.
Before, let me explain why Google Test can be incredibly useful in the software development process. The main advantage of using a popular testing framework like Google Test in your C++ project is the ability to make changes without the worry of breaking something unrelated. If you make a mistake, it would be clear from the tests built-in to the code. This freedom from accidentally breaking functionality not only allows for faster development, but it also supports a new way of writing code called test-driven development. If you have programmed before, you might have used some form of test-driven development without even realizing it. Test-driven development strives to make code based on tests instead of specifications alone. As part of my internship, I am moving the code in a direction where test-driven development can be used effectively. However, the process of integrating testing into a project that was originally built without it introduces its own unique challenges.
While Google Test introduces many benefits to the software development cycle, there are also costs to using a testing framework (including Google Test). There is a definite cost in the time spent maintaining testing code. Testing frameworks like Google Test often fail to work effectively with small projects whose code is quickly changing and undergoing frequent refactoring. I have firsthand experience of premature test creation and often the time spent maintaining existing test code can overtake the time adding features. However, the Jungle Disk client is a larger scale project and is also not undergoing frequent refactoring, so what makes integrating Google Test into the Jungle Disk software difficult?
Jungle Disk Client
One of the first statements in the official Google Test documentation is: “Tests should be independent and repeatable.” As I mentioned before, one of the main advantages of using Google Test is the ability to quickly identify mistakes, but they have to be independent mistakes. What does that mean? Having an independent mistake means that breaking a single piece of code does not break the code that depends on it. This is also what makes the addition of Google Test to Jungle Disk codebase difficult. This concept is a bit abstract, but hopefully it will become clearer as I discuss the proper solution.
Google Test natively provides an elegant solution to the problem of independent testing in the form of mock classes. Mock classes provide a platform on which to emulate the functionality of existing classes. While I won’t get into implementation details (the official Google Test documentation is already great), I will briefly discuss the first step to get your software project prepared to use mock classes. To use mock classes, the class you wish to emulate must have an interface class. Because C++ does not have native interfaces, Google Test uses a class with entirely pure virtual functions. By using an interface, you are then able to use polymorphism to substitute the mock class for the target class. The substituted class will emulate a sample of predictable behavior in order to make the tests depending on that functionality also predictable. This makes for an elegant solution to an otherwise difficult problem.
Google Test is a valuable tool and I’m surprised its use is not more common among C++ projects. While I am only a few weeks into my internship here at Jungle Disk, I am fairly sure that Google Test is my new favorite framework. The ability for test-driven development, elegant class encapsulation and substantial documentation make using it a pleasure. I look forward to learning more about this great tool and further integrating Jungle Disk code with the functional capabilities of Google Test.
To learn more about Google Test visit their GitHub page.