BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Announcing: New Google C++ Testing Framework

Announcing: New Google C++ Testing Framework

This item in japanese

The folks at Google have recently open-sourced their xUnit-based testing framework for C++ development. The framework is said by project developer Zhanyong Wan to have been in use internally at Google for years by thousands of their C++ developers.

According to Google the primary highlights of this release are:
  • Google Test is portable: it works on a variety of platforms (Linux, Windows, Mac OS X, and more), with several versions of GCC and MSVC compilers, and with or without exceptions. You can even use it in embedded systems like Windows CE and Symbian. Build tools and test runners for many of these are under active development, with Linux Autotools support already in place.
  • It supports both fatal and nonfatal assertions. The test will continue after a nonfatal failure. This allows more problems to be uncovered and fixed in a single edit-compile-test cycle.
  • It provides many assertions for common testing needs, and lets you easily define new assertions for less common cases.
  • On Linux, you can write death tests to ensure that your code crashes with expected errors.
  • Because it's based on the popular xUnit architecture, Google Test is easy to learn if you've used any testing framework in this family before.
While the C++ community has always been in agreement that their toolset for good unit testing does not live up to that offered for other modern languages, such as Java and C#, many are still sure to ask, "Why another toolset? What's special?". On the Faqs portion of their wiki, Googlers attempt to address this question and many others. They caveat that they are not claiming their toolset to be "better" per se than existing tools, but rather just offering a different combination of functionality that they at Google have found useful for their needs. From the site:
We hope this list can help you decide whether [Google Test is useful] for you too:
  • Google Test is designed to be portable. It works where many STL types (e.g. std::string and std::vector) don't compile. It doesn't require exceptions or RTTI. As a result, it runs on Linux, Mac OS X, Windows and several embedded operating systems.
  • Nonfatal assertions (EXPECT_*) have proven to be great time savers, as they allow a test to report multiple failures in a single edit-compile-test cycle.
  • It's easy to write assertions that generate informative messages: you just use the stream syntax to append any additional information, e.g. ASSERT_EQ(5, Foo(i)) << " where i = " << i;. It doesn't require a new set of macros or special functions.
  • Google Test automatically detects your tests and doesn't require you to enumerate them in order to run them.
  • No framework can anticipate all your needs, so Google Test provides EXPECT_PRED* to make it easy to extend your assertion vocabulary. For a nicer syntax, you can define your own assertion macros trivially in terms of EXPECT_PRED*.
  • Death tests are pretty handy for ensuring that your asserts in production code are triggered by the right conditions.
  • SCOPED_TRACE helps you understand the context of an assertion failure when it comes from inside a sub-routine or loop.
  • You can decide which tests to run using name patterns. This saves time when you want to quickly reproduce a test failure.
Saptarshi "Sunny" Purkayastha spent some time checking the new framework out and posted his initial thoughts on his blog. His reaction was positive:
Another excellent thing about the framework was that it’s easy to learn the basics as well has advanced features. Its very similar to xUnit tests and has good features to compare with CppUnit. The basics can be learnt from here and the advanced guide is here. Predicate Assertions are probably one of my favorite features, that we encountered quickly in a program that we were having trouble to debug. There are also a host of other assertions available and we’ve not got into very deep with all the assertions. But from the initial look at the framework, most of us very ranking it as one of the best testing frameworks out there...
According to the wiki, you can get up and running with Google Test by checking out their Primer, and then referencing the Advanced Guide for more intensive use.

Download the framework here, try it out, and let the community know what you think!

Rate this Article

Adoption
Style

BT