My experience using the Googletest C++ unit test framework

This is a lengthy read, but worth it if you are picking out a C++ Test Framework

When I am writing code for anything, I MUST unit test. For me, writing code without unit testing is like walking a tightrope without a net. The developers whose code I test are another story entirely. I’ve written about them previously in my post “Why developers balk at testing.”

Although I write most of my code in Java, at work, I mostly use C++. My testing is for a distributed system and many distributed systems are written in C++. When I started looking for a c++ unit testing framework, I found that unit testing frameworks for C++ are like the wild west when compared to Java. In Java, if you are unit testing, you use JUnit. In C++, there are choices, lots of choices. What I found problematic was that most of the choices were either not quite robust enough or were no longer being actively supported or just lacked enough documentation to be useful.

When I started exploring C++ unit testing with the developers in our group, we decided to try CxxTest since it has a facility for mock objects. I was a little bothered that the framework is for C++ but written in Perl, however, it was working well and I loved the choice of asserts. The big snag was when I wanted to start including mock objects. With CxxTest, you have to include headers for CxxTest in your source code. This turned me off enough that I started looking at what other frameworks had to offer.

At about this time, and quite serendipitously, Google came out with their C++ unit testing framework called Googletest (note that it’s one word). Although the developers were cranky when I said we were switching, what choice did they have. In my group, QA means Queen of it All…Queen of All testing, that is.

The install for Googletest has a couple of known bugs for Microsoft Visual Studio 2008, but since they are known, it just took some searching on the google site to find the fix. Once we had those fixed, we were able to run the sample tests.

This framework does a much better job of keeping the code and the unit tests separate than CxxTest did. It also keeps the unit tests in a *.cpp file which seems much more logical to me than having everything in a header file, like CxxTest. I haven’t found anything about mock objects, but I’m assuming they could be implemented with an interface if a stub is not adequate.

One approach Google has taken with their framework that I find extremely helpful is the way they have you group your tests. A string is included in the parameters for each test which becomes that test’s test suite. When the *.cpp file containing the tests is executed, the output displays the tests according to the grouping and shows which have passed, which have failed, etc. This makes grouping tests so easy, and developers seem more comfortable with it since all they are doing is copying and pasting a tag as a parameter.

They have also created a syntax that separates tests needing a fixture from those that do not. Any test that calls data set in the SetUp routine starts with TEST_F instead of TEST. This is awesome because I can immediately see which of my tests are using fixtures.

Since Googletest is supported, has documentation (that could be better) and allows for extensions, I will be sticking with it. As of today, its been downloaded over 7,000 times so it looks like I am not alone in my decision.

Please feel welcome to leave comments about Googletest or your C++ unit testing experience.

Reblog this post [with Zemanta]