Wednesday, September 07, 2011

Google Test

As usual Google gets the behind-the-scenes stuff just right with their "C++ Testing Framework":

http://code.google.com/p/googletest/

For the simplest possible usage example, just include gtest/gtest.h and add this to your application entry point:

::testing::InitGoogleTest(&argc, argv);
int ret = RUN_ALL_TESTS();

Now, each test you wanna add is declared like so:

TEST(TestCase,TestName)
{
   // test goes here
}

The framework provides a bunch of macros for value tests and assertions, a very simple example being:

ASSERT_EQ( 1, someValue );

where the test fails and spits out an instructive error message in case someValue != 1.

I've found this framework to be extremely simple to learn, but once you scratch the surface you'll find it also has quite a few advanced features such as listeners, reflection, type-parametrized testing etc. to keep you busy for quite some time.